Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

There are N people standing on the x-axis. Let the coordinate of Person i be xi. For every ixi 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:

  1. N M
  2. L1 R1 D1
  3. L2 R2 D2
  4. :
  5. 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
  1. 3 3
  2. 1 2 1
  3. 2 3 1
  4. 1 3 2

Sample Output 1

Copy
  1. Yes

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


Sample Input 2

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

Sample Output 2

Copy
  1. 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
  1. 4 3
  2. 2 1 1
  3. 2 3 5
  4. 3 4 2

Sample Output 3

Copy
  1. Yes

Sample Input 4

Copy
  1. 10 3
  2. 8 7 100
  3. 7 9 100
  4. 9 8 100

Sample Output 4

Copy
  1. No

Sample Input 5

Copy
  1. 100 0

Sample Output 5

Copy
  1. Yes
  2.  
  3. dfs,用邻接表标记每一个访问过的点,没访问的,更新disdis为当前子图中任意一点到其他点的距离),访问过看一下是否与dis匹配,不匹配就不满足。
    代码:
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <cmath>
  7. #include <iomanip>
  8. using namespace std;
  9. struct info
  10. {
  11. int l,r,d;
  12. }s[];
  13. int n,m;
  14. int flag = ;
  15. int dis[],visited[];
  16. int first[],nexti[];
  17. void dfs(int t)
  18. {
  19. if(!flag)return;
  20. int k = first[t];
  21. while(k != -)
  22. {
  23. if(!flag)return;
  24. if(!visited[s[k].r])
  25. {
  26. dis[s[k].r] = dis[s[k].l] + s[k].d;
  27. visited[s[k].r] = ;
  28. dfs(s[k].r);
  29. }
  30. else if(s[k].d != (dis[s[k].r] - dis[s[k].l]))
  31. {
  32. flag = ;
  33. return;
  34. }
  35. k = nexti[k];
  36. }
  37. }
  38. int main()
  39. {
  40. scanf("%d%d",&n,&m);
  41. memset(first,-,sizeof(first));
  42. for(int i = ;i < m;i ++)
  43. {
  44. scanf("%d%d%d",&s[i].l,&s[i].r,&s[i].d);
  45. nexti[i] = first[s[i].l];
  46. first[s[i].l] = i;
  47. s[i + m].r = s[i].l;
  48. s[i + m].l = s[i].r;
  49. s[i + m].d = -s[i].d;
  50. nexti[i + m] = first[s[i + m].l];
  51. first[s[i + m].l] = i + m;
  52. }
  53. for(int i = ;i <= n;i ++)
  54. {
  55. if(!visited[i])
  56. {
  57. visited[i] = ;
  58. dfs(i);
  59. }
  60. }
  61. if(flag)printf("Yes");
  62. else printf("No");
  63. }

AtCoder Beginner Contest 087 D - People on a Line的更多相关文章

  1. AtCoder Beginner Contest 087 D People on a Line(DFS)

    题意 给出n个点,m组关系L,R,D,L在R的左边距离D,判断是否存在n个人的位置满足m组关系 分析 Consider the following directed graph G: There ar ...

  2. AtCoder Beginner Contest 087 (ABC)

    A - Buying Sweets 题目链接:https://abc087.contest.atcoder.jp/tasks/abc087_a Time limit : 2sec / Memory l ...

  3. AtCoder Beginner Contest 087 B - Coins

    Time limit : 2sec / Memory limit : 256MB Score : 200 points Problem Statement You have A 500-yen coi ...

  4. AtCoder Beginner Contest 100 2018/06/16

    A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...

  5. AtCoder Beginner Contest 052

    没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...

  6. AtCoder Beginner Contest 053 ABCD题

    A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...

  7. AtCoder Beginner Contest 136

    AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...

  8. AtCoder Beginner Contest 137 F

    AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...

  9. AtCoder Beginner Contest 076

    A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...

随机推荐

  1. Hibernate学习---关联关系映射

    关联关系是用到的最多的一种关系,非常重要,在内存中反映为实体关系,映射到DB中主键外键关系,实体间的关联,即对外键的维护,关联关系的发生,即对外键数据的改变. 在这里就不赘述什么是外键什么是主键了. ...

  2. C++ IPv4与IPv6的兼容编码(转,出自http://blog.csdn.net/ligt0610/article/details/18667595)

    这里不再对IPv6 socket相关编程的基础知识进行讲解,只提供一个IP协议无关的服务端和客户端的代码,仅供参考. 服务端代码: #include <iostream> #include ...

  3. Pro*C基础

    SQL变量的申明: EXEC SQL BEGIN DECLARE SECTION; 类型 变量名[长度] varchar2 serv_number[]; 其中可以定义C变量 EXEC SQL END ...

  4. HNOI2019梦游记

    \(Day_0\) 十点半开始睡觉,开始了八个小时的不眠之夜,整晚都没睡着,这状态明天肯定挂了 \(Day_1\) 开局一条鱼,计算几何只会\(20\) 还是\(T2\)的\(20\)纯暴力好打,\( ...

  5. jstl标签 c:if和c:choose

    <c:if test="${test == null}">test为null</c:if> 其意思是,如果test 为 null,那么就打印 “test为n ...

  6. HIVE 配置文件详解

    hive的配置: hive.ddl.output.format:hive的ddl语句的输出格式,默认是text,纯文本,还有json格式,这个是0.90以后才出的新配置: hive.exec.scri ...

  7. JavaWeb -- JSP+JavaBean模式

    SUN公司推出JSP技术后,同时也推荐了两种web应用程序的开发模式,一种是JSP+JavaBean模式,一种是Servlet+JSP+JavaBean模式. JSP+JavaBean模式适合开发业务 ...

  8. nova Scheduling 配置

    Nova中调度配置: scheduler_driver_task_period = scheduler_driver = nova.scheduler.filter_scheduler.FilterS ...

  9. 【转】Oracle中插入和取出图片(用BLOB类型)

    原文地址:http://czllfy.iteye.com/blog/66737 其他参考资料地址:http://lavasoft.blog.51cto.com/62575/321882/ 要在orac ...

  10. 【转载】ORA-12519: TNS:no appropriate service handler found 解决

    感谢原作者! 原文地址:http://www.cnblogs.com/ungshow/archive/2008/10/16/1312846.html ORA-12519: TNS:no appropr ...