Welcome to Liukejie's|

liukejie

园龄:1年8个月粉丝:5关注:11

2023-07-01 11:21阅读: 4评论: 0推荐: 0

AT3882 [ARC090B] People on a Line 题解

题目传送门


思路

这道题很简单,就是纯 Dfs。

先建图,表示 lr 的左边,距离为 drl 的右边,距离为 d

再开一个 f 数组,只要这个点没有访问过,就跑一遍 Dfs。

如果发现结果一个点有两个不同的坐标,输出 No,否则就输出 Yes


代码如下:

#include <bits/stdc++.h> 

using namespace std;

const int N = 2e7 + 10;

void add (int x, int y, int z) ;
void Ios () ;
void dfs (int x) ;

bool f[N];

int n, m;
int tot;
int pre[N], now[N], son[N], val[N];
int l[N], r[N], d[N];
int deep[N];

main () {
	Ios () ;
	
	cin >> n >> m;
	for (int i = 1; i <= m; ++ i) {
		cin >> l[i] >> r[i] >> d[i];
		
        // 连边,表示 l 在 r 的左边,距离为 d,r 在 l 的右边,距离为 -d。
		add (l[i], r[i], -d[i]) ;
		add (r[i], l[i], d[i]) ;
	}
	
    // 只要这个点没有访问过,就跑一遍 Dfs。
	for (int i = 1; i <= n; ++ i)
		if (f[i] == 0)
			dfs (i) ;
	
    // 判断是否合法。
	for (int i = 1; i <= m; ++ i) 
		if (deep[r[i]] - deep[l[i]] != d[i]) {
			cout << "No\n" ;
			return 0; 
		}
	
	cout << "Yes \n" ;
}

void add (int x, int y, int z) { // 建图。
	pre[++ tot] = now[x];
	son[tot] =y;
	now[x] = tot;
	val[tot] = z;
}

void Ios () { // cin 加速。
	ios :: sync_with_stdio (0) ;
	cin.tie () ;
	cout.tie () ;
}

void dfs (int x) { // dfs
	f[x] = 1;
	
	for (int i = now[x]; i; i = pre[i]) {
		int y = son[i];
		if (f[y] == 0) {
			deep[y] = deep[x] - val[i];
			dfs (y) ;
		}
	}
}

完结撒花 ∼∼∼

posted @   liukejie  阅读(4)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起