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

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?

Input

The first line contains a single integer T, the number of test cases. And followed T cases.

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

The output should contain T lines. Write 'Yes' if the cave has the property stated above, or 'No' otherwise.

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?【强连通分量缩点+拓扑排序】的更多相关文章

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

  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. OpenGL ES andoid学习————2

    package com.xhm.getaccount; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.F ...

  2. final和finally面试时最好的回答

    finally: try块必须和catch块或和finally同在,不能单独存在,二者必须出现一个. finally块总会执行,不论是否有错误出现.但是若try语句块或会执行的catch语句块使用了J ...

  3. jqGrid单元格编辑配置,事件及方法

    转自 http://blog.csdn.net/xueshijun666/article/details/18151055 // var ret = $("#in_store_list_de ...

  4. python学习【第四篇】python函数 (一)

    一.函数的介绍 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也可以 ...

  5. restful demo 演示; jquery min1.1;

    [说明]上午建立了一个restful风格的一个测试,运行通过:下午试了试postman,想看看http请求的具体过程,但是chrome浏览器的network面板也可以查看,并且很方便,就索性用它了 一 ...

  6. tsinsen A1333. 矩阵乘法(梁 盾)

    A1333. 矩阵乘法(梁 盾) 时间限制:2.0s   内存限制:256.0MB   总提交次数:515   AC次数:211   平均分:54.14   将本题分享到:        查看未格式化 ...

  7. 【BZOJ2870】最长道路tree 点分治+树状数组

    [BZOJ2870]最长道路tree Description H城很大,有N个路口(从1到N编号),路口之间有N-1边,使得任意两个路口都能互相到达,这些道路的长度我们视作一样.每个路口都有很多车辆来 ...

  8. 【BZOJ3158】千钧一发 最小割

    [BZOJ3158]千钧一发 Description Input 第一行一个正整数N. 第二行共包括N个正整数,第 个正整数表示Ai. 第三行共包括N个正整数,第 个正整数表示Bi. Output 共 ...

  9. [转]C#静态方法与非静态方法的比较

    http://wenku.baidu.com/view/4e1704084a7302768e9939e0.html C#的类中可以包含两种方法:C#静态方法与非静态方法.那么他们的定义有什么不同呢?他 ...

  10. CSV导出

    CSV 导入导出工具类 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; impor ...