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. PAT 天梯赛 L1-024. 后天 【取余】

    题目链接 https://www.patest.cn/contests/gplt/L1-024 题意 给出一个数,表示星期几,输出后天是星期几 思路 取余的时候要小心点 AC代码 #include & ...

  2. 基于R语言的数据分析和挖掘方法总结——描述性统计

    1.1 方法简介 描述性统计包含多种基本描述统计量,让用户对于数据结构可以有一个初步的认识.在此所提供之统计量包含: 基本信息:样本数.总和 集中趋势:均值.中位数.众数 离散趋势:方差(标准差).变 ...

  3. usb2.0 协议分析

    转:https://blog.csdn.net/u011594613/article/details/48291307 一.USB硬件介绍1.1.概述 一条USB传输线分别由地线.电源线.D+和D-四 ...

  4. 主攻ASP.NET.4.5.1 MVC5.0之重生:在项目中使用zTree jQuery 树插件

    效果图和json格式 Controllers代码 using HR.Models; using HR.Models.Repository; /***************************** ...

  5. 20145230《Java程序设计》第5周学习总结

    20145230 <Java程序设计>第5周学习总结 教材学习内容 本周主要学习的内容是关于异常处理的,感觉这部分内容对我们这种初学者 来说非常重要.举个例子,倘若你在编写一个Java程序 ...

  6. 20145231《Java程序设计》第五次实验报告

    实验五 Java网络编程及安全 实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统. 实验要求 基于Java Socket实现安全传输 基于TCP实现客户端和服 ...

  7. matplotlib模块之plot画图

    关于matplotlib中一些常见的函数,https://www.cnblogs.com/TensorSense/p/6802280.html这篇文章讲的比较清楚了,https://blog.csdn ...

  8. INSPIRED启示录 读书笔记 - 第7章 管理产品经理

    产品总监的关键职责 1.组建优秀的产品经理团队 如果产品经理不称职,只能退而求其次,请其他团队成员(比如主程序员)越俎代庖 新产品经理必须经过约三个月刻苦学习才能开始管理产品,管理者应该为新人创造学习 ...

  9. How does asp.net web api work?

    https://hub.packtpub.com/working-aspnet-web-api/ https://docs.microsoft.com/en-us/aspnet/web-api/ove ...

  10. Spark基本概念快速入门

      Spark集群 一组计算机的集合,每个计算机节点作为独立的计算资源,又可以虚拟出多个具备计算能力的虚拟机,这些虚拟机是集群中的计算单元.Spark的核心模块专注于调度和管理虚拟机之上分布式计算任务 ...