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. svn命令获取项目中被忽略文件情况

    第一步.通过终端进入项目目录下第二步.运行命令svn pg svn:ignore -R

  2. tensorflow高效地推导pb模型,完整代码

    from matplotlib import pyplot as plt import numpy as np import os import six.moves.urllib as urllib ...

  3. 冒泡排序算法[C++]

    冒泡排序应该是最容易实现的一种排序算法了.其基本思想是:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一趟:首先比较第1个和第2个数,将小数放前,大数放后.然后比较第2个数和第3个数,将小 ...

  4. 通过gevent实现单线程下的多socket并发

    #通过gevent实现单线程下的多socket并发 服务器 #server side import sys import socket import time import gevent from g ...

  5. union 和order by 使用时排序不正确

    静态专题和APP版专题(order by不起作用): [query] sql=(select sp_f13577,sp_f13576 from sp_t113 where url_1 not like ...

  6. http请求生命周期流程

    https://mp.weixin.qq.com/s/fpA2CThk2L-YBw6z0k4rtw HTTP 请求/相应 1.客户端连接到Web服务器 一个HTTP客户端,通常是浏览器,与Web服务器 ...

  7. SPSS20.O---软件安装

    统计要与大量的数据打交道,涉及繁杂的计算和图表绘制.现代的数据分析工作如果离开统计软件几乎是无法正常开展.在准确理解和掌握了各种统计方法原理之后,再来掌握几种统计分析软件的实际操作,是十分必要的. 常 ...

  8. java-多线程的练习----妖,等待唤醒,代码重构,lock到condition

    1 需求 资源有姓名和性别. 两个线程,    一个负责给姓名和性别赋值,    一个负责获取姓名和性别的值. 要求1,运行一下,解决程序的 "妖"的问题. 要求2,实现正确数据的 ...

  9. 2018-2-13-C#-相对路径转绝对路径

    title author date CreateTime categories C# 相对路径转绝对路径 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23 ...

  10. python的pip更改源,因为我们处于局域网中

    很多时候,比如网络不给力,连接超时.防火墙阻挡等等各种原因,我们可能无法从Python官方的PyPi仓库进行pip安装,这时候可以选择国内的第三方源,推荐使用豆瓣源,速度不错. 使用方法: pip i ...