欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 综合教程 >内容正文

综合教程

AtCoder Beginner Contest 087 D - People on a Line

发布时间:2023/10/11 综合教程 39 老码农
生活随笔 收集整理的这篇文章主要介绍了 AtCoder Beginner Contest 087 D - People on a Line 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。