码迷,mamicode.com
首页 > 其他好文 > 详细

(tarjan/+DFS+反向建图) hdu 3639

时间:2015-04-18 01:07:33      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

G - Hawk-and-Chicken
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<set>
#include<map>
using namespace std;
vector<int> e[5005],mp[5005];
stack<int> s;
int n,m;
int Dfs[5005],use[5005],low[5005],dp[5005],in[5005],cnt[5005];
int isstack[5005],vis[5005];
int newflag,top;
void init()
{
    memset(Dfs,0,sizeof(Dfs));
    memset(use,0,sizeof(use));
    memset(low,0,sizeof(low));
    memset(dp,0,sizeof(dp));
    memset(in,0,sizeof(in));
    memset(isstack,0,sizeof(isstack));
    memset(cnt,-1,sizeof(cnt));
    newflag=0,top=0;
    while(!s.empty()) s.pop();
    for(int i=0;i<n;i++)
        e[i].clear(),mp[i].clear();
}
void tarjan(int u)
{
    Dfs[u]=low[u]=++top;
    isstack[u]=1;
    s.push(u);
    for(int i=0;i<e[u].size();i++)
    {
        int v=e[u][i];
        if(!Dfs[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(isstack[v])
        {
            low[u]=min(low[u],Dfs[v]);
        }
    }
    if(low[u]==Dfs[u])
    {
        newflag++;
        int x;
        do
        {
            x=s.top();
            s.pop();
            use[x]=newflag;
            isstack[x]=0;
            dp[newflag]++;
        }while(x!=u);
    }
}
int dfs(int x)
{
    int sum=dp[x];
    vis[x]=1;
    for(int i=0;i<mp[x].size();i++)
    {
        int v=mp[x][i];
        if(!vis[v]) sum+=dfs(v);
    }
    return sum;
}
int main()
{
    int tt,cas=1;
    scanf("%d",&tt);
    while(tt--)
    {
        init();
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            e[x].push_back(y);
        }
        for(int i=0;i<n;i++)
        {
            if(!Dfs[i])
                tarjan(i);
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<e[i].size();j++)
            {
                if(use[i]!=use[e[i][j]])
                {
                    mp[use[e[i][j]]].push_back(use[i]);
                    in[use[i]]++;
                }
            }
        }
        int ans=0;
        for(int i=1;i<=newflag;i++)
        {
            if(in[i]==0)
            {
                memset(vis,0,sizeof(vis));
                cnt[i]=dfs(i)-1;
                ans=max(ans,cnt[i]);
            }
        }
        int k;
        printf("Case %d: %d\n",cas++,ans);
        for(k=0;k<n;k++)
        {
            if(cnt[use[k]]==ans)
            {
                printf("%d",k);
                break;
            }
        }
        for(++k;k<n;k++)
        {
            if(cnt[use[k]]==ans)
                printf(" %d",k);
        }
        printf("\n");
    }
    return 0;
}

  

(tarjan/+DFS+反向建图) hdu 3639

标签:

原文地址:http://www.cnblogs.com/a972290869/p/4436367.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!