Hawk-and-Chicken

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

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
 
分析:
题目意思:
给你一个有向图,a可以到b,b可以到c,那么c点就可以得到两分
问你得到分数最高的点是哪些点(升序输出),且最高的得分是多少
分析:
对于某个点x
就是算有多少个点可以到x
直接dfs的话,不行,得反向建图,原因:
正向建图的话,对x点,有的点因为dfs顺序导致点标记的原因,导致某些点不到到达x
比如图:
1->x
2->x
3->1
3->2
如果是先dfs1,然后dfs2的话,1和2都被标记了,不能通过了,导致3不能到达x,实际上3能到x
反向建图的话,只要看x能到哪些点就可以了,可以直接dfs,不存在上述麻烦
只要反向建图,不论是否有强连通分量,都是可以直接dfs的!!!!
是对出度为0的点dfs
但是看看数据范围,直觉应该是会超时....
然后来了一发,果然超时
所以我们得进行优化
优化方法:
对强连通分量进行缩点处理
对某个强连通分量,假设该强连通分量有k个点,那么该强连通分量的每个点的得分都是k-1
所以我们缩点,然后重新建图,得到一个没有强连通分量的有向图
然后再跑dfs
所以我们用tarjan算法进行缩点重新建图处理
 
code:
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 0x7fffffff
#define me(a,x) memset(a,x,sizeof(a))
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
} #define max_v 5005
int vis[max_v];
int dfn[max_v];
int low[max_v];
int stk[max_v];
int color[max_v];//染色
int indgree[max_v];//入度
int v[max_v];//存放每个点dfs的结果
int a[max_v];//存放需要输出的分数最高的点
int used[max_v];//dfs的标记数组
int b[max_v];//每种颜色所包括的点的数量
vector<int> G[max_v];//原图
vector<int> G1[max_v];//新图
int n,m;
int sig,cnt,sp;
int cu; void init()
{
me(vis,);
me(dfn,);
me(low,);
me(stk,);
me(color,);
me(indgree,);
me(v,);
me(a,);
me(b,);
for(int i=;i<=n;i++)
{
G[i].clear();
G1[i].clear();
}
sig=;
cnt=;
sp=-;
cu=;
} void tarjan(int u)
{
dfn[u]=low[u]=cnt++;
vis[u]=;
stk[++sp]=u;
for(int j=;j<G[u].size();j++)
{
int v=G[u][j];
if(vis[v]==)
tarjan(v);
if(vis[v]==)
low[u]=min(low[u],low[v]);
}
if(dfn[u]==low[u])
{
sig++;
do
{
vis[stk[sp]]=-;
color[stk[sp]]=sig;
}while(stk[sp--]!=u);
}
} int f(int x)//统计颜色x的点的数目
{
int sum=;
for(int i=;i<=n;i++)
{
if(color[i]==x)
sum++;
}
return sum;
} int dfs(int u,int ans)
{
used[u]=;
for(int j=;j<G1[u].size();j++)
{
int v=G1[u][j];
if(used[v]==)
{
ans+=b[v];
ans=dfs(v,ans);
} }
return ans;
}
void ff(int x)//将颜色为x的点放进输出数组
{
for(int i=;i<=n;i++)
{
if(color[i]==x)
a[cu++]=i-;
}
}
int main()
{
int t;
int ca=;
int x,y;
cin>>t;
while(t--)
{
scanf("%d %d",&n,&m);
init();
for(int i=;i<=m;i++)
{
scanf("%d %d",&x,&y);
x++,y++;
if(count(G[x].begin(),G[x].end(),y)==)//防止重边
G[x].push_back(y);
}
for(int i=;i<=n;i++)
{
if(vis[i]==)
tarjan(i);
}
for(int i=;i<=sig;i++)
b[i]=f(i);//记忆化优化 //建新图,反向
for(int i=;i<=n;i++)
{
for(int j=;j<G[i].size();j++)
{
int v=G[i][j];
if(color[v]!=color[i])
{
if(count(G1[color[v]].begin(),G1[color[v]].end(),color[i])==)//预防重边
{
G1[color[v]].push_back(color[i]);
indgree[color[i]]++;
}
}
}
}
//跑新图入度为0的点的dfs
int maxv=-INF;
for(int i=;i<=sig;i++)
{
if(indgree[i]==)
{
me(used,);
v[i]=dfs(i,)+b[i]-;
maxv=max(maxv,v[i]);
}
}
printf("Case %d: %d\n",ca++,maxv);
for(int i=;i<=sig;i++)
{
if(v[i]==maxv)//存在得分最高的多个点
{
ff(i);
}
}
sort(a,a+cu);
for(int i=;i<cu;i++)
{
if(i==)
printf("%d",a[i]);
else
printf(" %d",a[i]);
}
printf("\n");
}
return ;
}
/*
题目意思:
给你一个有向图,a可以到b,b可以到c,那么c点就可以得到两分
问你得到分数最高的点是哪些点(升序输出),且最高的得分是多少 分析:
对于某个点x
就是算有多少个点可以到x
直接dfs的话,不行,得反向建图,原因:
正向建图的话,对x点,有的点因为dfs顺序导致点标记的原因,导致某些点不到到达x
比如图:
1->x
2->x
3->1
3->2
如果是先dfs1,然后dfs2的话,1和2都被标记了,不能通过了,导致3不能到达x,实际上3能到x
反向建图的话,只要看x能到哪些点就可以了,可以直接dfs,不存在上述麻烦 只要反向建图,不论是否有强连通分量,都是可以直接dfs的!!!!
是对出度为0的点dfs
但是看看数据范围,直觉应该是会超时....
然后来了一发,果然超时 所以我们得进行优化
优化方法:
对强连通分量进行缩点处理
对某个强连通分量,假设该强连通分量有k个点,那么该强连通分量的每个点的得分都是k-1
所以我们缩点,然后重新建图,得到一个没有强连通分量的有向图
然后再跑dfs
所以我们用tarjan算法进行缩点重新建图处理
*/
 

