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

来源:互联网 发布:骑马与砍杀哈劳斯数据 编辑:程序博客网 时间:2024/05/22 04:37

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
24 33 22 02 13 31 02 10 2


Sample Output
Case 1: 20 1Case 2: 20 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;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 百家利钱不到账怎么办 皮鞋撑大了怎么办变小 鞋穿着走路掉跟怎么办 浅口帆布鞋大了怎么办 布鞋后鞋跟老掉怎么办 穿坡跟凉鞋容易打滑摔倒怎么办 运动鞋烂了个口怎么办 运动鞋的网烂了怎么办 运动鞋鞋一个一个高一个低怎么办 淘宝预售水果不发货怎么办 淘宝评价忘记晒图了怎么办 参加水果展没有实物怎么办 没做过运营面试怎么办 苹果5s16g内存满了怎么办 618天猫有活动淘宝没有怎么办 天猫国际买到假货怎么办 天猫恶意差评怎么办 天猫删除差评被扣分了怎么办 天猫收到差评怎么办 天猫给差评骚扰怎么办 天猫客户差评怎么办 天猫没法给差评怎么办 天猫上限购一件怎么办 拍下商品不发货怎么办 天猫红包过期了怎么办 买天猫店被中介骗了钱怎么办 速卖通假货纠纷怎么办 天猫新开店被恶意拍下怎么办 新开的天猫店没有生意怎么办 银行的支票丢了怎么办 天猫积分用光了怎么办 淘宝店没有无线流量怎么办 京东卖家不发货怎么办会自动打款 一件代发顾客如果退款怎么办 京东以前的账号怎么办 旺旺对话框订单页面变宽了怎么办 阿里巴巴店铺上传图片很模糊怎么办 天猫跨店优惠券用了退货怎么办 淘宝店铺没流量没访客怎么办 京东微信和Q端黑号了怎么办? 买不了运费险了怎么办