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为边 ...
随机推荐
- Lattice Diamond 学习之编译、检查和设置约束
在新建工程以及完成代码的输入之后.则就要进行编译,并检测错误. 一. Generate Hierarchy(产生层次结构). 1. 点击Generate Hierarchy 图标或者Design -- ...
- Redis笔记(七)Java实现Redis消息队列
这里我使用Redis的发布.订阅功能实现简单的消息队列,基本的命令有publish.subscribe等. 在Jedis中,有对应的java方法,但是只能发布字符串消息.为了传输对象,需要将对象进行序 ...
- Delphi基本数据类型---枚举、子界、集合、数组
参考:http://blog.csdn.net/qustdong/article/details/9230743 参考:http://www.cnblogs.com/xumenger/p/440222 ...
- 回溯法解决N皇后问题(以四皇后为例)
以4皇后为例,其他的N皇后问题以此类推.所谓4皇后问题就是求解如何在4×4的棋盘上无冲突的摆放4个皇后棋子.在国际象棋中,皇后的移动方式为横竖交叉的,因此在任意一个皇后所在位置的水平.竖直.以及45度 ...
- 2014百度之星资格赛 1001:Energy Conversion(水题,逻辑题)
Energy Conversion Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 关于strcpy_s
#include"stdafx.h" #include<iostream> #include<cstring> int main() { using nam ...
- Matlab tips and tricks
matlab tips and tricks and ... page overview: I created this page as a vectorization helper but it g ...
- PHP 部署发布接口
环境前提:电脑已安装wamp ① 在\wamp\bin\apache\apache2.4.9\conf\httpd.conf文件中, 修改 DocumentRoot 为当前项目路径,例如 Docume ...
- PHP中include和require(转)
昨天去面试一个php开发,看到笔试试卷上有这么一道题目: include和require有什么区别? 这个题目可以称得上php开发面试中的必考题目,网上也有各种答案和解释.但是我当时却真的想不起来了. ...
- 【转】Struts2解决表单重复提交问题
用户重复提交表单在某些场合将会造成非常严重的后果.例如,在使用信用卡进行在线支付的时候,如果服务器的响应速度太慢,用户有可能会多次点击提交按钮,而这可能导致那张信用卡上的金额被消费了多次.因此,重复提 ...