HDU 3969 Hawk-and-Chicken(dfs+tarjan缩点优化,网上最详细解析!!!)的更多相关文章

  1. HDU 6165 FFF at Valentine(Tarjan缩点+拓扑排序)

    FFF at Valentine Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. hihoCoder 1185 连通性·三(Tarjan缩点+暴力DFS)

    #1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...

  3. 【BZOJ-1797】Mincut 最小割 最大流 + Tarjan + 缩点

    1797: [Ahoi2009]Mincut 最小割 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1685  Solved: 724[Submit] ...

  4. HDU4612+Tarjan缩点+BFS求树的直径

    tarjan+缩点+树的直径题意:给出n个点和m条边的图,存在重边,问加一条边以后,剩下的桥的数量最少为多少.先tarjan缩点,再在这棵树上求直径.加的边即是连接这条直径的两端. /* tarjan ...

  5. POJ 1236 Network of Schools(强连通 Tarjan+缩点)

    POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意:  给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...

  6. poj3694(tarjan缩点+lca)

    传送门:Network 题意:给你一个连通图,然后再给你n个询问,每个询问给一个点u,v表示加上u,v之后又多少个桥. 分析:方法(1219ms):用并查集缩点,把不是桥的点缩成一个点,然后全图都是桥 ...

  7. 强连通分量tarjan缩点——POJ2186 Popular Cows

    这里的Tarjan是基于DFS,用于求有向图的强联通分量. 运用了一个点dfn时间戳和low的关系巧妙地判断出一个强联通分量,从而实现一次DFS即可求出所有的强联通分量. §有向图中, u可达v不一定 ...

  8. [Usaco2015 Jan]Grass Cownoisseur Tarjan缩点+SPFA

    考试的时候忘了缩点,人为dfs模拟缩点,没想到竟然跑了30分,RB爆发... 边是可以重复走的,所以在同一个强连通分量里,无论从那个点进入从哪个点出,所有的点一定能被一条路走到. 要使用缩点. 然后我 ...

  9. BZOJ2199[Usaco2011 Jan]奶牛议会——2-SAT+tarjan缩点

    题目描述 由于对Farmer John的领导感到极其不悦,奶牛们退出了农场,组建了奶牛议会.议会以“每头牛 都可以获得自己想要的”为原则,建立了下面的投票系统: M只到场的奶牛 (1 <= M ...

随机推荐

  1. POJ1458(KB12-L LCS)

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 51319   Accepted: 21 ...

  2. 04-基本的mysql语句

    [转]04-基本的mysql语句 本节课先对mysql的基本语法初体验. 操作文件夹(库) 增 create database db1 charset utf8; 查 # 查看当前创建的数据库 sho ...

  3. linux vim 行缩进,批量移动多行

    显示行号用::set nu :49>5  从第49行开始,连接5行右移一个tab. :49,93>   从第49行开始到93行右移一个tab 选中多行,然后移动 https://jingy ...

  4. 2017年5月24日 HTML 基础知识(二)

    1 快捷方式:html:xt +tab   过渡XHTML html:xs+tab  严格XHTML !+tab  html5的标签结构 2.Charset   编码 <meta charset ...

  5. 网页CSS中*{margin:0; padding:0;}有什么用

    * 这东西叫“通配符”用来匹配页面上所有元素.*{margin:0; padding:0;} 像 2L 所说,body ,ul, li ,p,h1~h6,dd,dt 等……都有默认的margin 或p ...

  6. SQLServer 远程链接MySql数据库详解

    SQLServer 远程链接MySql数据库详解 by:授客 QQ:1033553122 测试环境: Microsoft Windows XP Professional 版本2000 Service ...

  7. VS2010部署相关

    找到一篇写得最负责的.贴住收藏了: http://blog.csdn.net/xhf55555/article/details/7702212. 之前在其它地方找的都缺胳膊少腿,真不知他们自己怎么实现 ...

  8. Echarts柱状图百分比显示

    option = { tooltip: { trigger: 'item', formatter:'{c}%' //这是关键,在需要的地方加上就行了 }, grid: { borderWidth: 0 ...

  9. Hive lateral view explode

    select 'hello', x from dual lateral view explode(array(1,2,3,4,5)) vt as x 结果是: hello   1 hello   2 ...

  10. linux设置永久环境变量

    vi /etc/profile 文件最后输入export PATH=$PATH:/usr/abc/def/ 保存 输入source /etc/profile刷新刚刚修改过的环境变量文件