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 ;
}

AtCoder Regular Contest 090 D - People on a Line的更多相关文章

  1. AtCoder Regular Contest 090

    C - Candies 链接:https://arc090.contest.atcoder.jp/tasks/arc090_a 题意:从左上角走到右下角,只能向右和向下走,问能最多能拿多少糖果. 思路 ...

  2. AtCoder Regular Contest 090 F - Number of Digits

    题目链接 Description For a positive integer \(n\), let us define \(f(n)\) as the number of digits in bas ...

  3. AtCoder Regular Contest 090 C D E F

    C - Candies 题意 求左上角走到右下角最大的数字和. 思路 直接\(dp\) Code #include <bits/stdc++.h> #define maxn 110 usi ...

  4. AtCoder Regular Contest 061

    AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...

  5. AtCoder Regular Contest 094 (ARC094) CDE题解

    原文链接http://www.cnblogs.com/zhouzhendong/p/8735114.html $AtCoder\ Regular\ Contest\ 094(ARC094)\ CDE$ ...

  6. AtCoder Regular Contest 092

    AtCoder Regular Contest 092 C - 2D Plane 2N Points 题意: 二维平面上给了\(2N\)个点,其中\(N\)个是\(A\)类点,\(N\)个是\(B\) ...

  7. AtCoder Regular Contest 093

    AtCoder Regular Contest 093 C - Traveling Plan 题意: 给定n个点,求出删去i号点时,按顺序从起点到一号点走到n号点最后回到起点所走的路程是多少. \(n ...

  8. AtCoder Regular Contest 094

    AtCoder Regular Contest 094 C - Same Integers 题意: 给定\(a,b,c\)三个数,可以进行两个操作:1.把一个数+2:2.把任意两个数+1.求最少需要几 ...

  9. AtCoder Regular Contest 095

    AtCoder Regular Contest 095 C - Many Medians 题意: 给出n个数,求出去掉第i个数之后所有数的中位数,保证n是偶数. \(n\le 200000\) 分析: ...

随机推荐

  1. php匿名函数与闭包函数

    匿名函数:没有名字的函数:并没有牵扯到应用其他函数的变量问题.仅仅是没有名字 $f=function($param){} 闭包:A函数中嵌套着B函数,B程序中有用到A的变量,当外部函数C调用函数A时, ...

  2. LINUX用户身份切换

    Su 命令作用 su的作用是变更为其它使用者的身份,超级用户除外,需要键入该使用者的密码. 使用方式 su [-fmp] [-c command] [-s shell] [--help] [--ver ...

  3. linux系统之间互传文件

    参考网址:http://blog.csdn.net/shaoxiaohu1/article/details/23191637 1.文件复制:本机->远程服务器: scp /home/shaoxi ...

  4. vim Tab的设置问题

    VIM 中处理 TAB 异常的方便. 键盘上的 TAB 键,与文件中的 TAB 符号一定要区分开,这是两个概念. 显示当前文件中的 Tab /\t 实际上,就是查询 \t 符号. shiftwidth ...

  5. Python之路,Day3- Python基础(转载Alex)

    本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 温故知新 1. 集合 主要作用: 去重 关系测 ...

  6. spring boot自动配置之jdbc(转)

    1.DataSource配置 1.1 默认配置application.xml spring.datasource.url=jdbc:mysql://localhost/test spring.data ...

  7. UE4物理模块(三)---碰撞查询(下)SAP/MBP/BVH算法简介

    在上一文中介绍了碰撞查询的配置方法: Jerry:UE4物理模块(三)---碰撞查询(上)​zhuanlan.zhihu.com 本篇介绍下UE4的各种零大小的射线检测,以及非零大小(带体积)的射线检 ...

  8. 让Drewtech的J2534 ToolBox 软件支持任何J2534的设备

    更改windows注册表中的FunctionLibrary和ConfigApplication,将DLL和exe路径替换原来的,其他不要动. 或者 create second key in regis ...

  9. 阿里云出手SaaS生态,中国SaaS市场小而不强有望破解

    企业服务SaaS市场还有很大的增长空间.SaaS的鼻祖Salesforces今年5月迈上了千亿美元市值的门槛,再一次为ToB市场注入了兴奋剂.单单一个SaaS CRM,目前全球的市场规模就超过400亿 ...

  10. 小米网关api

    http://bbs.xiaomi.cn/t-13198850 https://github.com/snOOrz/homebridge-aqara/blob/master/README.md htt ...