Description

In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors connecting some rooms. Each time, Wind choose two rooms x and y, and ask one of their little sons go from one to the other. The son can either go from x to y, or from y to x. Wind promised that her tasks are all possible, but she actually doesn't know how to decide if a task is possible. To make her life easier, Jiajia decided to choose a cave in which every pair of rooms is a possible task. Given a cave, can you tell Jiajia whether Wind can randomly choose two rooms without worrying about anything?
 

Solution

缩点,满足题意的充要条件是存在一条完整的链,这个可以用“是否存在唯一拓扑序”解决。

Code

 #include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=1e5+; int pre[maxn],low[maxn],clock;
int scc[maxn],s[maxn],c,cnt;
int head[maxn],e[maxn],nxt[maxn],k;
void adde(int u,int v){
e[++k]=v;nxt[k]=head[u];head[u]=k;
}
int _head[maxn],_e[maxn],_nxt[maxn],_k;
void _adde(int u,int v){
_e[++_k]=v;_nxt[_k]=_head[u];_head[u]=_k;
}
int n,m; int r[maxn],vis[maxn];
int topo(){
for(int i=;i<=cnt;i++){
int tot=,u;
for(int i=;i<=cnt;i++)//偷懒
if(!r[i]&&!vis[i]) tot++,u=i;
if(tot>) return ;
vis[u]=;
for(int i=_head[u];i;i=_nxt[i])
r[_e[i]]--;
}
return ;
} void dfs(int u){
pre[u]=low[u]=++clock;
s[++c]=u;
for(int i=head[u];i;i=nxt[i]){
int v=e[i];
if(!pre[v]){
dfs(v);
low[u]=min(low[u],low[v]);
}
else if(!scc[v]){
low[u]=min(low[u],pre[v]);
}
}
if(low[u]==pre[u]){
cnt++;
while(c){
scc[s[c]]=cnt;
if(s[c--]==u) break;
}
}
} void clear(){
memset(head,,sizeof(head));
memset(_head,,sizeof(_head));
memset(pre,,sizeof(pre));
memset(low,,sizeof(low));
memset(vis,,sizeof(vis));
memset(r,,sizeof(r));
c=cnt=k=_k=;
} int main(){
int T;
scanf("%d",&T);
while(T--){
clear();
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
adde(u,v);
}
for(int i=;i<=n;i++)
if(!pre[i]) dfs(i); for(int i=;i<=n;i++)
for(int j=head[i];j;j=nxt[j]){
int u=scc[i],v=scc[e[j]];
if(u==v) continue;
else _adde(u,v),r[v]++;
} if(topo()) printf("Yes\n");
else printf("No\n");
}
return ;
}

【缩点+拓扑判链】POJ2762 Going from u to v or from v to u?的更多相关文章

  1. POJ2762 单向连通图(缩点+拓扑排序

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19552 ...

  2. POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)

    这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...

  3. [poj2762] Going from u to v or from v to u?(Kosaraju缩点+拓排)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud     Going from u to v or from v to u? Tim ...

  4. 2018.11.06 bzoj1093: [ZJOI2007]最大半连通子图(缩点+拓扑排序)

    传送门 先将原图缩点,缩掉之后的点权就是连通块大小. 然后用拓扑排序统计最长链数就行了. 自己yyyyyy了一下一个好一点的统计方法. 把所有缩了之后的点都连向一个虚点. 然后再跑拓扑,这样最后虚点的 ...

  5. hdu 1811(缩点+拓扑排序+并查集)

    Rank of Tetris Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. POJ2762 Going from u to v or from v to u(单连通 缩点)

    判断图是否单连通,先用强连通分图处理,再拓扑排序,需注意: 符合要求的不一定是链拓扑排序列结果唯一,即在队列中的元素始终只有一个 #include<cstdio> #include< ...

  7. Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序

         Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K       Description I ...

  8. POJ 2762 Going from u to v or from v to u? (强连通分量缩点+拓扑排序)

    题目链接:http://poj.org/problem?id=2762 题意是 有t组样例,n个点m条有向边,取任意两个点u和v,问u能不能到v 或者v能不能到u,要是可以就输出Yes,否则输出No. ...

  9. poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)

    http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit:  ...

随机推荐

  1. (function(){xxx})(); 写法解释

    常见格式:(function() { /* code */ })(); 解释:包围函数(function(){})的第一对括号向脚本返回未命名的函数,随后一对空括号立即执行返回的未命名函数,括号内为匿 ...

  2. JVM的运行原理以及JDK 7增加的新特性(一)

    虚拟机(Virtual Machine) JRE是由Java API和JVM组成的.JVM的主要作用是通过Class Loader来加载Java程序,并且按照Java API来执行加载的程序. 虚拟机 ...

  3. 【图文详解】HDFS基本原理

    本文主要详述了HDFS的组成结构,客户端上传下载的过程,以及HDFS的高可用和联邦HDFS等内容.若有不当之处还请留言指出. 当数据集大小超过一台独立的物理计算机的存储能力时,就有必要对它进行分区,并 ...

  4. 关闭ipv6的方法

    公司研发反应,几台机器开了一些端口,但是访问一直不通. 检查后发现,发现服务开启的是ipv6的端口,所有首先想到的办法就是关闭ipv6. 关闭ipv6的方法有两种: 第一个是在内核配置文件修改配置(p ...

  5. CPA、CPS、CPM、CPT、CPC 是什么

    http://www.a-edm.com/cpa.html 网络营销之所以越来越受到重视一个主要的原因就是因为“精准”.相比较传统媒体的陈旧广告形式,网络营销能为广告主带来更为确切的效果与回报,更有传 ...

  6. Ocelot中文文档-Raft(实验功能不能用于生产环境)

    Ocelot最近整合了Rafty,这是我在去年一直研究的Raft的一个实现. 这个项目实验性非常强,所以在我认为它没问题之前,请不要在生产环境中使用Ocelot的这个功能. Raft是一种分布式一致性 ...

  7. JS——函数

    一.函数的种类 1.无参函数 function showName() { alert("我是无参函数"); } 2.有参函数 在函数中的参数为"形式参数" 形式 ...

  8. Robot Framework自动化_Selenium2Library 关键字

    Robot Framework自动化_Selenium2Library 关键字 培训老师:肖能尤 2016/06/12 课程目的 一.Robot framework Selenium2Library ...

  9. JavaScript 之函数

    刚开 始学习 JS 时,挺不习惯它函数的用法,就比如一个 function 里面会嵌套一个 function,对于函数里创建变量的作用域也感到很迷惑,这个的语法和 JAVA 相差太多,为此,阅读了&l ...

  10. crontab定时任务, 定时到秒

    crontab定时任务最小支持到minute, 定时到秒执行的任务, 写起来比较麻烦 * * * * * /bin/sh /home/jiewang/portraitProc/test.sh > ...