首页 技术 正文
技术 2022年11月14日
0 收藏 340 点赞 4,198 浏览 3749 个字

Hawk-and-Chicken

Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 39 Accepted Submission(s): 25
 
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. 
InputThere 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. 
OutputFor 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 13 3
1 0
2 1
0 2

 

Sample Output

Case 1: 2
0 1
Case 2: 2
0 1 2

 

AuthorDragon 
Source2010 ACM-ICPC Multi-University Training Contest(19)——Host by HDU 
Recommendlcy 
/*
题意:投票,投票是可以传递的,比如A投给B,B投给C,C就会得到两票初步思路:将投票的关系建成单向边的图,然后如果是在一个强连通子图中的肯定每人都能得到强连通顶点数-1的票数。
然后,剩下不是强连通子图的,只要是连成一片了,每条路的末尾的那个定点肯定能得到这条路的长度-1的票数。#错误:上面的思想有点错误,连成一片之后不需要考虑,是不是入度为零,但是去掉之后超时了.....#补充:上面的想的有点问题,判断连成片的时候,不是判断点和点之间的片,而是连通块和连通块之间的片
*/
#include<bits/stdc++.h>
using namespace std;
/**************************强连通模板******************************/
const int maxn = + ;
vector<int> G1[maxn];//正向
vector<int> G2[maxn];//逆向
int vis[maxn];//记录每个点逆向的入度
int num[maxn];//用于记录每个连通块顶点的个数
int ticket[maxn];//记录每个人的票数
int t;
int n, m, k = ;
int u,v;
int cnt = ;
int low[maxn];
int dfn[maxn];//记录这个点走没走过
int in[maxn];//记录属于哪一个强连通
stack<int> s;void tarjan(int u){
dfn[u]=low[u]=++k;
s.push(u);
for(int i=;i<G1[u].size();i++){
int v=G1[u][i];
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(!in[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u]) {
cnt++;
num[cnt]=;
while(true) {
int x=s.top(); s.pop();
in[x]=cnt;
num[cnt]++;
if(x==u) break;
}
}
}
/**************************强连通模板******************************/
int vis1[maxn];//用来逆向dfs是记录点数
int dfs(int u){
vis1[u]=true;
int sum=;
for(int i=;i<G2[u].size();i++){
int v=G2[u][i];
if(!vis1[v]) sum+=num[v]+dfs(v);
}
return sum;
}
void init(){
k=;cnt=;
for(int i=;i<n;i++){
G1[i].clear();
G2[i].clear();
vis[i]=;
}
memset(vis,,sizeof vis);
memset(ticket,,sizeof ticket);
memset(dfn,,sizeof dfn);
memset(low,,sizeof low);
memset(in,,sizeof in);
memset(num,,sizeof num);
while(!s.empty()) s.pop();
}
int main(){
// freopen("in.txt","r",stdin);
scanf("%d",&t);
for(int ca=;ca<=t;ca++){
init();
scanf("%d%d",&n,&m);
for(int i=;i<m;i++){
scanf("%d%d",&u,&v);
G1[u].push_back(v);
}
for(int i=;i<n;i++)
if(!dfn[i]) tarjan(i);
//将连通块之间逆向连接
for(int i=;i<n;i++){
cout<<in[i]<<" ";
}cout<<endl;
for(int u=;u<n;u++)
for(int i=;i<G1[u].size();i++)
{
int v=G1[u][i];
int x=in[u], y=in[v];
if(x!=y)
{
vis[x]++;
G2[y].push_back(x);//建立连通块的逆图
}
}
// for(int i=0;i<n;i++){
// cout<<num[i]<<" ";
// }cout<<endl;
int Max=-;
for(int i=;i<=cnt;i++){
if(!vis[i]){ //入度为零的联通块
memset(vis1,,sizeof vis1);
int sum=num[i]-+dfs(i);
ticket[i]=max(ticket[i],sum);
}
Max=max(Max,ticket[i]);
}
printf("Case %d: %d\n",ca,Max);
int x=;
for(int i=;i<n;i++){
if(ticket[in[i]]==Max){
if(x) printf("%d",i),x=;
else printf(" %d",i);
}
}
puts("");
}
return ;
}
/*
Case 5: 3
2 3
Case 6: 3
0 1 2 3*/
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,020
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,772
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,850