hdu 3639 Hawk-and-Chicken 强连通分量 targin

Hawk-and-Chicken

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


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
2010 ACM-ICPC Multi-University Training Contest(19)——Host by HDU

--------
题目:每个点指向一些点,有传递性,求能够被指向的次数的最多的点。
用强连通分量缩点,建立反图。然后删掉重边(不然会T)。对每个入度为0的点进行搜索即可。


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>

using namespace std;
struct node{
    int v,next,u;
};
#define maxn 6000
#define maxm 50007
int head[maxn];
node edge[maxm];
int cnt = 0;
int belong[maxn];
int visited[maxn];
int low[maxn],dfn[maxn];
void addedge(int u,int v){
    edge[cnt].v = v;
    edge[cnt].u = u;
    edge[cnt].next= head[u];
    head[u] = cnt++;
}
vector<int> q;
int lossnum,lownum;
void tarjan(int u){
    dfn[u] = low[u] = lownum++;
    q.push_back(u);
    visited[u] = 1;
    int v;
    for(int i = head[u]; i != -1; i = edge[i].next){
        v = edge[i].v;
        if(visited[v] == 2) continue;
        if(visited[v] == 1){
            low[u] = min(low[u],dfn[v]);

        }
        if(visited[v] == 0)
        {
            tarjan(v);
            low[u] = min(low[u],low[v]);
        }
    }
    if(dfn[u] == low[u]){
        do{
            v = q[q.size()-1];
            q.pop_back();
            visited[v] = 2;
            belong[v] = lossnum;
        }while(v != u);
        lossnum++;
    }
}
int in[maxn];
int size[maxn];
vector<int>head1[maxn];
int check[maxn];
int gcnt;
int dfs(int u){
    if(check[u] == gcnt) return 0;
    check[u] = gcnt;
    int ans = size[u];
    for(int i = 0;i < head1[u].size(); i++)
        ans += dfs(head1[u][i]);
    return ans;
}

int main(){
    int n,m,t,tt=1;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        memset(head,-1,sizeof(head));
        cnt = 0;
        int u,v;
        lossnum = lownum = 0;
        for(int i = 0;i < m; i++){
            scanf("%d%d",&u,&v);
            u++,v++;
            addedge(u,v);
        }
        memset(visited,0,sizeof(visited));
        for(int i = 1;i <= n; i++){
            q.clear();
            if(visited[i] == 0){
                tarjan(i);
                //cout<<endl;
            }
        }
        memset(in,0,sizeof(in));
        memset(size,0,sizeof(size));

        for(int i = 1;i <= n; i++){
            size[belong[i]]++;
        }
        for(int i = 0;i < lossnum; i++)
            head1[i].clear();
        for(int i = 0;i < m; i++){
            u = edge[i].u;
            v = edge[i].v;
            if(belong[u] != belong[v]){
                in[belong[u]]++;
                head1[belong[v]].push_back(belong[u]);
            }
        }
//        for(int i = 1;i <= n; i++){
//            cout<<belong[i]<<" ";cout<<endl;
//        }
        for(int i = 0;i < lossnum; i++){
            sort(head1[i].begin(),head1[i].end());
            int l = unique(head1[i].begin(),head1[i].end())-head1[i].begin();
            while(head1[i].size() > l) head1[i].pop_back();
        }


        int ans = 0;
        gcnt = 10;
        memset(check,0,sizeof(check));
        //for(int i = 1;i <= n; i++)
        for(int i = 0;i < lossnum; i++){
            if(in[i] == 0){
                size[i] = dfs(i);
                ans = max(ans,size[i]);
            }
            gcnt++;
        }
        printf("Case %d: %d\n",tt++,ans-1);
        int flag = 0;
        for(int i = 1;i <= n; i++){
            if(size[belong[i]] == ans){
                if(flag) printf(" ");
                flag = 1;
                printf("%d",i-1);
            }
        }
        printf("\n");
    }
    return 0;
}
/*
222
4 3
3 2
2 0
2 1

3 3
1 0
2 1
0 2

4 6
0 1
1 2
2 3
3 2
3 1
2 1

3 3
2 0
1 2
1 0
*/




你可能感兴趣的:(HDU,3639,targin)