AtCoder Beginner Contest 087 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
- dfs,用邻接表标记每一个访问过的点,没访问的,更新dis(dis为当前子图中任意一点到其他点的距离),访问过看一下是否与dis匹配,不匹配就不满足。
代码:
- #include <iostream>
- #include <algorithm>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <cmath>
- #include <iomanip>
- using namespace std;
- struct info
- {
- int l,r,d;
- }s[];
- int n,m;
- int flag = ;
- int dis[],visited[];
- int first[],nexti[];
- void dfs(int t)
- {
- if(!flag)return;
- int k = first[t];
- while(k != -)
- {
- if(!flag)return;
- if(!visited[s[k].r])
- {
- dis[s[k].r] = dis[s[k].l] + s[k].d;
- visited[s[k].r] = ;
- dfs(s[k].r);
- }
- else if(s[k].d != (dis[s[k].r] - dis[s[k].l]))
- {
- flag = ;
- return;
- }
- k = nexti[k];
- }
- }
- int main()
- {
- scanf("%d%d",&n,&m);
- memset(first,-,sizeof(first));
- for(int i = ;i < m;i ++)
- {
- scanf("%d%d%d",&s[i].l,&s[i].r,&s[i].d);
- nexti[i] = first[s[i].l];
- first[s[i].l] = i;
- s[i + m].r = s[i].l;
- s[i + m].l = s[i].r;
- s[i + m].d = -s[i].d;
- nexti[i + m] = first[s[i + m].l];
- first[s[i + m].l] = i + m;
- }
- for(int i = ;i <= n;i ++)
- {
- if(!visited[i])
- {
- visited[i] = ;
- dfs(i);
- }
- }
- if(flag)printf("Yes");
- else printf("No");
- }
AtCoder Beginner Contest 087 D - People on a Line的更多相关文章
- AtCoder Beginner Contest 087 D People on a Line(DFS)
题意 给出n个点,m组关系L,R,D,L在R的左边距离D,判断是否存在n个人的位置满足m组关系 分析 Consider the following directed graph G: There ar ...
- AtCoder Beginner Contest 087 (ABC)
A - Buying Sweets 题目链接:https://abc087.contest.atcoder.jp/tasks/abc087_a Time limit : 2sec / Memory l ...
- AtCoder Beginner Contest 087 B - Coins
Time limit : 2sec / Memory limit : 256MB Score : 200 points Problem Statement You have A 500-yen coi ...
- AtCoder Beginner Contest 100 2018/06/16
A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...
- AtCoder Beginner Contest 052
没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...
- AtCoder Beginner Contest 053 ABCD题
A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...
- AtCoder Beginner Contest 136
AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...
- AtCoder Beginner Contest 137 F
AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...
- AtCoder Beginner Contest 076
A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...
随机推荐
- Hibernate学习---关联关系映射
关联关系是用到的最多的一种关系,非常重要,在内存中反映为实体关系,映射到DB中主键外键关系,实体间的关联,即对外键的维护,关联关系的发生,即对外键数据的改变. 在这里就不赘述什么是外键什么是主键了. ...
- C++ IPv4与IPv6的兼容编码(转,出自http://blog.csdn.net/ligt0610/article/details/18667595)
这里不再对IPv6 socket相关编程的基础知识进行讲解,只提供一个IP协议无关的服务端和客户端的代码,仅供参考. 服务端代码: #include <iostream> #include ...
- Pro*C基础
SQL变量的申明: EXEC SQL BEGIN DECLARE SECTION; 类型 变量名[长度] varchar2 serv_number[]; 其中可以定义C变量 EXEC SQL END ...
- HNOI2019梦游记
\(Day_0\) 十点半开始睡觉,开始了八个小时的不眠之夜,整晚都没睡着,这状态明天肯定挂了 \(Day_1\) 开局一条鱼,计算几何只会\(20\) 还是\(T2\)的\(20\)纯暴力好打,\( ...
- jstl标签 c:if和c:choose
<c:if test="${test == null}">test为null</c:if> 其意思是,如果test 为 null,那么就打印 “test为n ...
- HIVE 配置文件详解
hive的配置: hive.ddl.output.format:hive的ddl语句的输出格式,默认是text,纯文本,还有json格式,这个是0.90以后才出的新配置: hive.exec.scri ...
- JavaWeb -- JSP+JavaBean模式
SUN公司推出JSP技术后,同时也推荐了两种web应用程序的开发模式,一种是JSP+JavaBean模式,一种是Servlet+JSP+JavaBean模式. JSP+JavaBean模式适合开发业务 ...
- nova Scheduling 配置
Nova中调度配置: scheduler_driver_task_period = scheduler_driver = nova.scheduler.filter_scheduler.FilterS ...
- 【转】Oracle中插入和取出图片(用BLOB类型)
原文地址:http://czllfy.iteye.com/blog/66737 其他参考资料地址:http://lavasoft.blog.51cto.com/62575/321882/ 要在orac ...
- 【转载】ORA-12519: TNS:no appropriate service handler found 解决
感谢原作者! 原文地址:http://www.cnblogs.com/ungshow/archive/2008/10/16/1312846.html ORA-12519: TNS:no appropr ...