HDU 3639 Hawk-and-Chicken

Hawk-and-Chicken

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1979    Accepted Submission(s): 570

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

Author

Dragon

Source

2010 ACM-ICPC Multi-University
Training Contest(19)——Host by HDU

解题思路:首先是强连通分量缩点,然后重新构图,对于同一个强连通分量的点的支持度一是强连通分量内的其他点,还有其他的强连通分量对他所在强连通分量的支持,为了找到这个我们之前构图时反向建图,然后bfs

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<queue>
using namespace std;
#define Max 5005
int head[Max],head2[Max],j,k,bcnt,dindex,dfn[Max],low[Max],belong[Max],sup[Max];
bool visit[Max],vis[Max];;
stack<int> s;
struct
{
    int s;
    int e;
    int next;
}edge[Max*6],edge2[Max*6];
void add(int s,int e)
{
    edge[k].s=s;
    edge[k].e=e;
    edge[k].next=head[s];
    head[s]=k++;
}
void add2(int s,int e)
{
    edge2[j].s=s;
    edge2[j].e=e;
    edge2[j].next=head2[s];
    head2[s]=j++;
}
void tarjan(int i)
{
    int ed;
    dfn[i]=low[i]=++dindex;
    visit[i]=true;
    s.push(i);
    for(int t=head[i];t!=-1;t=edge[t].next)
    {
        ed=edge[t].e;
        if(!dfn[ed])
        {
            tarjan(ed);
            if(low[i]>low[ed])
                low[i]=low[ed];
        }
        else if(visit[ed]&&low[i]>dfn[ed])
            low[i]=dfn[ed];
    }
    if(dfn[i]==low[i])
    {
        bcnt++;
        do
        {
            ed=s.top();
            s.pop();
            visit[ed]=false;
            belong[ed]=bcnt;
        }while(i!=ed);
    }
}
void solve(int n)
{
    int i;
    bcnt=dindex=0;
    memset(visit,false,sizeof(visit));
    memset(low,0,sizeof(low));
    memset(belong,0,sizeof(belong));
    memset(dfn,0,sizeof(dfn));
    while(!s.empty())
        s.pop();
    for(i=0;i<n;i++)
        if(!dfn[i])
            tarjan(i);
}
int bfs(int rt)
{
    int i,st,ed,sum=0;
    vis[rt]=true;
    queue<int> q;
    q.push(rt);
    while(!q.empty())
    {
        st=q.front();
        q.pop();
        for(i=head2[st];i!=-1;i=edge2[i].next)
        {
            ed=edge2[i].e;
            if(!vis[ed])
            {
                sum+=sup[ed]+1;
                q.push(ed);
                vis[ed]=true;
            }
        }
    }
    return sum;
}
int main()
{
    int i,t,m,n,a,b,ans[Max],sum[Max],flag,cnt,p[Max],ncase=1;
    scanf("%d",&t);
    while(t--)
    {
        flag=-1;
        j=k=cnt=0;
        memset(p,0,sizeof(p));
        memset(ans,0,sizeof(ans));
        memset(sum,0,sizeof(sum));
        memset(head,-1,sizeof(head));
        memset(head2,-1,sizeof(head2));
        memset(sup,0,sizeof(sup));
        scanf("%d%d",&n,&m);
        while(m--)
        {
            scanf("%d%d",&a,&b);
            add(a,b);
        }
        solve(n);
        for(i=0;i<k;i++)
        {
            int st=edge[i].s;
            int ed=edge[i].e;
            if(belong[st]!=belong[ed])
                add2(belong[ed],belong[st]);
        }
        for(i=0;i<n;i++)
            sup[belong[i]]++;
        for(i=1;i<=bcnt;i++)
            sup[i]--;
        for(i=1;i<=bcnt;i++)
        {
            memset(vis,false,sizeof(vis));
            sum[i]=bfs(i)+sup[i];
        }
        for(i=0;i<n;i++)
        {
            ans[i]=sum[belong[i]];
            flag=max(flag,ans[i]);
        }
        for(i=0;i<n;i++)
        {
            if(ans[i]==flag)
                p[cnt++]=i;
            //cout<<ans[i]<<endl;
        }
        printf("Case %d: %d\n",ncase++,flag);
        for(i=0;i<cnt;i++)
            printf("%d%c",p[i],i==cnt-1?'\n':' ');
    }
    return 0;
}
时间: 2024-08-05 16:40:56

