Cactus 【 仙人掌图 tarjan 求scc 】

Cactus
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).

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.

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.没有横向边;
2.图中没有桥; // 即 图是强联通的
3.每个点的反向边和low[v]值比dfn[u]小的子节点v的数量和小于2;

用tarjan 求的时候 就是 不能够让 now点指向其下方的已经在stack中的点

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#define CLR(a,b) memset((a),(b),sizeof(a))
#define inf 0x3f3f3f3f
#define mod 100009
#define LL long long
#define M  50000+4100
#define ll o<<1
#define rr o<<1|1
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
using namespace std;
void read(int &x){
    x=0;char c;
    while((c=getchar())<'0');
    do x=x*10+c-'0';while((c=getchar())>='0');
}
struct Edge {
    int from,to,next;
}edge[M];
int head[M],top;
int low[M],dfn[M];
int sccno[M],in[M];
int dfs_clock,scc_cnt;
vector<int>scc[M],G[M];
stack<int>S;int Instack[M];
int n,m;int In[M],Out[M];
int flag;
void addedge(int a,int b)
{
    Edge e={a,b,head[a]};
    edge[top]=e; head[a]=top++;
}
void init()
{
    memset(head,-1,sizeof(head));
    top=0;
    while(!S.empty()) S.pop();
}
void getmap()
{
    int a,b;
    while(scanf("%d%d",&a,&b)&&(a||b))
    addedge(a,b);
}
void tarjan(int now)
{
    int enxts;
    dfn[now]=low[now]=++dfs_clock;
    S.push(now);Instack[now]=1;
    for(int i=head[now];i!=-1;i=edge[i].next)
    {
        Edge e=edge[i];
        if(!dfn[e.to])
        {
            tarjan(e.to);
            low[now]=min(low[now],low[e.to]);
        }
        else if(Instack[e.to]) 
        {
            low[now]=min(low[now],dfn[e.to]);

            if(low[e.to] != dfn[e.to]) flag = 1;  //  这个就是表明  当前这个点指向的点是在其下方的(并且其还在 栈内 ),只有在其下方且在栈内的 点,他的low才是已经修改过了,不等于 dfn值 
        }
    }

    if(low[now]==dfn[now])
    {
        scc_cnt++;
        scc[scc_cnt].clear();
        while(1)
        {
            int nexts=S.top();S.pop();Instack[nexts]=0;
            sccno[nexts]=scc_cnt;
            scc[scc_cnt].push_back(nexts);
            if(now==nexts) break;
        }
    }
}

void find_cut(int le,int ri) 
{
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(sccno,0,sizeof(sccno));
    memset(Instack,0,sizeof(Instack));
    dfs_clock=scc_cnt=0;
    for(int i=0;i<n;i++)
    if(!dfn[i]) tarjan(i);
}
void solve()
{
    if(scc_cnt==1&&!flag)  printf("YES\n");
    else printf("NO\n");
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        flag=0;
        init();
        getmap();
        find_cut(0,n-1);
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值