Hawk-and-Chicken

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

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 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
 
反向建图的原因是方便统计,倘若是正向的话 ,那么如果有2->3,3->1,2->1  2这个节点在计算的时候很难搞
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
const int M=;
int head[N],tot,scnt,cnt,cont,sz[N];
int dfn[N],low[N],bl[N],q[N],l;
bool instack[N],ru[N];
vector<int>G;
vector<int>C[N];
struct node{
   int to,next;
}e[M];
void add(int u,int v){
   e[tot].to=v;
   e[tot].next=head[u];
   head[u]=tot++;
}
void init(){
   tot=scnt=cnt=l=;
   memset(head,-,sizeof(head));
   memset(dfn,,sizeof(dfn));
   memset(instack,,sizeof(instack));
   memset(ru,,sizeof(ru));
   G.clear();
   for(int i=;i<N;++i) C[i].clear();
}
void Tajan(int u){
   dfn[u]=low[u]=++cnt;
   q[l++]=u;
   instack[u]=;
   for(int i=head[u];i+;i=e[i].next){
    int v=e[i].to;
    if(!dfn[v]) {
        Tajan(v);
        low[u]=min(low[u],low[v]);
    }
    else if(instack[v]&&dfn[v]<low[u])
        low[u]=dfn[v];
   }
   if(dfn[u]==low[u]) {
    ++scnt;
    int t;
    sz[scnt]=;
    do{
        t=q[--l];
        bl[t]=scnt;
        ++sz[scnt];
        C[scnt].push_back(t);
        instack[t]=;
    }while(t!=u);
   }
}
bool used[N];
void dfs(int u,int tz){
   for(int i=head[u];i+;i=e[i].next)
   if(!used[e[i].to]){
    used[e[i].to]=;
    sz[tz]+=sz[e[i].to];
    dfs(e[i].to,tz);
   }
}
struct point{
   int u,v;
}ee[M];
int main(){
    int n,m,u,v,T;
    scanf("%d",&T);
    for(int tas=;tas<=T;++tas){
        init();
        scanf("%d%d",&n,&m);
        for(int i=;i<=m;++i){
            scanf("%d%d",&u,&v);
            add(v,u);
        }
        for(int i=;i<n;++i) if(!dfn[i]) Tajan(i);
        int pp=;
        for(int i=;i<n;++i)
        for(int j=head[i];j+;j=e[j].next){
            int v=e[j].to;
            if(bl[i]==bl[v]) continue;
            else {ee[pp].u=bl[i];ee[pp++].v=bl[v];ru[bl[v]]=;}
        }
        memset(head,-,sizeof(head));
        tot=;
        for(int i=;i<pp;++i) add(ee[i].u,ee[i].v);
        int maxx=-;
        for(int i=;i<=scnt;++i)
        if(!ru[i]){
            memset(used,,sizeof(used));
            dfs(i,i);
            if(maxx<sz[i]) {maxx=sz[i];G.clear(); for(int j=;j<(int)C[i].size();++j) G.push_back(C[i][j]);}
        else if(maxx==sz[i]) for(int j=;j<(int)C[i].size();++j) G.push_back(C[i][j]);
        }
        sort(G.begin(),G.end());
        printf("Case %d: %d\n",tas,maxx-);
        for(int i=;i<(int)G.size()-;++i) printf("%d ",G[i]);
        printf("%d\n",G[(int)G.size()-]);
    }
}

强连通 反向建图 hdu3639的更多相关文章

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

    http://acm.hdu.edu.cn/showproblem.php?pid=3639 题意: 有一群孩子正在玩老鹰抓小鸡,由于想当老鹰的人不少,孩子们通过投票的方式产生,但是投票有这么一条规则 ...

  2. HPU 3639--Hawk-and-Chicken【SCC缩点反向建图 &amp;&amp; 求传递的最大值】

    Hawk-and-Chicken Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. poj1122 FDNY to the Rescue!(dij+反向建图+输出路径)

    题目链接:poj1122 FDNY to the Rescue! 题意:给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路口之间没有直接路径,再给 ...

  4. HDU4857——逃生(反向建图+拓扑排序)(BestCoder Round #1)

    逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会 ...

  5. POJ3687——Labeling Balls(反向建图+拓扑排序)

    Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...

  6. POJ-1122 FDNY to the Rescue!---Dijkstra+反向建图

    题目链接: https://vjudge.net/problem/POJ-1122 题目大意: 给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路 ...

  7. HDU 2647 Reward 【拓扑排序反向建图+队列】

    题目 Reward Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to d ...

  8. HUD2647 Reward_反向建图拓扑排序

    HDU2647 Reward 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意:老板要发奖金了,有n个人,给你m对数,类似a b,这样的一对 ...

  9. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

随机推荐

  1. Spring Boot filter

    在Spring Boot中自定义filter 本文我们将会讲解如何在Spring Boot中自定义filter并指定执行顺序. 定义Filter很简单,我们只需要实现Filter接口即可,同时我们可指 ...

  2. iOS逆向之一 工具的安装和使用

    iOS逆向之一-工具的安装和使用 最近在学习iOS安全方面的技术,有些东西就记录下来了,所有有了这篇文章.顺便也上传了DEMO,可以再这里找到这些DEMO的源码:dhar/iOSReProject 越 ...

  3. HDU 4009 Transfer water(最小树形图)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4009 题意:给出一个村庄(x,y,z).每个村庄可以挖井或者修建水渠从其他村庄得到水.挖井有一个代价, ...

  4. Hawkeye部署Github监控系统

    2019独角兽企业重金招聘Python工程师标准>>> step1:python环境安装 #pwd /usr/local/soft #wget https://www.python. ...

  5. rabbitMQ消息队列原理

    MQ:Message Queue,消息队列,是一种应用程序对应用程序的通信方法:应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们. 1      rabbitMQ入 ...

  6. CF1335E Three Blocks Palindrome

    就是我这个菜鸡,赛时写出了 E2 的做法,但是算错复杂度,导致以为自己的做法只能AC E1,就没交到 E2 上,然后赛后秒A..... 题意 定义一种字串为形如:\([\underbrace{a, a ...

  7. G - Island Transport 网络流

    题目: In the vast waters far far away, there are many islands. People are living on the islands, and a ...

  8. 【Scala】Actor并发编程实现单机版wordCount

    文章目录 对单个文本文件进行单词计数 对多个文本文件进行单词计数 对单个文本文件进行单词计数 import scala.actors.Actor import scala.io.Source //读取 ...

  9. [hihoCoder1236 Scores 2015BeijingOnline]简单粗暴的分块+简单粗暴的bitset

    题意:50000个5维向量,50000次询问每一维都不大于某一向量的向量个数,强制在线. 思路:做完这题才知道bitset效率这么高,自己本地测试了下1s可以操作1010个bit,orz简单粗暴 令S ...

  10. [hdu5411 CRB and Puzzle]DP,矩阵快速幂

    题意:给一个有向图,从任意点开始,最多走m步,求形成的图案总数. 思路:令dp[i][j]表示走j步最后到达i的方法数,则dp[i][j]=∑dp[k][j-1],其中k表示可以直接到达i的点,答案= ...