强连通 反向建图 hdu3639
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
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.
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.
Then follow a line contain all the Hawks' number. The numbers must be listed in increasing order and separated by single spaces.
#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()-]);
}
}
强连通 反向建图 hdu3639的更多相关文章
- HDU 3639 Hawk-and-Chicken(强连通缩点+反向建图)
http://acm.hdu.edu.cn/showproblem.php?pid=3639 题意: 有一群孩子正在玩老鹰抓小鸡,由于想当老鹰的人不少,孩子们通过投票的方式产生,但是投票有这么一条规则 ...
- HPU 3639--Hawk-and-Chicken【SCC缩点反向建图 && 求传递的最大值】
Hawk-and-Chicken Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- poj1122 FDNY to the Rescue!(dij+反向建图+输出路径)
题目链接:poj1122 FDNY to the Rescue! 题意:给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路口之间没有直接路径,再给 ...
- HDU4857——逃生(反向建图+拓扑排序)(BestCoder Round #1)
逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会 ...
- POJ3687——Labeling Balls(反向建图+拓扑排序)
Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...
- POJ-1122 FDNY to the Rescue!---Dijkstra+反向建图
题目链接: https://vjudge.net/problem/POJ-1122 题目大意: 给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路 ...
- HDU 2647 Reward 【拓扑排序反向建图+队列】
题目 Reward Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to d ...
- HUD2647 Reward_反向建图拓扑排序
HDU2647 Reward 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意:老板要发奖金了,有n个人,给你m对数,类似a b,这样的一对 ...
- POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
随机推荐
- Excel中的clean函数
纯属note. 之前经常用excel处理数据的时候,对长文本或网站上拉取的值都会用clean函数清除一些我们肉眼看不到的非打印字符. Excel官方介绍:clean 删除文本中的所有非打印字符. 此次 ...
- SpringCloud之整合Feign
假设提供者有如下服务接口方法 @RestController @RequestMapping("/person") public class PersonController { ...
- Linked List-1
链表一直是面试的重点问题,恰好最近看到了Stanford的一篇材料,涵盖了链表的基础知识以及派生的各种问题. 第一篇主要是关于链表的基础知识. 一.基本结构 1.数组回顾 链表和数组都是用来存储一堆数 ...
- Fourier Transform
为了在统一框架里分析周期信号与非周期信号,可以给周期信号也建立傅里叶变换. 有两种方法求周期信号的傅里叶变换: **1. 利用傅里叶级数进行构造 ** 对于周期信号\(x(t)\),其傅里叶级数展开式 ...
- 题目分享N
题意:有辆车,有r行,s*2列,在第s列和第s+1列之间有个过道,出口在第r+1行的过道处,现在给出每个人的位置(行号和列号),每人每次只能动一格,问最少耗费多长时间全员才能逃出去 分析:假如车上只有 ...
- 2019国防科大校赛 B Escape LouvreⅡ
https://ac.nowcoder.com/acm/contest/878/B 这个题目是一个网络流,但是建图却没有那么好建,首先我们都会把每一个人与源点相连,每一个洞口和汇点相连. 然后人和洞口 ...
- 【Spark】RDD的依赖关系和缓存相关知识点
文章目录 RDD的依赖关系 宽依赖 窄依赖 血统 RDD缓存 概述 缓存方式 RDD的依赖关系 RDD和它依赖的父RDD的关系有两种不同的类型,即窄依赖(narrow dependency) 和宽依赖 ...
- JAVA知识总结(四):单例模式和多态
好吧,今天一定要把面向对象的最后一个特性:多态,给说完.不过我们先来聊一聊设计模式,因为它很重要. 设计模式 官方的解释是,设计模式是:一套被反复使用,多数人知晓的,经过分类编目,代码设计经验的总结. ...
- 小程序如何动态修改标题navigationBarTitleText
首先我们先设置标题.进入页面所在的json文件加入以下代码即可成功设置: "navigationBarTitleText": "我是标题啊!", 然后修改这个标 ...
- HMM-前向后向算法理解与实现(python)
目录 基本要素 HMM三大问题 概率计算问题 前向算法 后向算法 前向-后向算法 基本要素 状态 \(N\)个 状态序列 \(S = s_1,s_2,...\) 观测序列 \(O=O_1,O_2,.. ...