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:

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

dfs,用邻接表标记每一个访问过的点,没访问的,更新dis(dis为当前子图中任意一点到其他点的距离),访问过看一下是否与dis匹配,不匹配就不满足。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
using namespace std;
struct info
{
int l,r,d;
}s[];
int n,m;
int flag = ;
int dis[],visited[];
int first[],nexti[];
void dfs(int t)
{
if(!flag)return;
int k = first[t];
while(k != -)
{
if(!flag)return;
if(!visited[s[k].r])
{
dis[s[k].r] = dis[s[k].l] + s[k].d;
visited[s[k].r] = ;
dfs(s[k].r);
}
else if(s[k].d != (dis[s[k].r] - dis[s[k].l]))
{
flag = ;
return;
}
k = nexti[k];
}
}
int main()
{
scanf("%d%d",&n,&m);
memset(first,-,sizeof(first));
for(int i = ;i < m;i ++)
{
scanf("%d%d%d",&s[i].l,&s[i].r,&s[i].d);
nexti[i] = first[s[i].l];
first[s[i].l] = i;
s[i + m].r = s[i].l;
s[i + m].l = s[i].r;
s[i + m].d = -s[i].d;
nexti[i + m] = first[s[i + m].l];
first[s[i + m].l] = i + m;
}
for(int i = ;i <= n;i ++)
{
if(!visited[i])
{
visited[i] = ;
dfs(i);
}
}
if(flag)printf("Yes");
else printf("No");
}

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. MYSQL:基础—主键

    MYSQL:基础—主键 1.什么是主键 表中的每一行都应该具有可以唯一标识自己的一列(或一组列).而这个承担标识作用的列称为主键. 如果没有主键,数据的管理将会十分混乱.比如会存在多条一模一样的记录, ...

  2. docker 命令添加容器数据卷

    实现宿主机和容器的数据共享 只要建立连接,即使容器exit,主机的修改仍能提现到容器

  3. PAT 天梯赛 L1-026. I Love GPLT 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-026 AC代码 #include <iostream> #include <cstdio&g ...

  4. Linux用户和用户组管理 用户组管理命令

    添加用户组命令:groupadd 命令格式: [root@localhost ~]# groupadd [选项] 组名 选项: 选项 选项说明 -g GID 指定组ID: 修改用户组命令:groupm ...

  5. Android开发之旅-获取地理位置的经度和纬度

    在Android应用程序中,可以使用LocationManager来获取移动设备所在的地理位置信息.看如下实例:新建android应用程序TestLocation. 1.activity_main.x ...

  6. linux环境下的python安装过程(含setuptools)

    这里我不想采用诸如ubuntu下的apt-get install方式进行python的安装,而是在linux下采用源码包的方式进行python的安装. 一.下载python源码包 打开ubuntu下的 ...

  7. Why does typeof array with objects return “Object” and not “Array”?

    https://stackoverflow.com/questions/4775722/check-if-object-is-an-array One of the weird behaviour a ...

  8. space sniffer清理的空间

    部分超级大的单文件,比如数据库 C:\inetpub\logs\LogFiles\W3SVC4 C:\Users\clu\AppData\Local\JetBrains\Transient C:\Us ...

  9. UVA 11186 Circum Triangle (枚举三角形优化)(转)

    题意:圆上有n个点,求出这n个点组成的所有三角形的面积之和 题解: 当我们要求出S(i,j,k)时,我们需要假设k在j的左侧,k在i与j之间,k在i的右侧. 如果k在 j的左侧  那么 S(i,j,k ...

  10. DanceLink

    DanceLink是一个可以解决精确覆盖和重复覆盖的搜索算法 重复覆盖就是在精确覆盖的remove等处做改变 都是十字循环链表 精确覆盖 给出一个01矩阵 要求选择几行 使每一列都有且仅有一个1 在求 ...