POJ 2762推断单个联通(支撑点甚至通缩+拓扑排序)


Going from u to v or from v to u?

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14789   Accepted: 3915

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

Source

POJ Monthly--2006.02.26,zgl & twb

[Submit]   [Go Back]   [Status]  
[Discuss]

题意:对于随意两个节点u,v。假设能从u到v或者v到u,那么输出Yes,否则输出No。

思路:先强连通缩点,此时一定不含环,看能不能找到一条最长路包括全部的缩点即可(实际上是找推断单链),用拓扑排序就ok,只是假设仅仅有一个强连通分量那么一定是Yes啦,否则topo排序推断 ,代码例如以下:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn =1e4+10;
const ll mod=20140413;
vector<int>G1[maxn],G2[maxn],G[maxn];
int sccno[maxn],vis[maxn],scc_cnt;//sccno强连通编号,scc_cnt表示强连通分量的个数
int ind[maxn];//ind表示入度
void init_G(int n)//初始化缩点图
{
	memset(ind,0,sizeof ind);
	for(int i=1;i<=n;i++)G[sccno[i]].clear();
	for(int i=1;i<=n;i++) {
		for(int j=0;j<G1[i].size();++j){
			int &v =G1[i][j];
			if(sccno[i]!=sccno[v]) {
				G[sccno[i]].push_back(sccno[v]);
				ind[sccno[v]]++;
			}
		}
	}
}
bool toposort(int n)//topo排序
{
	init_G(n);
	int u,cnt=0;
	queue<int>q;
	for(int i=1;i<=n;i++){
		if(!ind[sccno[i]]){
			if(!q.empty())return false;
			q.push(sccno[i]);
			cnt++;
		}
	}
	while(!q.empty()){
		u=q.front();
		q.pop();
		ind[u]=-1;
		for(int i=0;i<G[u].size();i++) {
			int &v=G[u][i];
			ind[v]--;
			if(ind[v]==0){
				if(!q.empty())return false;
				q.push(v);
				cnt++;
			}
		}
	}
	return cnt==scc_cnt;
}
vector<int>S;
void init(int n)
{
	memset(vis,0,sizeof vis);
	memset(sccno,0,sizeof sccno);
	S.clear();
	scc_cnt=0;
	for(int i=1;i<=n;i++) {
		G1[i].clear();
		G2[i].clear();
		G[i].clear();
	}
}
void AddEdge(int u,int v)
{
	G1[u].push_back(v);
	G2[v].push_back(u);
}
void dfs1(int u)
{
	if(vis[u])return ;
	vis[u]=1;
	for(int i=0;i<G1[u].size();i++)dfs1(G1[u][i]);
	S.push_back(u);
}
void dfs2(int u)
{
	if(sccno[u]) return ;
	sccno[u]=scc_cnt;
	for(int i=0;i<G2[u].size();++i)dfs2(G2[u][i]);
}
bool is_Semiconnect(int n)///计算强连通分量,初步推断
{
	for(int i=1;i<=n;i++)dfs1(i);
	for(int i=S.size();i>=1;i--){
		if(!sccno[S[i-1]]){
			scc_cnt++;
			dfs2(S[i-1]);
		}
	}
	return scc_cnt<=1;
}
int main()
{
   int T,n,m;
   scanf("%d",&T);
   while(T--) {
   		scanf("%d%d",&n,&m);
   		int u,v;
   		init(n);
   		while(m--) {
   			scanf("%d%d",&u,&v);
   			AddEdge(u,v);
   		}
   		if(is_Semiconnect(n))puts("Yes");
   		else{
   			if(toposort(n))puts("Yes");
   			else puts("No");
   		}
   	}
   return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

时间: 2024-10-02 02:05:27

POJ 2762推断单个联通(支撑点甚至通缩+拓扑排序)的相关文章

POJ 2762判断单联通(强连通缩点+拓扑排序)

Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14789   Accepted: 3915 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

POJ 1094 Sorting It All Out【floyd传递闭包+拓扑排序】

Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31994 Accepted: 11117 Description An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from small

poj 2762 Going from u to v or from v to u? (判断是否是弱联通图)

题意:给定一个有向图有m条单向边,判断是否任意两点都可达(a能到b或者b能到a或者互相可达),即求 弱联通分量. 算法: 先缩点求强连通分量.然后重新建图,判断新图是否是一条单链,即不能分叉,如果分叉了就会存在不可达的情况. 怎么判断是否是单链呢? 就是每次入度为0的点都只有一个,即每次队列里只有一个点. (    o(╯□╰)o.....好像已经是第二次用pair记录原图的点对,然后存pair的vector忘记清空导致wa来wa去! ) #include<cstdio> #include&l

POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)

职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可能从一点到还有一点的. 代码例如以下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include

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 题意:为了让他们的儿子变得更勇敢些,Jiajia 和Wind 将他们带到一个大洞穴中.洞穴中有n 个房间,有一些单向的通道连接某些房间.每次,Wind 选择两个房间x 和y,要求他们的一个儿子从一个房间走到另一个房间,这个儿子可以从x 走到y,也可以从y 走到x.Wind 保证她布置的任务是可以完成的,但她确实不知道如何判断一个任务是否可以完成

POJ 2762 Going from u to v or from v to u? (图论-tarjan)

Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14469   Accepted: 3801 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

poj 2762 Going from u to v or from v to u?

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17689   Accepted: 4745 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,

[强连通分量] 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: 17089   Accepted: 4590 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

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