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.
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个关系,n个人站在x轴上,给你的关系包括
Li,Ri,Di
L
i
,
R
i
,
D
i
,表示站在
Li
L
i
位置的人距离右边
Di
D
i
距离是
Ri
R
i
,让你判断这m个关系是否完全正确
分析: 首先我们考虑用vector建邻接表,对于每个点进行DFS(考虑不是连通图的情况,可能会漏判),我们可以判断进而更新新的数据,当然得利用vis标记下,BFS一样,我们可以多点一起判断,这样速度快点,具体看代码
DFS代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
const int maxn = 1e6 + 10, INF = 1000000009;
ll a[maxn];
bool flg = false;
vector<pair<int,ll> > E[maxn];
void init() {
for(int i = 0;i < maxn;i++) a[i] = INF;
}
int n,m;
bool vis[maxn];
void dfs(int idx) {
for(int i = 0;i < E[idx].size();i++) {
int to = E[idx][i].fi;
int v = E[idx][i].se;
if(vis[to]) continue;
if(a[to] != INF) {
if(a[to] - a[idx] != v) {
flg = true;
return ;
}
} else {
a[to] = a[idx] + v;
dfs(to);
if(flg) return;
}
}
vis[idx] = true;
if(flg) return;
}
int L[maxn];
int main(){
ios_base::sync_with_stdio(0);
cin>>n>>m;
for(int i = 0;i < m;i++) {
int l,r,d;cin>>l>>r>>d;
L[i] = l;
E[l].push_back(make_pair(r,d));
E[r].push_back(make_pair(l,-d));
}
init();
for(int i = 0;i < n;i++) {
if(E[L[i]].size() >= 1) {
if(vis[L[i]]) continue;
a[L[i]] = 0;
dfs(L[i]);
if(flg) break;
}
}
if(flg) cout<<"No"<<endl;
else cout<<"Yes"<<endl;
return 0;
}
BFS代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 5e5 + 10, INF = 1000000009;
vector<pair<int,int> > E[maxn];
ll d[maxn];
int main(){
ios_base::sync_with_stdio(0);
init();
int n,m;cin>>n>>m;
for(int i = 0;i < m;i++) {
int l,r,d;cin>>l>>r>>d;
E[r].push_back(make_pair(l,-d));
E[l].push_back(make_pair(r,d));
}
for(int i = 0;i < maxn;i++) d[i] = INF;
for(int i = 1;i <= n;i++) {
if(d[i] == INF) {
queue<int> q;
q.push(i);
d[i] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for(int j = 0;j < E[u].size();j++) {
int v = E[u][j].first;
int val = E[u][j].second;
if(d[v] == INF) {
d[v] = d[u] + (ll)val;
q.push(v);
} else if(d[v] - d[u] != val) {
cout<<"No"<<endl;
return 0;
}
}
}
}
}
cout<<"Yes"<<endl;
return 0;
}
- 如有错误或遗漏,请私聊下UP,thx