AtCoder Regular Contest 088 D - People on a Line(DFS+图论)

AtCoder Regular Contest 088 D - People on a Line(DFS+图论)


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, 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
  • 1N100 000
  • 0M200 000
  • 1Li,RiN (1iM)
  • 0Di10 000 (1iM)
  • LiRi (1iM)
  • 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
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, x3x1=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
用邻接表储存图,然后遍历,记录每个点之间距离,如果再次到达访问的点,发现距离和之前储存的不同则返回false

最后返回true;

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int N = 1e5+10;
int n,m;
int dis[N];
bool vis[N];
vector<pair<int,int> >E[N];
bool dfs(int u){
    vis[u] = 1;
    vector<pair<int,int> >::iterator it;
    for(it = E[u].begin(); it != E[u].end(); it++){
        int v = (*it).first;//注意这里的迭代器要加(*)
        int d = (*it).second;
        if(!vis[v]){
            dis[v] = dis[u] + d;
            if(!dfs(v))
                return false;
        }
        else if(dis[u]+d!=dis[v])
            return false;
    }
    return true;
}
int main(){
    cin >> n >> m;
    int u,v,w;
    int i;
    for(i = 1; i <= m; i++){
        cin >> u >> v >> w;
        E[u].push_back(make_pair(v,w));
        E[v].push_back(make_pair(u,-w));
    }
    for(i = 1; i <= n; i++){
        if(!vis[i]){
            if(!dfs(i)){
                cout << "No" << endl;
                return 0;
            }
        }
    }
    cout << "Yes" << endl;
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值