CodeForces - 241E Flights 题解
题目大意:
有一个有向无环图,n个点m条边,所有边权为1或2,求一组使所有从1到n的路径长度相同的边权的方案。
思路:
设从1到i的最短路为dist[i],若有一条从x到y的边,则1<=dist[y]-dist[x]<=2,即dist[y]-dist[x]>=1且dist[x]-dist[y]>=-2,有了这个约束条件,就可以跑差分约束了。不过跑之前要先把从1到n的路径上的点找出来,否则会使无用的点对结果产生影响。
代码:
#include<queue>
#include<vector>
#include<cstdio>
using namespace std;
const int N=,M=;
int cnt,n,m,i,x,y,h,t,a[M],b[M],v[M<<],pre[N],last[M<<],w[M<<],head[N],dist[N],count[N];
bool vis[N],mark[N],flag[N];
vector <int> l[N],r[N]; int read()
{
int x=;
char ch=getchar();
while (ch<'' || ch>'') ch=getchar();
while (ch>='' && ch<='') x=(x<<)+(x<<)+ch-,ch=getchar();
return x;
} void add(int x,int y,int z)
{
v[++cnt]=y,last[cnt]=head[x],head[x]=cnt,w[cnt]=z;
} bool SPFA()
{
queue <int> q;
for (q.push(),vis[]=count[]=;!q.empty();)
for (x=q.front(),q.pop(),vis[x]=,i=head[x];i;i=last[i])
if (dist[y=v[i]]<w[i]+dist[x])
{
dist[y]=dist[x]+w[i];
if (!vis[y])
{
q.push(y),vis[y]=;
if (++count[y]>n) return ;
}
}
return ;
} void wk1(int x)
{
int i;
for (flag[x]=,i=;i<l[x].size();i++)
if (!flag[l[x][i]]) wk1(l[x][i]);
} void wk2(int x)
{
int i;
for (mark[x]=,i=;i<r[x].size();i++)
if (!mark[r[x][i]]) wk2(r[x][i]);
} int main()
{
for (n=read(),m=read(),i=;i<=m;i++) a[i]=read(),b[i]=read();
for (i=;i<=m;i++) l[a[i]].push_back(b[i]),r[b[i]].push_back(a[i]);
for (wk1(),wk2(n),i=;i<=n;i++) flag[i]&=mark[i];
for (i=;i<=m;i++)
if (flag[a[i]] && flag[b[i]]) add(a[i],b[i],),add(b[i],a[i],-);
if (SPFA()) puts("No");
else
for (puts("Yes"),i=;i<=m;i++)
if (flag[a[i]] && flag[b[i]]) printf("%d\n",dist[b[i]]-dist[a[i]]);
else puts("");
return ;
}
CodeForces - 241E Flights 题解的更多相关文章
- Codeforces Round #556 题解
Codeforces Round #556 题解 Div.2 A Stock Arbitraging 傻逼题 Div.2 B Tiling Challenge 傻逼题 Div.1 A Prefix S ...
- Codeforces Round #569 题解
Codeforces Round #569 题解 CF1179A Valeriy and Deque 有一个双端队列,每次取队首两个值,将较小值移动到队尾,较大值位置不变.多组询问求第\(m\)次操作 ...
- Codeforces Round #557 题解【更完了】
Codeforces Round #557 题解 掉分快乐 CF1161A Hide and Seek Alice和Bob在玩捉♂迷♂藏,有\(n\)个格子,Bob会检查\(k\)次,第\(i\)次检 ...
- CFEducational Codeforces Round 66题解报告
CFEducational Codeforces Round 66题解报告 感觉丧失了唯一一次能在CF上超过wqy的机会QAQ A 不管 B 不能直接累计乘法打\(tag\),要直接跳 C 考虑二分第 ...
- codeforces CF475 ABC 题解
Bayan 2015 Contest Warm Up http://codeforces.com/contest/475 A - Bayan Bus B - Strongly Connected Ci ...
- Codeforces Round #542 题解
Codeforces Round #542 abstract I决策中的独立性, II联通块染色板子 IIIVoronoi diagram O(N^2 logN) VI环上距离分类讨论加取模,最值中的 ...
- Codeforces 576D Flights for Regular Customers (图论、矩阵乘法、Bitset)
题目链接 http://codeforces.com/contest/576/problem/D 题解 把边按\(t_i\)从小到大排序后枚举\(i\), 求出按前\((i-1)\)条边走\(t_i\ ...
- Codeforces Choosing Laptop 题解
这题实在是太水了,具体看注释 蒟蒻的方法是一边找过时的电脑一边比大小 蒟蒻不才,只会C++ 其实还会free basic,但它已经过时了 附: 本题洛谷网址 Codeforces网址 希望蒟蒻的题解能 ...
- CF 241E flights 最短路,重复迭代直到稳定 难度:3
http://codeforces.com/problemset/problem/241/E 首先检测哪些点会出现在从起点到终点的路上,可以用dfs或者迭代, 然后,对于所有的边,设f为边起点,t为边 ...
随机推荐
- python中使用chrome进行自动化测试,浏览器变量设置
- java 缩略图
http://www.cnblogs.com/digdeep/p/4829471.html http://www.jb51.net/article/57648.htm http://blog.csdn ...
- 与你相遇好幸运,The Moe Node.js Code Style Guide
The Moe Node.js Code Style Guide By 一个最萌的开发者 @2016.9.21 >>代码是人来阅读的,格式规范的代码是对编程人员最好的礼物 :) > ...
- ytu 2558: 游起来吧!超妹!(水题,趣味数学题)
2558: 游起来吧!超妹! Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 7 Solved: 3[Submit][Status][Web Board ...
- go-martini 简单分析之一
env.go 环境变量 const ( Dev string = "development" Prod string = "production" Test s ...
- C# 重绘tabControl,添加关闭按钮(页签)
C# 重绘tabControl,添加关闭按钮(页签) 调用方法 参数: /// <summary> /// 初始化 /// </summary> /// <param n ...
- Sp EF输出 临时表
-- ============================================= -- Author: <Author,,Name> -- Create date: < ...
- 学习linux内核时常碰到的汇编指令(2)
转载:http://blog.sina.com.cn/s/blog_4be6adec01007xvh.html JNGE∶指令助记符——(有符号数比较)不大于且不等于转移(等价于JL).当SF和OF异 ...
- rsync+inotify实现实时同步案例--转
转自:http://chocolee.blog.51cto.com/8158455/1400596 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐 ...
- hdu 1024 Max Sum Plus Plus
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...