AtCoder Regular Contest 090 D - People on a Line
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条信息。每条信息:L,R,D,表示第R个人在第L个人的右边距离为D。求根据这M条信息判断安排是否成立。
题解:本来以为可以在线一条一条输入输入信息的同时判断是否合法,好像是不可以。答案说是构成图,然后DFS每一个节点。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cstdio>
using namespace std;
const int maxn = 1e5 + ;
typedef long long ll;
vector<pair<int, int> > e[maxn];
ll dis[maxn];
bool vis[maxn];
int n, m,flag=; void dfs(int x)
{
vis[x] = ;
for (int i = ; i < e[x].size(); i++)
{
pair<int, int> cur=e[x][i];
if (vis[cur.first]) {
if (dis[cur.first] - dis[x] != cur.second) {
flag = ; break;
}
}
else {
dis[cur.first] = dis[x] + cur.second;
dfs(cur.first);
}
}
} int main()
{
cin >> n >> m;
memset(vis, , sizeof(vis));
for (int i = ; i < m; i++) {
int l, r, d;
cin >> l >> r >> d;
e[l].push_back({ r,d });
e[r].push_back({ l,-d });
}
flag = ;
for (int i = ; i <n; i++) {
if (!vis[i])
dfs(i);
if (flag) {
break;
}
}
if (flag) {
cout << "No" << endl;
}
else
cout << "Yes" << endl;
return ;
}
AtCoder Regular Contest 090 D - People on a Line的更多相关文章
- AtCoder Regular Contest 090
C - Candies 链接:https://arc090.contest.atcoder.jp/tasks/arc090_a 题意:从左上角走到右下角,只能向右和向下走,问能最多能拿多少糖果. 思路 ...
- AtCoder Regular Contest 090 F - Number of Digits
题目链接 Description For a positive integer \(n\), let us define \(f(n)\) as the number of digits in bas ...
- AtCoder Regular Contest 090 C D E F
C - Candies 题意 求左上角走到右下角最大的数字和. 思路 直接\(dp\) Code #include <bits/stdc++.h> #define maxn 110 usi ...
- AtCoder Regular Contest 061
AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...
- AtCoder Regular Contest 094 (ARC094) CDE题解
原文链接http://www.cnblogs.com/zhouzhendong/p/8735114.html $AtCoder\ Regular\ Contest\ 094(ARC094)\ CDE$ ...
- AtCoder Regular Contest 092
AtCoder Regular Contest 092 C - 2D Plane 2N Points 题意: 二维平面上给了\(2N\)个点,其中\(N\)个是\(A\)类点,\(N\)个是\(B\) ...
- AtCoder Regular Contest 093
AtCoder Regular Contest 093 C - Traveling Plan 题意: 给定n个点,求出删去i号点时,按顺序从起点到一号点走到n号点最后回到起点所走的路程是多少. \(n ...
- AtCoder Regular Contest 094
AtCoder Regular Contest 094 C - Same Integers 题意: 给定\(a,b,c\)三个数,可以进行两个操作:1.把一个数+2:2.把任意两个数+1.求最少需要几 ...
- AtCoder Regular Contest 095
AtCoder Regular Contest 095 C - Many Medians 题意: 给出n个数,求出去掉第i个数之后所有数的中位数,保证n是偶数. \(n\le 200000\) 分析: ...
随机推荐
- java基础之异常。
异常: (1)异常:异常就是Java程序在运行过程中出现的错误. 异常由来:问题也是现实生活中一个具体事务,也可以通过java 的类的形式进行描述,并封装成对象.其实就是Java对不正常情况进行描述后 ...
- Django项目: 6.新闻详情页
一.功能需求分析 1.功能 新闻详情 加载评论功能 添加评论功能 二.新闻详情页 1.业务流程分析 业务流程: 判断前端传递新闻id是否为空,是否为整数,是否存在 2.接口设计 接口说明: 类目 说明 ...
- AIX系统搭建NFS服务器
本文使用场景:aix6.1升级到aix7.1之后,需要打补丁aix7.1 TL4的补丁,补丁文件有将近10G,当多个系统都升级时,此时搭建nfs服务器,只需要一次上传,其余需升级系统作为客户端只需通过 ...
- javascript基础:bom
一.BOM 1.概念:Browser Object Model 浏览器对象模型 * 将浏览器的各个组成部分封装成对象 2.组成: * Window:窗口对象 1 ...
- Java数据结构和算法(八)--红黑树与2-3树
红黑树规则: 1.每个节点要么是红色,要么是黑色 2.根节点都是黑色节点 3.每个叶节点是黑色节点 3.每个红色节点的两个子节点都是黑色节点,反之,不做要求,换句话说就是不能有连续两个红色节点 4.从 ...
- tyvj 1423 GF和猫咪的玩具
传送门 解题思路 题目比较水,floyd求出最短路取个最小值即可.结果joyoi时限写错了..好像只有0ms才能过??突然发现加了快读就T不加就A,数据在10000以下的还是scanf快啊. 代码 # ...
- union 和order by 使用时排序不正确
静态专题和APP版专题(order by不起作用): [query] sql=(select sp_f13577,sp_f13576 from sp_t113 where url_1 not like ...
- div style标签内嵌CSS样式
我们在DIV标签内.SPAN标签内.p标签等html标签内使用style属性直接设置div的样式. 一.在<div>标签内使用style设置css样式 - TOP 1.实例html ...
- Python学习(一) 安装,环境搭建,IDE
第一篇废话太多了,我的博客最主要的是给自己看的,大家觉得还凑合也可以看看,能说自己想法的就更好了,因为一个人的思想是有局限性的.集思广益,自己的认知才不会被禁锢. 注:其他的系统没装,在Windows ...
- oracle function学习1
oracle function学习基层: 函数就是一个有返回值的过程. 首先 知道oracle 使用限制: 函数调用限制: 1. SQL语句中只能调用存储函数(服务器端),而不能调用客户端 ...