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 ...
随机推荐
- PHP中的面向对象 中的类(class)
2.11 上午讲的是面向对象中的类(class),一个非常抽象的概念, 类里面成员的定义有 public$abc; private$abc(私有变量): protect $abc(受保护的变量): 下 ...
- EasyPlayer安卓Android流媒体播放器实现直播过程中客户端快照功能
本文转自:http://blog.csdn.net/jyt0551/article/details/56942795 对于一个裸的RTSP URL,存放在播放列表上略显单调与枯燥.大家可以看到Easy ...
- SPDY: An experimental protocol for a faster web HTTP/2
http://www.chromium.org/spdy/spdy-whitepaper https://en.wikipedia.org/wiki/SPDY Internet protocol su ...
- 解决shell脚本“syntax error near unexpected token `fi'”的问题。
执行shell脚本的时候,提示如下错误: 查询资料后发现: 执行: vi finddir.sh 然后,输入 :set ff 结果是: 解决方案就是,修改为unix: :set ff=unix 执行保存 ...
- java创建文件夹以及文件
java在创建文件的过程中如果改文件的路径不存在: 会出现下面这种情况 java.io.IOException: 系统找不到指定的路径. at java.io.WinNTFileSystem.crea ...
- appium(13)- server config
//本文讲解:启动server时,如何配置capabilities 和 flag.可以将不同client端需要的通用的capabilities都放到server端配置. Requirements In ...
- HZNU 2154 ldh发奖金【字符串】
题目链接 http://acm.hznu.edu.cn/OJ/problem.php?id=2154 思路 先判断不能拆分的情况 以为需要拆分成两个正整数 所以我们可以知道 只有个位的数字 是不能够拆 ...
- ansible 魔法变量
hostvars 可以让你调用其他host的变量和facts, 即使你没有在这个机器上执行过playbook, 你仍然可以访问变量, 但是不能访问facts. 例如: {{ hostvars['te ...
- 编辑xml文件时不能自动提示问题的解决
在编辑xml文件时,eclipse总是不能自动提示,在网上找了一些资料,大部分都是说关于xml editor配置的,下面也把这个方法罗列在下面,以供参考: 解决办法:在eclipse的菜单里,找到wi ...
- BluetoothLELibrary 支持1对1连接
github地址:https://github.com/qindachang/BluetoothLELibrary 该库只支持1对1连接,如果你想1对多设备连接,请移步至 BluetoothLE-Mu ...