HDU3639 强连通

Hawk-and-Chicken

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


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 total supports 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
题意:
投票,每个人可以把自己的得到的票和自己的那一票投给其他人,投完之后自己的票数不会减少,不能投给自己,问最后最高的票数是几票,哪几个人得到最高票数。
代码:
//同一个联通分量里面每个点得到得票数是一样的。可以将每个联通分量缩成一个点,这若干个联通分量中一定会有
//出度为0的,此联通分量中所有的点就是要求的点,票数就是自身的票数加上与他相连的联通分量中的点数,建立
//反向图,也就是反向图中入度为零的点。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int maxn=5003;
int t,n,m,a,b,sum;
int pre[maxn],sccno[maxn],lowlink[maxn],dfs_clock,scc_cnt,in[maxn],vis[maxn],sccnu[maxn],ans[maxn];
//in[i]记录编号为i的联通分量的入度,sccnu[i]记录编号为i的联通分量中有几个点。
vector<int>g[maxn],g2[maxn];
stack<int>s;
void dfs(int u)
{
    pre[u]=lowlink[u]=++dfs_clock;
    s.push(u);
    for(int i=0;i<(int)g[u].size();i++){
        int v=g[u][i];
        if(!pre[v]){
            dfs(v);
            lowlink[u]=min(lowlink[u],lowlink[v]);
        }
        else if(!sccno[v])
            lowlink[u]=min(lowlink[u],pre[v]);
    }
    if(lowlink[u]==pre[u]){
        scc_cnt++;
        for(;;){
            int x=s.top();
            s.pop();
            sccno[x]=scc_cnt;
            sccnu[scc_cnt]++;
            if(x==u) break;
        }
    }
}
void find_scc()
{
    dfs_clock=scc_cnt=0;
    memset(pre,0,sizeof(pre));
    memset(sccno,0,sizeof(sccno));
    memset(sccnu,0,sizeof(sccnu));
    for(int i=0;i<n;i++){
        if(!pre[i]) dfs(i);
    }
}
void dfs2(int x)//求出到达该联通分量的点数
{
    vis[x]=1;
    sum+=sccnu[x];
    for(int i=0;i<(int)g2[x].size();i++){
        if(!vis[g2[x][i]]) dfs2(g2[x][i]);
    }
}
int main()
{
    scanf("%d",&t);
    for(int h=1;h<=t;h++){
        scanf("%d%d",&n,&m);
        for(int i=0;i<=n;i++){
            g[i].clear();
            g2[i].clear();
        }
        while(!s.empty()) s.pop();
        for(int i=0;i<m;i++){
            scanf("%d%d",&a,&b);
            g[a].push_back(b);
        }
        find_scc();
        memset(in,0,sizeof(in));
        for(int i=0;i<n;i++){   //把每个联通分量看做点,重新建反向图
            for(int j=0;j<(int)g[i].size();j++){
                if(sccno[i]!=sccno[g[i][j]]){
                    g2[sccno[g[i][j]]].push_back(sccno[i]);
                    in[sccno[i]]++;
                }
            }
        }
        int MAX=0;
        memset(ans,0,sizeof(ans));
        for(int i=1;i<=scc_cnt;i++){
            if(in[i]==0){
                sum=0;
                memset(vis,0,sizeof(vis));
                dfs2(i);
                ans[i]=sum;
                MAX=max(sum,MAX);
            }
        }
        printf("Case %d: %d
",h,MAX-1);//去掉自己的那一票
        int flag=0;
        for(int i=0;i<n;i++){
            if(ans[sccno[i]]==MAX){
                if(flag) printf(" %d",i);
                else {printf("%d",i);flag=1;}
            }
        }
        printf("
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6343847.html