题意

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

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

  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. mnesia练习及基本操作

    Mnesia基本用法 查看表结构 查看mnesia表的结构: mnesia:info(). 查看此表的基本信息: mnesia:table_info(<tableName>, all). ...

  2. return和exit的差别

    #include<stdio.h> #include<sys/types.h> #include<sys/wait.h> #include<unistd.h& ...

  3. 开源安卓Android流媒体音视频播放器实现声音自动停止、恢复、一键静音功能源码

    本文转自EasyDarwin团队John的博客:http://blog.csdn.net/jyt0551/article/details/60802145 我们在开发安卓Android流媒体音视频播放 ...

  4. 九度OJ 1110:小白鼠排队 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1734 解决:1054 题目描述: N只小白鼠(1 <= N <= 100),每只鼠头上戴着一顶有颜色的帽子.现在称出每只白鼠的 ...

  5. Win10更新后,IE和Edge以外的浏览器打开网页速度慢的解决方案

    下载修复工具,提取码:you0 以管理员身份运行修复工具,点击“修复” 点击“确定” 提示“修复成功” 参考链接:Win10下极速模式无法打开网页的解决办法_360社区

  6. Rime输入工具的修改与编译安装

    作为一个比较“事儿多”的五笔用户,在使用过几乎所有的主流输入工具后,我最终选择了定制性非常高(同时也比较难以上手)的Rime.刚开始是在Windows下使用小狼毫0.9.30版,这个工具在上屏速度.热 ...

  7. mtk6589显示子系统笔记(一)

    拿到MT6589的版本不久,发现显示系统代码结构改变很大.做些备忘,后续不忙的时候可以继续看. MT6589之前的MTK的Android系统显示系统同featurePhone基本一致. 先来回顾下MT ...

  8. Gym - 100676F Palindrome —— 并查集

    题目链接:https://vjudge.net/contest/155789#problem/E 题解: 由于是回文串,所以可以先将在对称位置的字符放在同一个集合(如果期间有两个非‘?’,且不相等,则 ...

  9. Consul环境搭建

    大家在玩的时候 一定要使用ningx 1.9以上版本啊! 下载:wget https://releases.hashicorp.com/consul/0.7.5/consul_0.7.5_linux_ ...

  10. the art of seo(chapter six)

    Developing an SEO-Friendly Website ***Making Your Site Accessible to Search Engines***1.Indexable Co ...