D - People on a Line

Problem Statement

There are N people standing on the x-axis. Let the coordinate of Person i be xi. For every i, xi is an integer between 0 and 109 (inclusive). It is possible that more than one person is standing at the same coordinate.

You will given M pieces of information regarding the positions of these people. The i-th piece of information has the form (Li,Ri,Di). This means that Person Ri is to the right of Person Li by Di units of distance, that is, xRixLi=Di holds.

It turns out that some of these M pieces of information may be incorrect. Determine if there exists a set of values (x1,x2,…,xN) that is consistent with the given pieces of information.

Constraints

  • 1≤N≤100 000
  • 0≤M≤200 000
  • 1≤Li,RiN (1≤iM)
  • 0≤Di≤10 000 (1≤iM)
  • LiRi (1≤iM)
  • If ij, then (Li,Ri)≠(Lj,Rj) and (Li,Ri)≠(Rj,Lj).
  • Di are integers.

Input

Input is given from Standard Input in the following format:

N M
L1 R1 D1
L2 R2 D2
:
LM RM DM

Output

If there exists a set of values (x1,x2,…,xN) that is consistent with all given pieces of information, print Yes; if it does not exist, print No.


Sample Input 1

Copy
3 3
1 2 1
2 3 1
1 3 2

Sample Output 1

Copy
Yes

Some possible sets of values (x1,x2,x3) are (0,1,2) and (101,102,103).


Sample Input 2

Copy
3 3
1 2 1
2 3 1
1 3 5

Sample Output 2

Copy
No

If the first two pieces of information are correct, x3x1=2 holds, which is contradictory to the last piece of information.


Sample Input 3

Copy
4 3
2 1 1
2 3 5
3 4 2

Sample Output 3

Copy
Yes

Sample Input 4

Copy
10 3
8 7 100
7 9 100
9 8 100

Sample Output 4

Copy
No

Sample Input 5

Copy
100 0

Sample Output 5

Copy
Yes

题意:给定有N个人,M条信息。每条信息:L,R,D,表示第R个人在第L个人的右边距离为D。求根据这M条信息判断安排是否成立。
题解:本来以为可以在线一条一条输入输入信息的同时判断是否合法,好像是不可以。答案说是构成图,然后DFS每一个节点。
 #include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cstdio>
using namespace std;
const int maxn = 1e5 + ;
typedef long long ll;
vector<pair<int, int> > e[maxn];
ll dis[maxn];
bool vis[maxn];
int n, m,flag=; void dfs(int x)
{
vis[x] = ;
for (int i = ; i < e[x].size(); i++)
{
pair<int, int> cur=e[x][i];
if (vis[cur.first]) {
if (dis[cur.first] - dis[x] != cur.second) {
flag = ; break;
}
}
else {
dis[cur.first] = dis[x] + cur.second;
dfs(cur.first);
}
}
} int main()
{
cin >> n >> m;
memset(vis, , sizeof(vis));
for (int i = ; i < m; i++) {
int l, r, d;
cin >> l >> r >> d;
e[l].push_back({ r,d });
e[r].push_back({ l,-d });
}
flag = ;
for (int i = ; i <n; i++) {
if (!vis[i])
dfs(i);
if (flag) {
break;
}
}
if (flag) {
cout << "No" << endl;
}
else
cout << "Yes" << endl;
return ;
}

最新文章

  1. 一个可随意定位置的带色Toast——开源代码Crouton的简单使用
  2. 总结-eclipse
  3. ASP.NET MVC SSO 单点登录设计与实现
  4. C# 根据身份证号码获取简易信息
  5. 社交APP经典死法18种,听野路子产品菜狗怎么说
  6. Http请求的 HttpURLConnection 和 HttpClient
  7. mybatis xml的无效判空
  8. 有关java 8
  9. HTTP错误代码大全
  10. 如何用jQuery实现五星好评
  11. Unity添加多个可视镜头Preview功能(一)
  12. Lua 和 C++ 交互
  13. Windows7 VS2015 下编译 Python3.6.6
  14. 【Java】 剑指offer(22) 链表中倒数第k个结点
  15. 004-读书笔记-企业IT架构转型之道-阿里巴巴中台战略思想与架构实战-共享服务中心建设原则
  16. 20169207《Linux内核原理与分析》第七周作业
  17. Kubeadm 安装
  18. 使用Githubdesktop管理Eclipse项目
  19. typedef 与 #define的区别
  20. python 结构化数据解析

热门文章

  1. 1.appium工作原理及环境搭建
  2. 嘴巴题2 UVA10779 收集者的难题
  3. 阿里云CDN上线 WAF,一站式提供分发+安全能力
  4. laravel--设置不需要csrfToken校验的接口
  5. JAVA--养成好的编程习惯
  6. maven下载安装以及环境配置
  7. 使用session实现一次性验证码
  8. Python实例 类和继承
  9. DNS客户端配置文件/etc/resolv.conf
  10. 小飞音箱wifi配网流程