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, 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

  • 1≤N≤100 000
  • 0≤M≤200 000
  • 1≤Li,RiN (1≤iM)
  • 0≤Di≤10 000 (1≤iM)
  • LiRi (1≤iM)
  • 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

Copy
3 3
1 2 1
2 3 1
1 3 2

Sample Output 1

Copy
Yes

Some possible sets of values (x1,x2,x3) are (0,1,2) and (101,102,103).


Sample Input 2

Copy
3 3
1 2 1
2 3 1
1 3 5

Sample Output 2

Copy
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

Copy
4 3
2 1 1
2 3 5
3 4 2

Sample Output 3

Copy
Yes

Sample Input 4

Copy
10 3
8 7 100
7 9 100
9 8 100

Sample Output 4

Copy
No

Sample Input 5

Copy
100 0

Sample Output 5

Copy
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的更多相关文章

  1. AtCoder Regular Contest 090

    C - Candies 链接:https://arc090.contest.atcoder.jp/tasks/arc090_a 题意:从左上角走到右下角,只能向右和向下走,问能最多能拿多少糖果. 思路 ...

  2. 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 ...

  3. AtCoder Regular Contest 090 C D E F

    C - Candies 题意 求左上角走到右下角最大的数字和. 思路 直接\(dp\) Code #include <bits/stdc++.h> #define maxn 110 usi ...

  4. AtCoder Regular Contest 061

    AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...

  5. AtCoder Regular Contest 094 (ARC094) CDE题解

    原文链接http://www.cnblogs.com/zhouzhendong/p/8735114.html $AtCoder\ Regular\ Contest\ 094(ARC094)\ CDE$ ...

  6. AtCoder Regular Contest 092

    AtCoder Regular Contest 092 C - 2D Plane 2N Points 题意: 二维平面上给了\(2N\)个点,其中\(N\)个是\(A\)类点,\(N\)个是\(B\) ...

  7. AtCoder Regular Contest 093

    AtCoder Regular Contest 093 C - Traveling Plan 题意: 给定n个点,求出删去i号点时,按顺序从起点到一号点走到n号点最后回到起点所走的路程是多少. \(n ...

  8. AtCoder Regular Contest 094

    AtCoder Regular Contest 094 C - Same Integers 题意: 给定\(a,b,c\)三个数,可以进行两个操作:1.把一个数+2:2.把任意两个数+1.求最少需要几 ...

  9. AtCoder Regular Contest 095

    AtCoder Regular Contest 095 C - Many Medians 题意: 给出n个数,求出去掉第i个数之后所有数的中位数,保证n是偶数. \(n\le 200000\) 分析: ...

随机推荐

  1. Django项目: 6.新闻详情页

    一.功能需求分析 1.功能 新闻详情 加载评论功能 添加评论功能 二.新闻详情页 1.业务流程分析 业务流程: 判断前端传递新闻id是否为空,是否为整数,是否存在 2.接口设计 接口说明: 类目 说明 ...

  2. 1.关于Spring Cloud的一些基本知识

    GA代表 general avaliable 通用可用版  也就是 正式发行版 PRE 代表预版本  就是还没有成熟 SNAPSHOT 快照版 这个版本可用 没有bug但是后期还会改进 选了这个spr ...

  3. int 13h,磁盘中断

    直接磁盘服务(Direct Disk Service——INT 13H)  00H —磁盘系统复位 01H —读取磁盘系统状态 02H —读扇区 03H —写扇区 04H —检验扇区 05H —格式化 ...

  4. lumen使用CORS解决跨域问题

    因为公司的业务是前后端分离,web前端和后端接口域名不同,所以存在跨域问题,开始使用的是jsonp解决,但是因为接口风格是rest的,还有delete.put等请求,jsonp就不够用了(涉及HTTP ...

  5. js 之 call 、 apply

    在学习js过程中怎么也绕不过用到call.apply方法,感觉都差不多,现在看看他们的用法,区别 在 javascript 中,call 和 apply 都是为了改变某个函数运行时的上下文(conte ...

  6. finger 工具:用来查询用户信息,侧重用户家目录、登录SHELL等

    finger 工具侧重于用户信息的查询:查询的内容包括用户名(也被称为登录名Login),家目录,用户真实的名字(Name)... ... 办公地址.办公电话:也包括登录终端.写状态.空闭时间等: 我 ...

  7. AntColony 磁力搜索引擎的核心

    介绍 AntColony(Github)是findit磁力搜索引擎的核心.用来在DHT网络中,收集活跃资源的infohash,下载并解析资源的种子文件,存入数据库等.AntColony是若干功能的合集 ...

  8. vue前后端分离

    axios前后端交互 安装 一定要安装到`项目目录下 cnpm install axios 配置 在main.js中配置 //配置axios import axios from 'axios' Vue ...

  9. Angungular.js 的过滤器&工具方法

    字母大小写 数字 货币 截取字符串 截取数组 用JS操作 ----------------------------------------------------------------------- ...

  10. Uva116 Unidirectional TSP

    https://odzkskevi.qnssl.com/292ca2c84ab5bd27a2a91d66827dd320?v=1508162936 https://vjudge.net/problem ...