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.
 
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
 
Author
Dragon
 
Source
2010 ACM-ICPC Multi-University Training Contest(19)——Host by HDU
 
Recommend
lcy
 
/*
题意:投票,投票是可以传递的,比如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 */

Hawk-and-Chicken的更多相关文章

  1. 设计爬虫Hawk背后的故事

    本文写于圣诞节北京下午慵懒的午后.本文偏技术向,不过应该大部分人能看懂. 五年之痒 2016年,能记入个人年终总结的事情没几件,其中一个便是开源了Hawk.我花不少时间优化和推广它,得到的评价还算比较 ...

  2. 120项改进:开源超级爬虫Hawk 2.0 重磅发布!

    沙漠君在历时半年,修改无数bug,更新一票新功能后,在今天隆重推出最新改进的超级爬虫Hawk 2.0! 啥?你不知道Hawk干吗用的? 这是采集数据的挖掘机,网络猎杀的重狙!半年多以前,沙漠君写了一篇 ...

  3. Hawk 7. 常见问题

    本页面您可以通过关键字搜索来获取信息. 理性使用爬虫 爬虫是一种灰色的应用,虽然作为Hawk的设计者,但我依然不得不这么说. 各大网站都在收集和整理数据上花费了大量的精力,因此抓取的数据应当仅仅作为科 ...

  4. Hawk 6. 编译和扩展开发

    Hawk是开源项目,因此任何人都可以为其贡献代码.作者也非常欢迎使用者能够扩展出更有用的插件. 编译 编译需要Visual Stuido,版本建议使用2015, 2010及以上没有经过测试,但应该可以 ...

  5. Hawk 5.1 数据导入和导出

    除了一般的数据库导入导出,Hawk还支持从文件导入和导出,支持的文件类型包括: Excel CSV(逗号分割文本文件) TXT (制表符分割文本文件) Json xml Excel 目前来看,Exce ...

  6. Hawk 5. 数据库系统

    Hawk在设计之初,就是以弱schema风格定义的.没有严格的列名和列属性.用C#这样的静态强类型语言编写Hawk,其实并不方便.但弱schema让Hawk变得更灵活更强大. 因此,Hawk虽然之前支 ...

  7. Hawk 4.6 并行化

    并行化 Hawk支持单机并行化,也就是使用多线程获取数据.它可以控制目前所有任务的数量,为了不给网站造成过大的压力,仅当任务池中的任务数量小于一定值后,才会插入新的任务. 你可以在数据清洗的 执行面板 ...

  8. Hawk 4.4 执行器

    执行器是负责将Hawk的结果传送到外部环境的工具.你可以写入数据表,数据库,甚至执行某个特定的动作,或是生成文件等等. 在调试模式下,执行器都是不工作的.这是为了避免产生副作用.否则,每刷新一遍数据, ...

  9. Hawk 4.3 转换器

    转换器是最为常用的一种类型,当然它的使用也是最复杂的. 转换器有三种子类型: A:单文档->单文档:例如仅将某一列的字符提取出来 B:单文档->多文档:典型的如从爬虫转换,每一行url都可 ...

  10. Hawk 4. 数据清洗

    数据清洗模块,包括几十个子模块, 这些子模块包含四类:生成, 转换, 过滤和执行. 数据清洗可以通过组合多个不同的子模块,生成多样的功能,通过拖拽构造出一个工作流,它能够产生一个有限或无限的文档序列. ...

随机推荐

  1. Angular2 Service实践

    引言: 如果说组件系统(Component)是ng2应用的躯体,那把服务(Service)认为是流通于组件之间并为其带来生机的血液再合适不过了.组件间通信的其中一种优等选择就是使用服务,在ng1里就有 ...

  2. 【个人笔记】《知了堂》MySQL三种关系:一对一,一对多,多对多。

    一对一:比如一个学生对应一个身份证号.学生档案: 一对多:一个班可以有很多学生,但是一个学生只能在一个班: 多对多:一个班可以有很多学生,学生也可以有很多课程: 一对多关系处理: 我们以学生和班级之间 ...

  3. 如何用kaldi做孤立词识别-初版

    ---------------------------------------------------------------------------------------------------- ...

  4. [js高手之路] html5 canvas系列教程 - 文本样式(strokeText,fillText,measureText,textAlign,textBaseline)

    接着上文线条样式[js高手之路] html5 canvas系列教程 - 线条样式(lineWidth,lineCap,lineJoin,setLineDash)继续. canvas提供两种输出文本的方 ...

  5. 使用paramiko远程执行

    写部署脚本时,难免涉及到一些远程执行命令或者传输文件. 之前一直使用sh库,调用sh.ssh远程执行一些命令,sh.scp传输文件,但是实际使用中还是比较麻烦的,光是模拟用户登陆这一点,还需要单独定义 ...

  6. 深入浅出数据结构C语言版(22)——排序决策树与桶式排序

    在(17)中我们对排序算法进行了简单的分析,并得出了两个结论: 1.只进行相邻元素交换的排序算法时间复杂度为O(N2) 2.要想时间复杂度低于O(N2),算法必须进行远距离的元素交换 而今天,我们将对 ...

  7. 【Spring】高级装配

    前言 前面讲解了bean的核心装配技术,其可应付很多中装配情况,但Spring提供了高级装配技术,以此实现更为高级的bean装配功能. 高级装配 配置profile bean 将所有不同bean定义放 ...

  8. Mybatis Dynamic Query 2.0.2

    项目地址:https://github.com/wz2cool/mybatis-dynamic-query 文档地址:https://wz2cool.gitbooks.io/mybatis-dynam ...

  9. GCD hdu1695容斥原理

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  10. 用ESP8266+android,制作自己的WIFI小车(Android 软件)

    先说一下这篇文章里面的内容:TCP 客户端, 自定义对话框, 自定义按钮, ProgressBar竖直显示, 重力感应传感器,手机返回键新开启界面的问题(返回上次的界面),数据保存 软件的通信是配合 ...