Cactus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 913 Accepted Submission(s): 426
Problem Description
1. It is a Strongly Connected graph.
2. Each edge of the graph belongs to a circle and only belongs to one circle.
We call this graph as CACTUS.
There is an example as the figure above. The left one is a cactus, but the right one isn’t. Because the edge (0, 1) in the right graph belongs to two circles as (0, 1, 3) and (0, 1, 2, 3).
2. Each edge of the graph belongs to a circle and only belongs to one circle.
We call this graph as CACTUS.
![](https://i-blog.csdnimg.cn/blog_migrate/e1478ce74822ed5bdd93e951dc63e84d.jpeg)
There is an example as the figure above. The left one is a cactus, but the right one isn’t. Because the edge (0, 1) in the right graph belongs to two circles as (0, 1, 3) and (0, 1, 2, 3).
Input
The input consists of several test cases. The first line contains an integer T (1<=T<=10), representing the number of test cases.
For each case, the first line contains a integer n (1<=n<=20000), representing the number of points.
The following lines, each line has two numbers a and b, representing a single-way edge (a->b). Each case ends with (0 0).
Notice: The total number of edges does not exceed 50000.
For each case, the first line contains a integer n (1<=n<=20000), representing the number of points.
The following lines, each line has two numbers a and b, representing a single-way edge (a->b). Each case ends with (0 0).
Notice: The total number of edges does not exceed 50000.
Output
For each case, output a line contains “YES” or “NO”, representing whether this graph is a cactus or not.
Sample Input
2 4 0 1 1 2 2 0 2 3 3 2 0 0 4 0 1 1 2 2 3 3 0 1 3 0 0
Sample Output
YES NO
Author
alpc91
Source
Recommend
zhengfeng
题意是说给你一个有向图,此图如果具备这两个条件:1,它是一个强连通图,2它的每一条边仅属于一个环。就输出YES,否则输出NO。
只要对tarjan算法非常了解才能够理解这么做。这个题需要很好的理解dfn和low两个数组的作用,如果low[u]!=dfn[u],则说明已经存在环,如果我们访问u点时发现low[u]!=dfn[u],则说明有一条边在两个环上了。
#include<stdio.h>
#include<string.h>
#include<stack>
#define M 20007
using namespace std;
int head[M],low[M],dfn[M],vis[M],id[M];
stack<int>s;
int n,cnt,num,count,ok;
struct Edg
{
int to,next;
}edg[M*5];
void init()
{
cnt=num=count=ok=0;
memset(vis,0,sizeof(vis));
memset(dfn,0,sizeof(id));
memset(head,-1,sizeof(head));
}
void addedge(int a,int b)
{
edg[cnt].to=b;
edg[cnt].next=head[a];
head[a]=cnt++;
}
void tarjan(int u)
{
int v;
low[u]=dfn[u]=++num;
vis[u]=1;
s.push(u);
for(int i=head[u];i!=-1;i=edg[i].next)
{
v=edg[i].to;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[v],low[u]);
}
else if(vis[v])
{
low[u]=min(dfn[v],low[u]);
if(dfn[v]!=low[v])//说明一条边在两个环上
ok=1;
}
if(ok)return;
}
if(dfn[u]==low[u])
{
count++;
if(count>1)//如果count大于1就说明存在多个环
ok=1;
do
{
v=s.top();
s.pop();
vis[v]=0;
}while(s.top()!=u);
}
}
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
init();
int a,b;
while(scanf("%d%d",&a,&b),a|b)
{
a++;b++;
addedge(a,b);
}
for(int i=1;i<=n;i++)
if(!dfn[i])
tarjan(i);
if(ok||count!=1)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}