题目大意:

  有一个有向无环图,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 题解的更多相关文章

  1. Codeforces Round #556 题解

    Codeforces Round #556 题解 Div.2 A Stock Arbitraging 傻逼题 Div.2 B Tiling Challenge 傻逼题 Div.1 A Prefix S ...

  2. Codeforces Round #569 题解

    Codeforces Round #569 题解 CF1179A Valeriy and Deque 有一个双端队列,每次取队首两个值,将较小值移动到队尾,较大值位置不变.多组询问求第\(m\)次操作 ...

  3. Codeforces Round #557 题解【更完了】

    Codeforces Round #557 题解 掉分快乐 CF1161A Hide and Seek Alice和Bob在玩捉♂迷♂藏,有\(n\)个格子,Bob会检查\(k\)次,第\(i\)次检 ...

  4. CFEducational Codeforces Round 66题解报告

    CFEducational Codeforces Round 66题解报告 感觉丧失了唯一一次能在CF上超过wqy的机会QAQ A 不管 B 不能直接累计乘法打\(tag\),要直接跳 C 考虑二分第 ...

  5. codeforces CF475 ABC 题解

    Bayan 2015 Contest Warm Up http://codeforces.com/contest/475 A - Bayan Bus B - Strongly Connected Ci ...

  6. Codeforces Round #542 题解

    Codeforces Round #542 abstract I决策中的独立性, II联通块染色板子 IIIVoronoi diagram O(N^2 logN) VI环上距离分类讨论加取模,最值中的 ...

  7. Codeforces 576D Flights for Regular Customers (图论、矩阵乘法、Bitset)

    题目链接 http://codeforces.com/contest/576/problem/D 题解 把边按\(t_i\)从小到大排序后枚举\(i\), 求出按前\((i-1)\)条边走\(t_i\ ...

  8. Codeforces Choosing Laptop 题解

    这题实在是太水了,具体看注释 蒟蒻的方法是一边找过时的电脑一边比大小 蒟蒻不才,只会C++ 其实还会free basic,但它已经过时了 附: 本题洛谷网址 Codeforces网址 希望蒟蒻的题解能 ...

  9. CF 241E flights 最短路,重复迭代直到稳定 难度:3

    http://codeforces.com/problemset/problem/241/E 首先检测哪些点会出现在从起点到终点的路上,可以用dfs或者迭代, 然后,对于所有的边,设f为边起点,t为边 ...

随机推荐

  1. Java -- 找不到或无法加载主类

    原文:http://wenku.baidu.com/link?url=5nS1GEaePn-hmtAg6xXdJvtt9Z89JQsakhqSv8fambaJY2t9nKPtf3hXFpjW-BtD9 ...

  2. Git+VirtalBaox+Vagrant创建Linux虚拟机

    文章内容来自Udacity课程:Linux Command Line Basics--Getting Started with the Shell Your own Linux box To lear ...

  3. 数据结构和算法 – 3.堆栈和队列

    1.栈的实现   后进先出     自己实现栈的代码 using System; using System.Collections.Generic; using System.Linq; using ...

  4. java创建线程的几种方式

    1.继承Thread类 /** * @author Ash * @date: 2016年8月6日 下午10:56:45 * @func: 通过继承Thread类来实现多线程 * @email 4086 ...

  5. SSIS 包单元测试检查列表

    1. 使用脚本任务(Script tasks) 组建的时候,在日志里增加一些调试信息,例如变量更新信息,可以帮助我们从日志中查看到变量是在何时何地更新的. 2. 使用ForceExecutionRes ...

  6. 网站通用登录模块代码 分类: ASP.NET 2014-12-06 10:49 615人阅读 评论(0) 收藏

    1.HTML部分:     <form id="form1" runat="server">     <script src=".. ...

  7. Jquery获取iframe子/父窗口中的标签

    获取子窗口中的标签: $("#id",document.frames('iframename').document); 获取父窗口中的标签: $('#id', parent.doc ...

  8. winows下使用ssh服务远程登录vbox中的虚拟机

    1.到http://www.putty.org/下载并安装SSH客户端 2.查看是否安装ssh服务 在ubuntu终端命令界面键入: #ssh localhost 如果出现下面提示则表示还没有安装: ...

  9. Java Thread join() 的用法

    Java Thread中, join() 方法主要是让调用改方法的thread完成run方法里面的东西后, 在执行join()方法后面的代码.示例: class ThreadTesterA imple ...

  10. 利用Visual GDB在Visual Studio中进行Android开发

    转载请注明http://www.cnblogs.com/adong7639/p/4119467.html 无意中发现了Visual GDB这个工具,可以再Visual Studio中进行Android ...