poj 2762

9 篇文章 0 订阅
5 篇文章 0 订阅
#include <iostream>
#include <cstdio>
#include <cstring>
#define N 1010
#define M 6060
using namespace std;
struct Node
{
    int v,next;
}edge[M], edge2[M];
int head[N], head2[N], SCC, num, top;
int low[N], dfn[N], sta[N], sn[N], id[N];
bool mark[N];

void add_edge(Node edge[], int head[], int u, int v,int &num )
{
    edge[num].v = v;
    edge[num].next = head[u];
    head[u] = num++;
}

void get_map( int n, int m )
{
    int num = 0, u, v;
    for( int i = 0; i <= n; i ++ )
        head[i] = -1;
    while( m -- )
    {
        scanf("%d%d",&u,&v);
        add_edge( edge,head,u,v,num);
    }
}

void dfs( int u )
{
    low[u] = dfn[u] = ++ num;
    mark[u] = true;
    sta[top++] = u;
    for( int p = head[u]; p!=-1; p = edge[p].next )
    {
        int v = edge[p].v;
        if( !dfn[ v ])
        {
            dfs(v);
            low[u] = min(low[u],low[v]);
        }
        else if( mark[v] && dfn[v] < low[u] ) low[u] = dfn[v];
    }
    if( low[u] == dfn[u] )
    {
        SCC++;
        do
        {
            u = sta[--top];
            mark[u] = false;
            sn[u] = SCC;
        }
        while( low[u] != dfn[u] );
    }
}

bool bfs( int s )
{
    int front = 0, rear = 0, sum = 0;
    low[rear++] = s;
    while( front!=rear )
    {
        int u = low[front++];
        sum++;
        s = -1;
        for( int p = head2[u]; p!=-1; p = edge2[p].next ) if( --id[edge2[p].v] == 0 )
        {
            if( s != -1 ) return false;
            else s = edge2[p].v;
        }
        if( s != -1 ) low[rear++] = s;
    }
    if( sum!=SCC ) return false;
    else return true;
}
bool tarjan( int n )
{
    SCC = num = 0;
    for( int i = 0; i <= n; i ++ )
    {
        mark[i] = false;
        dfn[i] = low[i] = id[i] = sn[i] = 0;
        head2[i] = -1;
    }
    for( int i = 1; i <= n; i ++ )
        if( !dfn[i] ) dfs(i);
    if( SCC<=1 ) return true;
    num = 0;
    for( int u = 1; u <= n; u ++ )
    for( int p = head[u]; p!=-1; p = edge[p].next )
    {
        int v = edge[p].v;
        if( sn[u] != sn[v] )
        {
            id[ sn[v] ]++;
            add_edge(edge2,head2,sn[u],sn[v], num );
        }
    }
    int s = -1;
    for( int i = 1; i <= SCC; i ++ )
        if( id[i] == 0 )
            if( s == -1 ) s = i;
            else return false;
    if( bfs(s) )  return true;
    else return false;
}

int main()
{
    int cas, m, n;
    scanf("%d",&cas);
    while( cas -- )
    {
        scanf("%d%d",&n,&m);
        get_map(n,m);
        if( tarjan(n) ) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值