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 ...
随机推荐
- duplicate symbols for architeture arm64 linker command failed with code 1(use-c to see invocation)
duplicate symbols for architeture arm64 linker command failed with code 1(use-c to see invocation) ...
- apktool + eclipse 动态调试APK
用了会AndBug,尽管挺强大的可是作为习惯了OD.EDB作为动态调试工具的人,自然有些不习惯,于是乎寻求新的动态调试解决方式.但大多数都是NetBeans + apktool.想着还得多下一个IDE ...
- 【BZOJ4668】冷战 并查集
[BZOJ4668]冷战 Description 1946 年 3 月 5 日,英国前首相温斯顿·丘吉尔在美国富尔顿发表“铁幕演说”,正式拉开了冷战序幕. 美国和苏联同为世界上的“超级大国”,为了争夺 ...
- EasyDarwin幼教云视频平台在幼教平台领域大放异彩!
在之前的一篇方案<基于EasyDarwin云视频平台的幼儿园视频直播(手机直播/微信直播)解决方案>中,我们提到一种可以广泛应用于幼教.工厂.建筑工地以及各种现场监控的云视频平台方案,这种 ...
- superslider网站特效插件
网站上常用的“焦点图/幻灯片”“Tab标签切换”“图片滚动”“无缝滚动” 如何使用 1.引入jquery.js 引入superslider.js 2.编写HTML 以下是默认的HTMl结构,分别 ...
- Eclipse中连接Sql Sever2008 -----转自Yogurshine
Eclipse中连接Sql Sever2008 -----转自Yogurshine 一 SQl Sever服务器配置 1我之前已经安装好SQL Sever 2008R2.(注意:安装一遍未成功时,一定 ...
- java之冒泡排序
//冒泡排序(Bubble Sorting)的基本思想是:通过对待排序序列从后向前(从下标较大的元素开始),依次比较相邻元素的排序码,若发现逆序则交换,使排序码较小的元素逐渐从后部移向前部(从下标较大 ...
- eclipse集成SVN插件-----复制添加插件
首先从网上下载一个svn的插件包, 将插件包解压, 打开eclipse的文件夹, 将svn插件中features文件夹中的jar复制到eclipse中features文件夹中去: 将svn插件中plu ...
- dialog更新数据
将数据显示在最上面
- Xcode各个版本及模拟器下载
如果你嫌在 App Store 下载 Xcode 太慢,你也可以选择从网络上下载: Xcode下载(Beta版打的包是不能提交到App Store上的) 绝对官方源!!!绝对官方源!!!绝对官方源!! ...