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()-]);
    }
}

最新文章

  1. 反调试技术常用API,用来对付检测od和自动退出程序
  2. JS关于时间的计算
  3. 使用scala开发spark入门总结
  4. colorbox 自适应 高度
  5. Jquery自定义扩展方法(二)--HTML日历控件
  6. D3.js 做一个简单的图表(条形图)
  7. Web前端开发面试题
  8. Hello Qt
  9. Sqoop的使用(Mysql To HBase)
  10. 使用bacula实现Linux的远程备份和还原
  11. oracle 触发器number判断空值,:NEW赋值,for each row,sql变量引号,to_date,to_char
  12. 如何实现 集群化/Session 复制-doc(cluster-howto.html)
  13. redis常用命令大全
  14. Python3.0科学计算学习之绘图(二)
  15. JAVA版本微信管家平台—JeeWx 捷微 4.1 微服务版本发布,微信砍价活动闪亮登场!
  16. GCC 符号表小结【转】
  17. String、StringBuffer、StringBulider
  18. SpringMVC解析Json字符串
  19. calc() --- css3
  20. sql语句实战

热门文章

  1. Spring Boot的TestRestTemplate使用
  2. 运用jieba库统计词频及制作词云
  3. 基于java的OpenCV安装和配置
  4. 【集群实战】Rsync常见错误总结
  5. mac OS 配置Apache服务器
  6. 工具 在 Nuget 发布自己的包
  7. angular2相关
  8. C语言编程入门题目--No.8
  9. P3119 [USACO15JAN]Grass Cownoisseur G
  10. Docker安装各种服务