题目链接:http://poj.org/problem?id=2762

题意是 有t组样例,n个点m条有向边,取任意两个点u和v,问u能不能到v 或者v能不能到u,要是可以就输出Yes,否则输出No。注意一点,条件是或者!所以不是判断双连通图的问题。

我一开始没看到'or'这个条件,所以直接tarjan判断是否只有一个强连通分量,果断WA。

所以需要给原图缩点,用tarjan把图变成一个有向无环图,要是只有一个scc,那就直接输出Yes。那接下来讨论多个scc,要是新图中有两个及以上的点的入度为0,则这些点都不能相互到达,所以输出No,所以我们找到唯一一个入度为0的点作为root,然后从这个点来拓扑排序,出队入队的过程肯定是一进一出的,所以根据过程来判断是否输出Yes和No。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int MAXN = ;
struct data {
int next , to;
}edge[MAXN * ];
int head[MAXN] , st[MAXN] , low[MAXN] , dfn[MAXN] , block[MAXN] , du[MAXN];
int top , ord , sccnum , cont;
bool instack[MAXN];
vector <int> G[MAXN]; inline void add(int u , int v) {
edge[cont].next = head[u];
edge[cont].to = v;
head[u] = cont++;
} void init() {
memset(head , - , sizeof(head));
memset(dfn , , sizeof(dfn));
memset(du , , sizeof(du));
memset(instack , false , sizeof(instack));
top = sccnum = ord = cont = ;
} void tarjan(int u) {
low[u] = dfn[u] = ++ord;
st[++top] = u;
instack[u] = true;
for(int i = head[u] ; ~i ; i = edge[i].next) {
int v = edge[i].to;
if(!dfn[v]) {
tarjan(v);
low[u] = min(low[u] , low[v]);
}
else if(instack[v]) {
low[u] = min(low[u] , low[v]);
}
}
if(low[u] == dfn[u]) {
int v;
sccnum++;
do {
v = st[top--];
instack[v] = false;
block[v] = sccnum;
}while(u != v);
}
} void top_sort() {
queue <int> que;
while(!que.empty()) {
que.pop();
}
cont = ;
for(int i = ; i <= sccnum ; i++) {
if(!du[i]) {
que.push(i);
cont++;
}
}
if(cont > ) {
printf("No\n");
return ;
}
while(!que.empty()) {
int temp = que.front() , cnt = ;
que.pop();
for(int i = ; i < G[temp].size() ; i++) {
du[G[temp][i]]--;
if(!du[G[temp][i]]) {
cnt++;
cont++;
que.push(G[temp][i]);
}
}
if(cnt > ) {
printf("No\n");
return ;
}
}
if(cont != sccnum) {
printf("No\n");
}
else {
printf("Yes\n");
}
} int main()
{
int t , n , m , u , v;
scanf("%d" , &t);
while(t--) {
scanf("%d %d" , &n , &m);
init();
for(int i = ; i < m ; i++) {
scanf("%d %d" , &u , &v);
add(u , v);
}
for(int i = ; i <= n ; i++) {
G[i].clear();
if(!dfn[i])
tarjan(i);
}
if(sccnum == ) {
printf("Yes\n");
continue;
}
for(int u = ; u <= n ; u++) {
for(int i = head[u] ; ~i ; i = edge[i].next) {
int v = edge[i].to;
if(block[u] != block[v]) {
du[block[v]]++;
G[block[u]].push_back(block[v]);
}
}
}
top_sort();
}
}

POJ 2762 Going from u to v or from v to u? (强连通分量缩点+拓扑排序)的更多相关文章

  1. poj 2762 Going from u to v or from v to u?【强连通分量缩点+拓扑排序】

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

  2. POJ2762 Going from u to v or from v to u? 强连通分量缩点+拓扑排序

    题目链接:https://vjudge.net/contest/295959#problem/I 或者 http://poj.org/problem?id=2762 题意:输入多组样例,输入n个点和m ...

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

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

  4. 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:  ...

  5. 【强连通分量缩点】poj 1236 Network of Schools

    poj.org/problem?id=1236 [题意] 给定一个有向图,求: (1)至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点 (2)至少要加多少条边,才能使得从任何一个顶点出发,都 ...

  6. POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)

    Network of Schools A number of schools are connected to a computer network. Agreements have been dev ...

  7. poj 2762 强连通缩点+拓扑排序

    这题搞了好久,先是拓扑排序这里没想到,一开始自己傻乎乎的跑去找每层出度为1的点,然后才想到能用拓扑排序来弄. 拓扑排序的时候也弄了挺久的,拓扑排序用的也不多. 题意:给一个图求是否从对于任意两个点能从 ...

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

    [题意]: 有N个房间,M条有向边,问能否毫无顾虑的随机选两个点x, y,使从①x到达y,或者,②从y到达x,一定至少有一条成立.注意是或者,不是且. [思路]: 先考虑,x->y或者y-> ...

  9. 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 ...

随机推荐

  1. HDU 1754 I Hate It (线段树 单点更新)

    题目链接 中文题意,与上题类似. #include <iostream> #include <cstdio> #include <cstring> #include ...

  2. ASP.NET MVC 学习2、从Controller传递数据到View

      参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-view 一,Control ...

  3. UVALive 4452 The Ministers' Major Mess(2-sat)

    2-sat.又学到了一种使用的方法:当确定选择某中状态A时,从它的对立状态A^1引一条边add(A^1,A),从而使凡是dfs经过对立状态,必然return false:即保证若存在一种可能性,必然是 ...

  4. VS2010开发2dx无法解析的外部符号解决记录

    首先新建HelloWorld项目... 想使用Cocos2d扩展包需要引入相关头文件,如:#include “cocos-ext.h”...接下来我们右键工程属性->配置属性->c/c++ ...

  5. 【WEB小工具】BaseServlet—一个Servlet处理多个请求

    package cn.itcast.test.web.servlet; import java.io.IOException; import java.io.PrintWriter; import j ...

  6. Jabber/XMPP协议与架构

    一.概述 由Jeremie Miller于1998年开始这个项目.Jabber是一个开放源码形式组织产生的网络实时通信协议,第一个公开版本于2000年5月发行.Jabber已经由IETF XMPP协议 ...

  7. 130道ASP.NET面试题

    转:http://boke.25k5.com/kan88569.html 1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . privat ...

  8. linux下网络排错与查看

    基本的故障排除错误 故障的排除一定是先简单后复杂的,有的人把上述的文件反复配置,就是上不了网,一直都认为是系统出了故障,想重装机子.结果发现原来是网线压根就没插上. 排错要慢慢的按部就班的来: (1) ...

  9. FTP文件上传与下载

    实现FTP文件上传与下载可以通过以下两种种方式实现(不知道还有没有其他方式),分别为:1.通过JDK自带的API实现:2.通过Apache提供的API是实现. 第一种方式:使用jdk中的ftpClie ...

  10. iOS 7 自定义Back按钮 与 Pop interactive gesture 问题

    1.自定义Back按钮 iOS中很多时候我们都会自定义返回按钮,也是一件easy的事,类似如下: // 返回按钮 1 - (void)showNavBackButton { UIButton *bac ...