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 ...
随机推荐
- 红外图像盲元补偿matlab实现源码与效果验证
在国内红外公司绝大多数一直以来国外进口的成像芯片,能够进行红外芯片自助开发的电学应该只有大立光电和广微积电光学方法只有上海巨哥和一直未能产 品化的昆山光微电子.由于政治和历史原因,欧美对中国大陆还是实 ...
- array_sum的用法
众所周知,PHP中函数是功能很强大的,那么今天就说下array_sum的功能吧. 函数功能:返回数组中所有值的和. 举例: <?php $a = array(1,2); $b = array_s ...
- dos下查找进程,如果找到echo find并结束该进程
@echo offset var=chromedriver.exetasklist | findstr "%var%" && echo findtaskkill / ...
- redis string底层数据结构sds
redis的string没有采用c语言的字符串数组而采用自定义的数据结构SDS(simple dynamic string)设计 len 为字符串的实际长度 在redis中获取字符串的key长度的时 ...
- Android无线测试之—Genymotion配置过程中常见问题
一.前提条件: 已经部署好了Android UiAutomator测试环境. 二.在部署Genymotion时遇到了两类问题: 1.通过eclipse打开一个模拟设备,然后将编译好的jar包push到 ...
- 深入C#学习系列一:序列化(Serialize)、反序列化(Deserialize)(转)
序列化又称串行化,是.NET运行时环境用来支持用户定义类型的流化的机制.其目的是以某种存储形成使自定义对象持久化,或者将这种对象从一个地方传输到另一个地方. .NET框架提供了两种串行化的方式: ...
- mongodb超时
1 connection timeout 设置连接mongo数据库的超时时间,如果在该时间内未连接成功,那么就是超时了. 2 socket timeout 设置一次操作的超时时间,比如一次查询,如果在 ...
- 【原创】学习CGLIB动态代理中遇到的问题
代码清单1 CGLIB动态代理 package wulj.proxy.cglibProxy; import java.lang.reflect.Method; import net.sf.cglib. ...
- BigDecimal使用整理
BigDecimal使用整理 一. BigDecimal简介 计算机计算中无论是float还是double都是浮点数,由于计算机是二进制的,导致在在浮点数计算时会出现精度丢失,因此引入BigD ...
- h5 localStorage本地存储
用户名:<input type="text" id="txtname"/> 密码:<input type="text" i ...