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

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

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

  2. AtCoder Beginner Contest 087 (ABC)

    A - Buying Sweets 题目链接:https://abc087.contest.atcoder.jp/tasks/abc087_a Time limit : 2sec / Memory l ...

  3. AtCoder Beginner Contest 087 B - Coins

    Time limit : 2sec / Memory limit : 256MB Score : 200 points Problem Statement You have A 500-yen coi ...

  4. AtCoder Beginner Contest 100 2018/06/16

    A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...

  5. AtCoder Beginner Contest 052

    没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...

  6. AtCoder Beginner Contest 053 ABCD题

    A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...

  7. AtCoder Beginner Contest 136

    AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...

  8. AtCoder Beginner Contest 137 F

    AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...

  9. AtCoder Beginner Contest 076

    A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...

随机推荐

  1. $Android AlarmManager的用法详解

    在Android的Alarm机制中,使用AlarmManager可以实现类似闹钟这样的定时任务.在毕业设计项目中要实现定时任务的功能,所以在这里先进行一下梳理. (一)AlarmManager与Bro ...

  2. windows通过ftp下载linux文件

    # windows 下载 linux的文件>> ftp <domain_or_ip>>> <input_username>>> <in ...

  3. linux中安装php

    1.在php官网找到对应的php版本下载下来(php官网地址http://php.net) 2.把下载下来的安装包放入到linux系统中 3.解压php压缩包,通过tar -zxvf  +  下载下来 ...

  4. VoLTE的前世今生...说清楚VoIP、VoLTE、CSFB、VoWiFi、SIP、IMS那些事...

    转:https://mp.weixin.qq.com/s?__biz=MzA3MTA3OTIwMw==&mid=401344844&idx=1&sn=497b351f524af ...

  5. Python编程-函数进阶二

    一.生成器补充 1.什么是生成器? 可以理解为一种数据类型,这种数据类型自动实现了迭代器协议(其他的数据类型需要调用自己内置的__iter__方法),所以生成器就是可迭代对象. 2.生成器分类 (1) ...

  6. R中的参数传递函数:commandArgs(),getopt().

    1.commandArgs(),是R自带的参数传递函数,属于位置参数. ##test.R args=commandArgs(T) print (args[1])##第一个外部参数 print (arg ...

  7. INSPIRED启示录 读书笔记 - 第41章 产品经理的反省清单

    十大问题 1.产品能吸引目标消费者的关注吗? 2.产品的设计是否人性化,是否易于操作? 3.产品能在竞争中取胜吗?即使是面对未来风云变化的市场,依旧有取胜的把握吗? 4.我了解目标用户吗?产品(不是理 ...

  8. 剑指Offer——反转链表

    Question 输入一个链表,反转链表后,输出链表的所有元素. Solution 如果空间复杂度要求为O(1)的话,可以考虑用三个指针来进行反转 如果没有空间复杂度限制的话,可以考虑用一个栈,将节点 ...

  9. VC 模拟CMD 匿名管道

    #include "stdafx.h" #include <Windows.h> #include <stdio.h> #include <stdli ...

  10. CSDN_帖子__WapPc

    CSDN的帖子,Wap版 和 PC版 网址对应关系: 1. 举个例子: 手机版的网址: http://bbs.csdn.net/wap/topics/391891319 PC版的网址: http:// ...