HDU 3639 Hawk-and-Chicken
2015-10-02 15:39
363 查看
Hawk-and-Chicken
Time Limit: 2000msMemory Limit: 32768KB
This problem will be judged on HDU. Original ID: 3639
64-bit integer IO format: %I64d Java class name: Main
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
Source
2010 ACM-ICPC Multi-University Training Contest(19)——Host by HDU解题:强连通分量后建反图直接乱搞
#include <bits/stdc++.h> using namespace std; const int maxn = 5100; struct arc { int to,next; arc(int x = 0,int y = -1) { to = x; next = y; } } e[100010]; int head[maxn],dfn[maxn],low[maxn],cnt[maxn],clk,scc,tot; int belong[maxn],du[maxn],fan[maxn],n,m; bool instack[maxn]; stack<int>stk; vector<int>g[maxn]; void add(int u,int v) { e[tot] = arc(v,head[u]); head[u] = tot++; } void tarjan(int u) { dfn[u] = low[u] = ++clk; instack[u] = true; stk.push(u); for(int i = head[u]; ~i; i = e[i].next) { if(!dfn[e[i].to]) { tarjan(e[i].to); low[u] = min(low[u],low[e[i].to]); } else if(instack[e[i].to]) low[u] = min(low[u],dfn[e[i].to]); } if(low[u] == dfn[u]) { int v; cnt[++scc] = 0; do { instack[v = stk.top()] = false; stk.pop(); belong[v] = scc; cnt[scc]++; } while(v != u); } } bool vis[maxn]; void init() { for(int i = tot = clk = scc = 0; i < maxn; ++i) { head[i] = -1; dfn[i] = low[i] = belong[i] = 0; instack[i] = false; g[i].clear(); fan[i] = du[i] = 0; } } int dfs(int u) { vis[u] = true; int ret = cnt[u]; for(int i = g[u].size()-1; i >= 0; --i) { if(vis[g[u][i]]) continue; ret += dfs(g[u][i]); } return ret; } int main() { int kase,u,v,cs = 1; scanf("%d",&kase); while(kase--) { scanf("%d%d",&n,&m); init(); for(int i = 0; i < m; ++i) { scanf("%d%d",&u,&v); add(u,v); } for(int i = 0; i < n; ++i) if(!dfn[i]) tarjan(i); for(int i = 0; i < n; ++i) { for(int j = head[i]; ~j; j = e[j].next) { if(belong[i] == belong[e[j].to]) continue; g[belong[e[j].to]].push_back(belong[i]); du[belong[i]]++; } } int mx = -1; for(int i = 1; i <= scc; ++i) if(!du[i]) { memset(vis,false,sizeof vis); fan[i] = dfs(i) - 1; mx = max(mx,fan[i]); } bool flag = true; printf("Case %d: %d\n",cs++,mx); for(int i = 0; i < n; ++i) if(fan[belong[i]] == mx) { if(flag) { printf("%d",i); flag = false; } else printf(" %d",i); } puts(""); } return 0; }
View Code
相关文章推荐
- happens-before规则解析
- 在Windows下制作静态库和动态库
- HDU 1269 迷宫城堡(强连通)
- Rich控件一
- 值参数
- ForTran快速入门
- 作为码农,我们为什么要写作
- C#笔记誊录<二>
- codeforces round 321 div2 D Kefa and Dishes(状态压缩dp)
- lca
- 结对编程任务的算法流程思路
- 2013 ACM-ICPC吉林通化全国邀请赛 && HDU 4499 Cannon (搜索)
- Single Number III -- leetcode
- 欧几里得算法求两个正整数的最大公因子
- redis教程(二):安装遇到的某些问题
- 冒泡排序算法 C++和PHP达到
- Light oj 1038 - Race to 1 Again(概率dp)
- Aoj 2450 Do use segment tree【树链剖分】
- 图论——拓扑排序
- sql笔记