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. springboot2 webflux 响应式编程学习路径

    springboot2 已经发布,其中最亮眼的非webflux响应式编程莫属了!响应式的weblfux可以支持高吞吐量,意味着使用相同的资源可以处理更加多的请求,毫无疑问将会成为未来技术的趋势,是必学 ...

  2. 1-bit and 2-bit Characters

    We have two special characters. The first character can be represented by one bit 0. The second char ...

  3. java——内部类

    内部类:将一个类定义在另一个类的里面,对里面那个类就称为内部类.内部类的访问特点: 1,内部类可以直接访问外部类的成员. 2,外部类要访问内部类,必须建立在内部类的对象.什么时候用? 一般用于类的设计 ...

  4. LOVO学习之思维导图和文档编辑器

    思维导图——是一种图示笔记方法,一种图示笔记工具,一个思考的利器.能将放射性思考具体化,帮助人们理解和记忆事物. 思维导图绘制规则:1,在纸的正中央用一个彩色图像或者符号开始画思维导图. 2,把所有主 ...

  5. Day11 数据库的基本语法(偏重于查询)

    数据库的介绍: 老师博客: MYSQL-1 - Yuan先生 - 博客园 http://www.cnblogs.com/yuanchenqi/articles/7269675.html 作业地址: h ...

  6. Django push: Using Server-Sent Events and WebSocket with Django

    http://curella.org/blog/2012/jul/17/django-push-using-server-sent-events-and-websocket/ The goal of ...

  7. OSG嵌入QT(QT界面使用Qt Designer编辑)

    本文主要内容:使用Qt Designer编辑好QT界面后,将OSG中的ViewerWidget嵌入到QT的Widget中. 在VS中嵌入QT工具,建立QT GUIApplication后,打开自动生成 ...

  8. ES入门笔一

    ES6一共有6种声明变量的方法 --ES5只有var 和 function --ES6新增了let.const.import和class四种 ES6新增let和const,用来声明变量,是对var的扩 ...

  9. hadoop is running beyond virtual memory limits问题解决

    单机搭建了2.6.5的伪分布式集群,写了一个tf-idf计算程序,分词用的是结巴分词,使用standalone模式运行没有任何问题,切换到伪分布式模式运行一直报错: hadoop is running ...

  10. Java基础:JVM垃圾回收算法

    众所周知,Java的垃圾回收是不需要程序员去手动操控的,而是由JVM去完成.本文介绍JVM进行垃圾回收的各种算法. 1. 如何确定某个对象是垃圾 1.1. 引用计数法 1.2. 可达性分析 2. 典型 ...