poj 2762 Going from u to v or from v to u?【强连通分量缩点+拓扑排序】
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 15812 | Accepted: 4194 |
Description
Input
The first line for each case contains two integers n, m(0 < n
< 1001,m < 6000), the number of rooms and corridors in the cave.
The next m lines each contains two integers u and v, indicating that
there is a corridor connecting room u and room v directly.
Output
Sample Input
1
3 3
1 2
2 3
3 1
Sample Output
Yes
题目大意:n个节点 m条单向边。构成一个图,问你是否可以保证任选2个点u、v,这两个点之间是连通的?(能从x到达y,或者能从y到达x既可,不一定是x与y是互通
的) 保证则输出:Yes 否则:No 【强连通缩点, 到最后我们只要进行一次拓扑排序,看看是否存在一条一次可以走完的路径既可】
强连通分量算法将图化简缩点后,进行拓扑排序。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <stack>
#include <algorithm>
#define N 1002 using namespace std; struct node
{
int e, val;
}item;
vector<node>g[N];
stack<int>st; int dfn[N], low[N];
int id[N]; //标记i节点属于哪个连通分量
int mat2[N][N];
int n2;
int counter, n, scnt; void Tarjan(int e)
{
int t, i;
int mm=low[e]=dfn[e]=counter++;
st.push(e);//入栈
for(i=0; i<g[e].size(); i++){
t=g[e][i].e;
if(dfn[t]==-1)
Tarjan(t);
if(low[t]<mm)
mm=low[t];
}
if(mm<low[e]){//有回边
low[e]=mm; return;
}
do{
id[t=st.top()]=scnt; low[t]=n;
st.pop();
}while(t!=e);
scnt++;
} void Search(int n)
{
int i;
memset(dfn, -1, sizeof(dfn));
counter=0; scnt=0;
for(i=0; i<n; i++)
if(dfn[i]==-1)
Tarjan(i);
} void base_vertex()
{
int i, j, t;
Search(n); //调用求强连通分量
n2=scnt;
memset(mat2,0,sizeof(mat2));
for(i=0; i<n; i++){
for(j=0; j<g[i].size(); j++){
t=g[i][j].e; mat2[id[i]][id[t]]=1;
}
}
} int topo_sort(int n, int mat[][N], int *topo)
{//拓扑排序的n是强连通分量的个数
int d[N], i, j, k;
for(i=0; i<n; i++){
d[i]=0;
for(j=0; j<n; j++)
d[i]=d[i]+mat[j][i];//入度数
}
for(k=0; k<n; k++){
for(i=0; d[i]&&i<n; i++);
if(i==n) return 0;
d[i]=-1;
for(j=0; j<n; j++)
d[j]-=mat[i][j];
topo[k]=i;
}
return 1;
}
int main()
{
int tg; scanf("%d", &tg);
int m, u, v;
int i, j;
int topo[N];
while(tg--)
{
scanf("%d %d", &n, &m);
for(i=0; i<=n; i++)
g[i].clear();
for(i=0; i<m; i++){
scanf("%d %d", &u, &v);
item.e=v-1; item.val=1;
g[u-1].push_back(item);
}//建立单向图
base_vertex();
topo_sort(n2, mat2, topo);
int flag=1;
for(i=0; i<n2-1; i++){
if(!mat2[topo[i]][topo[i+1]] ){
flag=0; break;
}
}
if(flag) printf("Yes\n");
else printf("No\n");
}
return 0;
} /*
下面的数据会访问冲突崩溃掉 但poj可以Accepted 不懂~~~
1
8 11
1 2
2 3
2 5
2 6
3 5
4 3
5 2
5 4
6 7
6 8
7 6 */
poj 2762 Going from u to v or from v to u?【强连通分量缩点+拓扑排序】的更多相关文章
- 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. ...
- 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 ...
- POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)
这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...
- 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: ...
- 【强连通分量缩点】poj 1236 Network of Schools
poj.org/problem?id=1236 [题意] 给定一个有向图,求: (1)至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点 (2)至少要加多少条边,才能使得从任何一个顶点出发,都 ...
- POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)
Network of Schools A number of schools are connected to a computer network. Agreements have been dev ...
- poj 2762 强连通缩点+拓扑排序
这题搞了好久,先是拓扑排序这里没想到,一开始自己傻乎乎的跑去找每层出度为1的点,然后才想到能用拓扑排序来弄. 拓扑排序的时候也弄了挺久的,拓扑排序用的也不多. 题意:给一个图求是否从对于任意两个点能从 ...
- POJ 2762Going from u to v or from v to u?(强联通 + 缩点 + 拓扑排序)
[题意]: 有N个房间,M条有向边,问能否毫无顾虑的随机选两个点x, y,使从①x到达y,或者,②从y到达x,一定至少有一条成立.注意是或者,不是且. [思路]: 先考虑,x->y或者y-> ...
- 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 ...
随机推荐
- 使用PHP函数输出前一天的时间和后一天的时间
1.明确date()函数和time()函数的功能,其中time()函数是获取时间戳函数 2.输出前一天的当前时间: echo '一天之前的时间为:'.date('Y-m-d H:i:s',time() ...
- DB facade实现CURD
数据表 CREATE TABLE IF NOT EXISTS students( `id` INT AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(255) NO ...
- Laragon集成开发环境+配置Xdebug+postman运行Xdebug
[ Laravel 5.5 文档 ] 快速入门 —— 使用 Laragon 在 Windows 中搭建 Laravel 开发环境:http://laravelacademy.org/post/7754 ...
- LINQ TO SQL 实现无限递归查询
from:http://blog.csdn.net/q107770540/article/list 见论坛内有网友提问类似的问题已经不止一次了, 现总结一下,希望能给以后再碰到此类问题的朋友一些帮助 ...
- 使用bbed编辑研究oracle数据块结构
bbed是随oracle软件公布的一款数据块查看和编辑工具,作为一款内部工具.bbed的功能很强大,可是假设使用不当可能给数据库造成无法挽回的损失.因此.我们建议在使用bbed改动数据块前备份被改动的 ...
- python正则表达式练习
正则表达式修饰符 - 可选标志 正则表达式可以包含一些可选标志修饰符来控制匹配的模式.修饰符被指定为一个可选的标志.多个标志可以通过按位 OR(|) 它们来指定.如 re.I | re.M 被设置成 ...
- unison+inotify 同步web代码并排除指定目录不同步
unison + inotify 实现web 数据双向同步 unison 是一款跨平台的文件同步对象,不仅支撑本地对本地同步,也支持通过SSH,RSH和Socket 等网络协议进行同步.unis ...
- 【BZOJ1880】[Sdoi2009]Elaxia的路线 最短路+DP
[BZOJ1880][Sdoi2009]Elaxia的路线 Description 最近,Elaxia和w**的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们 必须合理地安排两个人在一起 ...
- Delphi里的Windows消息(可查MSDN指定位置)
各种控件的通知消码和控制消息可由MSDN-> Platform SDK-> User Interface Services->Windows User Interface->C ...
- JS添加标签
<script> function show(){ $('.add').unbind(); $('.low ...