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 are N vertices numbered 1,2,...,N.
For each i, there are an edge from vertex Li to vertex Ri with weight Di, and an edge from vertex
Ri to vertex Li with weight −Di.
The problem asks whether we can assign an integer xv to each vertex v in G, such that for each edgefrom u to v with cost d, xv − xu = d holds. (Clearly, we can ignore the condition 0 ≤ xi ≤ 109.)
We handle each connected component in G independently. For each connected component, we choosean arbitary vertex v and assume that xv = 0. By running a dfs from v, we can uniquly determine thevalues of xi in this component. After that, we should check if the conditions are actually satisfied.
思路是建图+DFS
建图:对于L,R,D,L->D设置长度为D,R->L设置长度为-D
DFS:遍历每个联通块,对于一个点,若它被访问过,利用dis判断是否合法,若未被访问过,标记并dfs
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int n,m;
int L,R,D;
vector<pair<int,int> >mp[100100];
int dis[100100];
bool vis[100100];
int flag;
void dfs(int u,int pre)
{
if(!flag) return;
int sz=mp[u].size();
pair<int,int>tmp;
for(int i=0;i<sz;++i)
{
tmp=mp[u][i];
if(vis[tmp.first])
{
if(dis[u]+tmp.second!=dis[tmp.first])
{
flag=0;return;
}
}
else
{
vis[tmp.first]=1;
dis[tmp.first]=dis[u]+tmp.second;
dfs(tmp.first,u);
}
}
}
int main(int argc, char const *argv[])
{
/* code */
scanf("%d %d",&n,&m);
for(int i=1;i<=m;++i)
{
scanf("%d %d %d",&L,&R,&D);
mp[R].push_back(make_pair(L,D));
mp[L].push_back(make_pair(R,-D));
}
flag=1;
for(int i=1;i<=n;++i) if(!vis[i])
{
vis[i]=1;
dfs(i,-1);
}
if(flag) puts("Yes");else puts("No");
return 0;
}
AtCoder Beginner Contest 087 D People on a Line(DFS)的更多相关文章
- AtCoder Beginner Contest 087 D - People on a Line
Time limit : 2sec / Memory limit : 256MB Score : 400 points Problem Statement There are N people sta ...
- 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 ...
随机推荐
- hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- 九度OJ 1141:Financial Management (财务管理) (平均数)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:939 解决:489 题目描述: Larry graduated this year and finally has a job. He's ...
- 有返回值的Bookmark
首先代码创建Activity: public sealed class WaitForResponse<TResult>:NativeActivity<TResult> { p ...
- JAVA with Cassandra
maven项目,在pom.xml里加入依赖.不是的话下载相应的jar包放到lib目录下.这里驱动包的版本要和你cassandra的大版本一致. <dependency> <group ...
- MongoDB 倾向于将数据都放在一个 Collection 下吗?
不是这样的. Collection 的单个 doc 有大小上限,现在是 16MB,这就使得你不可能把所有东西都揉到一个 collection 里.而且如果 collection 结构过于复杂,既会影响 ...
- ffmpeg 编码h264 profile如何设置为baseline的问题
http://blog.csdn.net/kisaa133/article/details/7792008 使用最新版ffmpeg-0.11 libx264-125,使用默认编码时,用Eyecard发 ...
- codeforces 465C.No to Palindromes! 解题报告
题目链接:http://codeforces.com/problemset/problem/464/A 题目意思:给出一个长度为 n 且是 tolerable 的字符串s,需要求出字典序最小的长度也为 ...
- 使用XMLHttpRequest
请求种类 通过XMLHttpRequest的请求可以通过同步和异步的方式获取数据,请求的种类在XMLHttpRequest的open()方法的第三三个可选参数async设置.如果这个参数是true或者 ...
- 编程模式(schema) —— 表驱动法(table-driven)
使用表驱动法,而非繁琐冗长的 if/else, switch case(本身也代表一种代码坏味道),也是替身编程质量的重要手段, 表驱动法是一种编程模式(schema)-- 从表里面查找信息而不使用逻 ...
- 「LOJ#103」子串查找 (Hash
题目描述 这是一道模板题. 给定一个字符串 A A A 和一个字符串 B B B,求 B B B 在 A A A 中的出现次数.AAA 和 BBB 中的字符均为英语大写字母或小写字母. A A A 中 ...