HDU 3639 Hawk-and-Chicken的相关文章

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

G - Hawk-and-Chicken Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3639 Description Kids in kindergarten enjoy playing a game called Hawk-and-Chicken. But there always exists a big problem: ev

HDU 3639 Hawk-and-Chicken (强连通分量+树形DP)

题目地址:HDU 3639 先用强连通分量缩点,缩点之后,再重新按缩点之后的块逆序构图,每个块的值是里边缩的点的个数,那么得到选票的最大的一定是重新构图后入度为0的块,然后求出来找最大值即可. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h>

hdu 3639 有向图缩点+建反向图+搜索

题意:给个有向图,每个人可以投票(可以投很多人,一次一票),但是一个人只能支持一人一次,支持可以传递,自己支持自己不算,被投支持最多的人. 开始想到缩点,然后搜索,问题是有一点想错了!以为支持按票数计算,而不是按人数!还各种树形dp/搜索求可以到达边数..提交WA了... 又反复读了题目之后才发现..错误..只要人数就行...问题简单了许多... 缩点成有向无环图后:每个SCC中支持的人数就是scc里面的人,要求可到达的点最多的点,当然要放过来求方便:反向图那个点可以到达的点最多!于是建反向图直

HDU 3639 Hawk-and-Chicken(强连通)

HDU 3639 Hawk-and-Chicken 题目链接 题意:就是在一个有向图上,满足传递关系,比如a->b, b->c,那么c可以得到2的支持,问得到支持最大的是谁,并且输出这些人 思路:先强连通的缩点,然后逆向建图,对于每个出度为0的点,进行dfs求哪些点可达这个点 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #inclu

[tarjan] hdu 3639 Hawk-and-Chicken

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3639 Hawk-and-Chicken Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1986    Accepted Submission(s): 575 Problem Description Kids in kindergarte

HDU 3639 Hawk-and-Chicken(强连通缩点+反向建图)

http://acm.hdu.edu.cn/showproblem.php?pid=3639 题意: 有一群孩子正在玩老鹰抓小鸡,由于想当老鹰的人不少,孩子们通过投票的方式产生,但是投票有这么一条规则:投票具有传递性,A支持B,B支持C,那么C获得2票(A.B共两票),输出最多能获得的票数是多少张和获得最多票数的人是谁? 思路: 先强连通缩点反向建图,在计算强连通的时候,需要保存每个连通分支的结点个数. 为什么要反向建图呢?因为要寻找票数最多的,那么肯定是入度为0的点,然后dfs计算它的子节点的

hdu 3639 Hawk-and-Chicken 【强连通分量+反向建图dfs】

链接:http://acm.hdu.edu.cn/showproblem.php?pid=3639 题意:有n个人,m条边,每条边代表u给v投一票,票可以传递,比如A->B,B->C,这时C可以得到2票,求出得到最大票数的人有哪些. 分析:从一个强连通到另一个强连通分量的贡献为这两个强连通分量大小和减一.显然票数最大的人在图的端点. 将缩点后的图方向,可以得到一些入度为0的点,用DFS可以求出这些点的票数,最后去求出最大票数. 代码: <pre name="code"

HDU 3639 Hawk-and-Chicken(Tarjan缩点+反向DFS)

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

HDU 3639 SCC Hawk-and-Chicken

求SCC缩点,统计出每个SCC中的点的个数. 然后统计能到达u的最多的点的个数,可以反向建图,再dfs一遍统计出来. 最后说一下,有必要开一个标记数组,因为测试数据中有重边,结果无限WA. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <stack>