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 ...
随机推荐
- 整理的一些java中常使用jar包以及说明
slf4j:Simple Logging Facade for Java SLF4J,即简单日志门面(Simple Logging Facade for Java),不是具体的日志解决方案,它只服务于 ...
- 这样就能用MathType编辑^符号
大家都知道数学公式中的符号有很多,有些符号的名称还很多,比如,^这个字符,可以是乘方.插入符号.插入符.托字符等.所以一些用户在使用过程中有点搞不清,但是Mathtype的符号模板有很多种,基本可以满 ...
- web安全漏洞防护
Password type input with autocomplete enabled The autocomplete attribute works with the following &l ...
- [NSDate distantPast]使用
本文转载至 http://blog.sina.com.cn/s/blog_5f1967e00101ge0i.html 使用下面的方法: 关闭定时器不能使用invalidate方法,应该使用下面的方法 ...
- Eclipse 启动时闪退问题解决方案
一.以前Eclipse都可以正常使用,突然有一天不能启动了,点击图标后启动画面一闪之后就消失了,以下是一些解决方案: 1. 找到Eclipse目录下的eclipse.exe,右键点击->发送到桌 ...
- linux下软件的安装与卸载
一 软件安装包的类型 通常Linux应用软件的安装有五种: 1) tar+ gz包,如software-1.2.3-1.tar.gz.他是使用UNIX系统的打包工具tar打包的. 2) r ...
- 《从零开始学Swift》学习笔记(Day 53)——do-try-catch错误处理模式
原创文章,欢迎转载.转载请注明:关东升的博客 Swift 1.x的错误处理模式存在很多弊端,例如:为了在编程时候省事,给error参数传递一个nil,或者方法调用完成后不去判断error是否为nil, ...
- 清空javascript数组数据
var arrayObj = new Array(); arrayObj.splice(0, arrayObj.length);//清空数组数据
- nodejs post请求
const http = require('http'); const querystring = require('querystring'); const postData = querystri ...
- 004-ibus输入法,快捷键,浏览器
一.输入法 用 root 身份在终端下,运行下面命令: yum install ibus-pinyin ibus ibus-gtk ibus-qt 使用im-chooser命令,选择ibus为默认输入 ...