Cactus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 887 Accepted Submission(s): 410
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.
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
题意:判断一个有向图是否为仙人掌图。
仙人掌图满足三个性质:
性质 1: 仙人掌图的DFS 树没有横向边。
性质 2: Low(v)<=DFN(u) (v 是u 的儿子)
性质 3:设某个点u有a(u)个儿子的Low 值小于DFN(u),同时u自己有b(u)条逆向边。那么a(u)+b(u)<2。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <map>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define ll long long
using namespace std;
const int maxn=20005;
const int maxm=50005;
const int INF=1000000000;
struct node
{
int v,next;
}edge[maxm];
int head[maxn],low[maxn],dfn[maxn];
bool ins[maxn],vis[maxn];
int n,num,cnt;
void init()
{
memset(head,-1,sizeof(head));
memset(dfn,0,sizeof(dfn));
memset(ins,false,sizeof(ins));
memset(vis,false,sizeof(vis));
num=cnt=0;
}
void add(int u,int v)
{
edge[num].v=v;
edge[num].next=head[u];
head[u]=num++;
}
void input()
{
int a,b;
scanf("%d",&n);
while(scanf("%d%d",&a,&b),a||b)
add(a,b);
}
bool dfs(int u)
{
low[u]=dfn[u]=++cnt;
ins[u]=true;
int sum=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(vis[v]) return false; //若该点被遍历过,则存在横向边,不满足性质1
if(!dfn[v])
{
if(!dfs(v)) return false;
if(low[v]>dfn[u]) return false; //low[v]>dfn[u]说明不是强连通图,不满足性质2
if(low[v]<dfn[u]) sum++; //累加a(u)
if(sum==2) return false; //sum必须<2,根据性质3
low[u]=min(low[v],low[u]);
}
else if(ins[v])
{
low[u]=min(low[u],dfn[v]);
sum++; //累加b(u)
if(sum==2) return false; //sum必须<2,根据性质3
}
}
vis[u]=true;
return true;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
input();
bool flag=dfs(0);
if(flag)
{
for(int i=0;i<n;i++) //判断图是否是连通的
if(!dfn[i]) {flag=false; break;}
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}