现在的位置: 首页 > 综合 > 正文

HDU3639 Hawk-and-Chicken 强连通+缩点+建反向图

2017年11月22日 ⁄ 综合 ⁄ 共 3446字 ⁄ 字号 评论关闭

Hawk-and-Chicken

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

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
/*
HDU 3639 强连通+缩点+建反向图

不会,查看别人;
把杂乱的有向图通过缩点变成有向无环图,然后建反向图,
并标记每个点的入度(最大值一定在反向图的入度为的点中)然后dfs,最后就是在原图中找等于MAX的点就可以了。 
*/
#include<iostream>
#include<stdio.h> 
#include<vector>
#include<stack>
const int MAXN=5000+10;
using namespace std;

vector<int>mp1[MAXN];//原图
vector<int>mp2[MAXN];//反向图

stack<int>S;
bool mark[MAXN];//标记元素是否在栈中
int color[MAXN];//缩点,染色
int to[MAXN];//入度
int n,m,cnt,_count;
int dfn[MAXN],low[MAXN];
int num[MAXN];//用来保存所有的最大点
int dp[MAXN];//用于保存每个强连通分量的点数

//求强连通分量Tarjan算法
void Tarjan(int u)
{
    dfn[u]=low[u]=++cnt;
    mark[u]=true;
    S.push(u);
    for(int i=0;i<mp1[u].size();i++)
	{
        int v=mp1[u][i];
        if(dfn[v]==0)
		{
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }else if(mark[v])
		{
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(low[u]==dfn[u])
	{
        int v;
        do{
            v=S.top();
            S.pop();
            mark[v]=false;
            color[v]=_count;//缩点,染色
            dp[_count]++;//保存该强联通分量的点数
        }while(u!=v);
        _count++;
    }
}

//计算反向图中每一个入度为0的点的最大值
int dfs(int u){
    mark[u]=true;
    cnt+=dp[u];
    for(int i=0;i<mp2[u].size();i++){
        int v=mp2[u][i];
        if(!mark[v])dfs(v);
    }
    return cnt;
}

int main(){
    int T,t=1,x,y;
    scanf("%d",&T);
    while(T--)
	{
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
		{
            mp1[i].clear();
            mp2[i].clear();
        }
        
        for(int i=1;i<=m;i++)
		{
            scanf("%d%d",&x,&y);
            mp1[x].push_back(y);//建图 x->y 
        }
        memset(mark,false,sizeof(mark));
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(color,0,sizeof(color));
        memset(dp,0,sizeof(dp));
        memset(to,0,sizeof(to));
        memset(num,0,sizeof(num));
        _count=0;
        
		cnt=0;
        for(int i=0;i<n;i++)
		{
            if(dfn[i]==0)
                Tarjan(i);
        }
        
        //重新构图,建反向图
        for(int i=0;i<n;i++)
		{
            for(int j=0;j<mp1[i].size();j++)
			{
                //不在同一个连通分量的点
                if(color[i]!=color[mp1[i][j]])
				{
                    mp2[color[mp1[i][j]]].push_back(color[i]);
                    to[color[i]]++;//入度
                }
            }
        }
        printf("Case %d: ",t++);
        int MAX=-1,tag=0;
        for(int i=0;i<_count;i++)
		{
            //最大值一定在反向图中入度为0的点中
            cnt=0;
            if(to[i]==0){
                memset(mark,false,sizeof(mark));
                int cnt=dfs(i);
                num[i]=cnt;//保存每一个入度0的最大值
                MAX=max(MAX,cnt);
            }
        }
        printf("%d\n",MAX-1);
        //在原图中找最大的点
        for(int i=0;i<n;i++)
		{
            if(num[color[i]]==MAX)
			{
                if(!tag)
				{
                    printf("%d",i);
                    tag=1;
                }
				else
				{
                    printf(" %d",i);
                }
            }
        }
        printf("\n");
    }
    return 0;
}

抱歉!评论已关闭.