HDU 4003 HawkandChicken (Tarjan )

Hawk-and-Chicken

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1259    Accepted Submission(s): 360


Problem Description
Kids in kindergarten enjoy playing a game called Hawk-and-Chicken. But there always exists a big problem: every kid in this game want to play the role of Hawk. 
So the teacher came up with an idea: Vote. Every child have some nice handkerchiefs, and if he/she think someone is suitable for the role of Hawk, he/she gives a handkerchief to this kid, which means this kid who is given the handkerchief win the support. Note the support can be transmitted. Kids who get the most supports win in the vote and able to play the role of Hawk.(A note:if A can win
support from B(A != B) A can win only one support from B in any case the number of the supports transmitted from B to A are many. And A can't win the support from himself in any case.
If two or more kids own the same number of support from others, we treat all of them as winner.
Here's a sample: 3 kids A, B and C, A gives a handkerchief to B, B gives a handkerchief to C, so C wins 2 supports and he is choosen to be the Hawk.
 
Input
There are several test cases. First is a integer T(T <= 50), means the number of test cases.
Each test case start with two integer n, m in a line (2 <= n <= 5000, 0 <m <= 30000). n means there are n children(numbered from 0 to n - 1). Each of the following m lines contains two integers A and B(A != B) denoting that the child numbered A give a handkerchief to B.
 
Output
For each test case, the output should first contain one line with "Case x:", here x means the case number start from 1. Followed by one number which is the totalsupports the winner(s) get. 
Then follow a line contain all the Hawks' number. The numbers must be listed in increasing order and separated by single spaces.
 
Sample Input
2 4 3 3 2 2 0 2 1 3 3 1 0 2 1 0 2
 
Sample Output
Case 1: 2 0 1 Case 2: 2 0 1 2
 
Author
Dragon
 
Source
 
Recommend
lcy
 

 解题思路:先把强连通分支压缩成一个点,再组成一个有向无环反向图, 对每个入度为0的点进行dfs, 找出最大的值即可。注意到,答案只能是在反向图入度为0 的点中。

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int VM=5010;
const int EM=30010;
const int INF=0x3f3f3f3f;

struct Edge{
    int to,nxt;
}edge[EM<<1],redge[EM<<1];

int n,m,cnt,head[VM],val[VM];
int dep,top,atype;
int dfn[VM],low[VM],vis[VM],stack[VM],belong[VM],indeg[VM],outdeg[VM];
int rcnt,rhead[VM];

void addedge(int cu,int cv){
    edge[cnt].to=cv;    edge[cnt].nxt=head[cu];     head[cu]=cnt++;
}

void raddedge(int cu,int cv){
    redge[rcnt].to=cv;   redge[rcnt].nxt=rhead[cu];   rhead[cu]=rcnt++;
}

void Tarjan(int u){
    dfn[u]=low[u]=++dep;
    stack[top++]=u;
    vis[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].nxt){
        int v=edge[i].to;
        if(!dfn[v]){
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }else if(vis[v]){
            low[u]=min(low[u],dfn[v]);
        }
    }
    int j;
    if(dfn[u]==low[u]){
        atype++;
        do{
            j=stack[--top];
            belong[j]=atype;
            vis[j]=0;
        }while(u!=j);
    }
}

void init(){
    cnt=0;
    memset(head,-1,sizeof(head));
    dep=0,  top=0,  atype=0;
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(vis,0,sizeof(vis));
    memset(belong,0,sizeof(belong));
    memset(indeg,0,sizeof(indeg));
    memset(outdeg,0,sizeof(outdeg));
}

int sup[VM],tmp;

void DFS(int u){
    vis[u]=1;
    tmp+=val[u];
    for(int i=rhead[u];i!=-1;i=redge[i].nxt){
        int v=redge[i].to;
        if(!vis[v])
            DFS(v);
    }
}

int main(){

    //freopen("input.txt","r",stdin);

    int t,cases=0;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        init();
        int u,v;
        while(m--){
            scanf("%d%d",&u,&v);
            addedge(u,v);
        }
        for(int i=0;i<n;i++)
            if(!dfn[i])
                Tarjan(i);
        memset(val,0,sizeof(val));
        for(int i=0;i<n;i++)
            val[belong[i]]++;

        rcnt=0;
        memset(rhead,-1,sizeof(rhead));
        for(int u=0;u<n;u++)
            for(int i=head[u];i!=-1;i=edge[i].nxt){
                int v=edge[i].to;
                if(belong[v]!=belong[u]){
                    indeg[belong[u]]++;
                    raddedge(belong[v],belong[u]);
                }
            }

        memset(sup,0,sizeof(sup));
        int ans=0;
        for(int i=1;i<=atype;i++){
            if(indeg[i]==0){
                memset(vis,0,sizeof(vis));
                tmp=0;
                DFS(i);
                sup[i]=tmp-1;
                ans=max(ans,sup[i]);
            }
        }

        printf("Case %d: %d\n",++cases,ans);
        int flag=0;
        for(int i=0;i<n;i++)
            if(sup[belong[i]]==ans){
                if(!flag)
                    flag=1;
                else
                    printf(" ");
                printf("%d",i);
            }
        printf("\n");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jackge/p/3137984.html