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 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.
Problem D
Consider the following directed graph G:
-
There are N vertices numbered 1,2,...,N.
-
For each i, there are an edge from vertex Li to vertex Ri with weight Di, and an edge from vertex
Ri to vertex Li with weight −Di.
The problem asks whether we can assign an integer xv to each vertex v in G, such that for each edgefrom u to v with cost d, xv − xu = d holds. (Clearly, we can ignore the condition 0 ≤ xi ≤ 109.)
We handle each connected component in G independently. For each connected component, we choosean arbitary vertex v and assume that xv = 0. By running a dfs from v, we can uniquly determine thevalues of xi in this component. After that, we should check if the conditions are actually satisfied.
#include <string>
#include <string.h>
#include <iostream>
#include <vector>
#include <math.h>
#include <stdio.h>
using namespace std;
const int maxn = 400200;
int head[maxn],nxt[maxn],to[maxn],w[maxn];
int flag[maxn];
int sz;
int n,m;
int ans = 1;
void init()
{
for(int i=1;i<=n;i++) head[i]=-1,flag[i]=0;
sz=0;
}
void add(int u,int v,int ww)
{
nxt[++sz]=head[u];
to[sz]=v;
w[sz]=ww;
head[u]=sz;
}
int dis[maxn];
void dfs(int u,int pre)
{
if(!ans) return;
for(int i=head[u];~i;i=nxt[i]){
int v = to[i];
if(v==pre) continue;
if(flag[v]){
if(dis[v]-dis[u]!=w[i]){
ans=0;
return;
}
}else{
flag[v]=1;
dis[v]=dis[u]+w[i];
dfs(v,u);
}
}
}
int main()
{
scanf("%d %d",&n,&m);
init();
for(int i=1;i<=m;i++){
int l,r,d;scanf("%d %d %d",&l,&r,&d);
add(l,r,d);
add(r,l,-d);
}
for(int i=1;i<=n;i++){
if(flag[i]==0){
flag[i]=1;
dis[i]=0;
dfs(i,-1);
}
}
if(ans){
printf("Yes\n");
}else
printf("No\n");
return 0;
}