您的位置:首页 > 运维架构

AtCoder Beginner Contest 087 D - People on a Line 并查集(区间和问题)

2018-02-14 11:56 441 查看
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, xRi−xLi=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,Ri≤N (1≤i≤M)
0≤Di≤10 000 (1≤i≤M)
Li≠Ri (1≤i≤M)
If i≠j, 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

3 3
1 2 1
2 3 1
1 3 2
Sample Output 1

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

Sample Input 2

3 3
1 2 1
2 3 1
1 3 5
Sample Output 2

No
If the first two pieces of information are correct, x3−x1=2 holds, which is contradictory to the last piece of information.

Sample Input 3

4 3
2 1 1
2 3 5
3 4 2
Sample Output 3

Yes
Sample Input 4

10 3
8 7 100
7 9 100
9 8 100
Sample Output 4

No
Sample Input 5

100 0
Sample Output 5

Yes
题意:给出n个数字,m个区间和,判断是否存在那样的区间数字满足m个区间和
思路:并查集区间判断#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 200020;
int parent
;
int sum
; //记录当前结点到根结点的距离
int root(int x)
{
if(parent[x]==x) return x;//一直到寻找到根节点
int t=parent[x]; //存贮中间节点
parent[x]=root(parent[x]);
sum[x]+=sum[t];
return parent[x];
}
void merge(int x,int y,int a,int b,int v)
{
parent[x]=y;
sum[x]=v+sum[b]-sum[a];

}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2)
{
for(int i=0;i<=n;i++)
parent[i]=i;
memset(sum,0,sizeof(sum));
int a,b,v,re=0;
while(m--){
scanf("%d%d%d",&a,&b,&v);
a--;
int x=root(a);
int y=root(b);
if(x==y&&sum[a]-sum[b]!=v){ //同根节点,但值不为之前的事实
re++;
}
else if (x!=y){
merge(x,y,a,b,v);
}
}
printf("%d\n",re);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: