[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 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

Recommend

lcy   |   We have carefully selected several similar problems for you:  3634 3633 3636 3637 3638

Statistic | Submit | Discuss | Note

题目意思:

给一幅图,求点i,使得能够到达i的点数最多。

解题思路:

tarjan缩点,反向建图,然后dfs统计入度为0的联通分量,求出能够到达的节点数a,比较记录a最大的那些连通分量。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

#define Maxn 5500

int low[Maxn],dfn[Maxn],sta[Maxn],sc,bc,n,m,dep;
bool iss[Maxn];
int in[Maxn],nu[Maxn],dei[Maxn],ans,temp;
vector<vector<int> >myv;
vector<vector<int> >tree;
map<int,int>myp[Maxn];
bool vis[Maxn];

void tarjan(int cur)
{
    int ne;

    low[cur]=dfn[cur]=++dep;
    sta[++sc]=cur;
    iss[cur]=true;

    for(int i=0;i<myv[cur].size();i++)
    {
        ne=myv[cur][i];
        if(!dfn[ne])
        {
            tarjan(ne);
            if(low[ne]<low[cur])
                low[cur]=low[ne];
        }
        else if(iss[ne]&&dfn[ne]<low[cur])
            low[cur]=dfn[ne];
    }
    if(low[cur]==dfn[cur])
    {
        bc++;
        do
        {
            ne=sta[sc--];
            iss[ne]=false;
            in[ne]=bc;
            nu[bc]++;
        }while(ne!=cur);
    }
}
void solve()
{
    dep=sc=bc=0;
    memset(iss,false,sizeof(iss));
    memset(dfn,0,sizeof(dfn));
    memset(nu,0,sizeof(nu));

    for(int i=0;i<n;i++)
        if(!dfn[i])
            tarjan(i);
}
void dfs(int cur)
{
    vis[cur]=true;
    temp+=nu[cur];

    for(int j=0;j<tree[cur].size();j++)
    {
        int ne=tree[cur][j];
        if(!vis[ne])
            dfs(ne);
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   int t,kcas=0;

   scanf("%d",&t);
   while(t--)
   {
       scanf("%d%d",&n,&m);
       myv.clear();
       myv.resize(n+1);

       for(int i=1;i<=m;i++)
       {
           int a,b;
           scanf("%d%d",&a,&b);
           myv[a].push_back(b);
       }
       solve();

       tree.clear();
       tree.resize(bc+1);
       for(int i=1;i<=bc;i++)
            myp[i].clear();

       memset(dei,0,sizeof(dei));
       for(int i=0;i<n;i++)
       {
           for(int j=0;j<myv[i].size();j++)
           {
               int ne=myv[i][j];
               if(in[ne]!=in[i])
               {
                   if(myp[in[ne]][in[i]]==0)
                   {
                       //system("pause");
                       myp[in[ne]][in[i]]=1;
                       tree[in[ne]].push_back(in[i]);
                       dei[in[i]]++;
                   }
               }
           }
       }
       ans=0;
       /*printf("bc:%d\n",bc);
       for(int i=0;i<n;i++)
            printf("i:%d bei:%d dei:%d\n",i,in[i],dei[in[i]]);*/
       for(int i=1;i<=bc;i++)
       {
           if(!dei[i])
            {
                //printf("i:%d\n",i);
                //system("pause");

                for(int j=1;j<=bc;j++)
                    vis[j]=false;
                temp=0;
                dfs(i);
                nu[i]=temp;
                ans=max(ans,temp);
            }
       }
       printf("Case %d: %d\n",++kcas,ans-1);
       bool fi=true;

       for(int i=0;i<n;i++)
       {
           if(nu[in[i]]==ans)
           {
               if(!fi)
                    putchar(' ');
               else
                    fi=false;
               printf("%d",i);
           }

       }
       putchar('\n');
   }

    return 0;
}
/*
4 4
0 1
0 2
1 3
2 3
*/
时间: 09-30

[tarjan] 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 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 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 t

[tarjan] hdu 3836 Equivalent Sets

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3836 Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Others) Total Submission(s): 2890    Accepted Submission(s): 1006 Problem Description To prove two set

[tarjan] hdu 4635 Strongly connected

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4635 Strongly connected Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1568    Accepted Submission(s): 654 Problem Description Give a simple dir

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>

HDU - 3639 Hawk-and-Chicken(dfs+强连通分量)

题目大意:有N个人进行投票,想要选择出最受欢迎的人,投票的规则如下 1.不能投给自己 2.投票可以传递,比如A投给B一票,B投给C一票,那么C就得到了A的一票和B的一票 问票数最多能得多少,得到最高票的有哪些人 解题思路:求出所有的强连通分量,这些强连通分量里面的人得到的票数都是一样的,为强连通分量内的点数-1 接着将强连通分量进行缩点,用桥连接起来,反向建边 接着以所有出度为0的点进行dfs,得出这个连通分量内的点所能得到的最高票数(出度为0的点得到的票数是最多的) #include <cst

LCA(最近公共祖先)--tarjan离线算法 hdu 2586

HDU 2586 How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11320    Accepted Submission(s): 4119 Problem Description There are n houses in the village and some bidirectional roads c

HDU 3207 Ikki&#39;s Story IV - Panda&#39;s Trick(图论-2SAT,图论-tarjan)

Ikki's Story IV - Panda's Trick Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7821   Accepted: 2892 Description liympanda, one of Ikki's friend, likes playing games with Ikki. Today after minesweeping with Ikki and winning so many tim