您的位置:首页 > 编程语言 > Go语言

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

2015-07-22 20:47 861 查看
u到v或者v到u

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

简单来说,这是一个有向图的弱连通问题…

注意,是有向图!

学习了图的强连通后,不难发现,一个样有向图的强连通是有向环,对于环上的每一个点(room),他们都可以互相到达。

如果我们找到图中的强连通分量,将他们缩点,再连边,会形成一个“压缩到极致的”无环有向图(不想无向图,缩点连边后是一棵树。实际上是因为有向图有横叉边)

再仔细想想,如果此时的新图中,有两个或以上入度为0的点,那么这两个点肯定不能互相到达。

如果只有一个入度为0的点,以这个点为root,DFS遍历整个图,如果对于某个结点,他有两个或以上的儿子,那么这两棵或以上的子树上的点,就不能互相到达。说了这么多其实就是能形成一条单链。。。

那么,就来实现一下。

代码:

#include<cstdio>
#include<stack>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 1000
#define MAXM 6000
int t,n,m;
struct node{
int v;
node *next;
}edge[MAXM+10],*adj[MAXN+10],*ecnt=&edge[0];
int dfn[MAXN+10],low[MAXN+10],dcnt,block_cnt,bccno[MAXN+10],in[MAXN+10],out[MAXN+10];
stack<int> point;
bool instack[MAXN+10],mat[MAXN+10][MAXN+10],vis[MAXN+10];
void reset()
{
memset(edge,0,sizeof(edge)); memset(adj,0,sizeof(adj)); ecnt=&edge[0];
memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(bccno,0,sizeof(bccno));
memset(in,0,sizeof(in)); memset(out,0,sizeof(out));
memset(instack,0,sizeof(instack)); memset(mat,0,sizeof(mat)); memset(vis,0,sizeof(vis));
dcnt=block_cnt=0;
}
void addedge(int u,int v)
{
node *p=++ecnt;
p->v=v;
p->next=adj[u];
adj[u]=p;
}
void read()
{
int x,y;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d%d",&x,&y);
addedge(x,y);
mat[x][y]=true;
}
}
void dfs_find_SCC(int u,int fa)
{
point.push(u);
instack[u]=true;
dfn[u]=low[u]=++dcnt;
for(node *p=adj[u];p;p=p->next){
int v=p->v;
if(!dfn[v]){
dfs_find_SCC(v,u);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u]){
++block_cnt;
int uu;
do{
uu=point.top(); point.pop();
bccno[uu]=block_cnt;
instack[uu]=false;
}while(uu!=u);
}
}
void build_new_tree()
{
memset(edge,0,sizeof(edge));
memset(adj,0,sizeof(adj));
ecnt=&edge[0];
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
if(j==i) continue;
if(mat[i][j]&&bccno[i]!=bccno[j]){
addedge(bccno[i],bccno[j]);
out[bccno[i]]++,in[bccno[j]]++;
}
}
}
bool dfs_Judge(int u)
{
vis[u]=true;
int cnt=0;
for(node *p=adj[u];p;p=p->next){
int v=p->v;
if(!vis[v]){
cnt++;
bool f=dfs_Judge(v);
if(!f) return false;
}
}
if(cnt>=2) return false;
return true;
}
bool solve()
{
//最多只能有一个入度为0的点,如果有多个入度为0的点,则这两个连通分量肯定是不连通的
int incnt=0,rt=-1;
for(int i=1;i<=block_cnt;i++)
if(!in[i]) incnt++,rt=i;
if(incnt>1) return false;
if(dfs_Judge(rt)) return true;
else return false;
}

int main()
{
scanf("%d",&t);
for(int i=1;i<=t;i++){
reset();
read();
for(int i=1;i<=n;i++)
if(!dfn[i])
dfs_find_SCC(i,-1); //strongly connected componenet:强连通
build_new_tree();  //缩点,建树
bool f=solve();
if(f) printf("Yes\n");
else printf("No\n");
}
}


不大满意的地方:reset写得比较丑。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: