HPU 3639--Hawk-and-Chicken【SCC缩点反向建图 && 求传递的最大值】
Hawk-and-Chicken
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2409 Accepted Submission(s): 712
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.
2
4 3
3 2
2 0
2 1 3 3
1 0
2 1
0 2
Case 1: 2
0 1
Case 2: 2
0 1 2
题意:如今有n个孩子正在玩游戏,完毕一个传递的游戏,举个样例,A传递到B小朋友,然后B小朋友传递给C小朋友。那么此时,C小朋友将拥有2的传递值,C是胜利者,若C又传递回A。那么A,B,C三者都拥有传递值为2的胜利者。问胜利着获得的最大传递值,以及胜利者的编号(可能有多个)。
解析:这题的 n的数据量为5000, 暴力肯定是不行的,有题意可知:如有 A - > B, B - > C, C - >A。那么A。B,C都是拥有传递值为2的胜利者。
所以我们能够先缩点求出SCC。对每一个SCC内的点来说。 它们的传递值是一样的,但要明确:假设从一个SCC:a, 传到还有一个SCC:b, 即 a
- > b。传递的值为SCC a 内部点的个数。这点要清楚
并且我们知道,胜利者肯定在那些出度为0的SCC中,所以我们能够从每一个入度为0的SCC開始搜索,DFS遍历一遍,得到每一个出度为0的SCC的传递值,找出最大的就可以,
但这样的思路是不正确的,由于入度为0的SCC的个数肯定远远的大于出度为0的SCC个数,并且这题的n是很大的。枚举每一个入度为0的SCC然后DFS遍历一遍肯定会超时。
所以我们要反向建图, 这样每次从入度为0的SCC【即正向建边出度为0 的SCC】開始DFS,就能够得到每一个入度为0的SCC的传递值。
找出当中传递值最大的。这里还有注意
当我们找这个传递值最大的SCC :A 时,我们把这SCC的传递值多算了1,由于我们算的时候是累加的每一个SCC中点的个数。
当找到传递值最大的SCC A时,它内部的传递值为 内部点的个数 - 1。(如A - > B, B - > C, C - >A,传递值为2),但我们加的时候没有 - 1,所以最后的结果 - 1才是正确答案,这点比較绕,我解释的也不是太清楚。读者自己多想想。不是太难明确。
明天開始刷独立集,2-SAT。 强连通就先做到这。。
这应该是有史以来自己写的最长的解析了,曾经直接总是懒得写题解。认为能写出AC代码即可了,有时懒省事就粘一下别人的。但如今才发现自己写解析的时候收获才最大的,曾经真真真真真蠢。能把自己的思路完整的用文字呈现出来。肯定是比較理解解题的思路了。以后解析都要自己写。还要好好写。加油阿欢。
<pre name="code" class="cpp">#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#define maxn 5000 + 500
#define maxm 30000 + 3000
using namespace std; struct node{
int u, v, next;
}; node edge[maxm]; int head[maxn], cnt;
int low[maxn], dfn[maxn];
int dfs_clock;
int Belong[maxn];
int scc_clock;
bool Instack[maxn];
int Stack[maxn], top;
bool vis[maxn];//记录缩点后的SCC是否訪问
int in[maxn];//记录SCC的入度
int num[maxn];//记录每一个SCC能得到的最大手帕数
vector<int>scc[maxn];//存储每一个SCC中的节点
vector<int> Map[maxn];//存储缩点后新图
int n, m, k; void init(){
cnt = 0;
memset(head, -1, sizeof(head));
} void add(int u, int v){
edge[cnt] = {u, v, head[u]};
head[u] = cnt++;
} void getmap(){
scanf("%d%d", &n, &m);
int a, b;
while(m--){
scanf("%d%d", &a, &b);
a++, b++;
add(a, b);
}
} void tarjan(int u){
int v;
low[u] = dfn[u] = ++dfs_clock;
Stack[top++] = u;
Instack[u] = true;
for(int i = head[u]; i != -1; i = edge[i].next){
v = edge[i].v;
if(!dfn[v]){
tarjan(v);
low[u] = min(low[u], low[v]);
}
else if(Instack[v])
low[u] = min(low[u], dfn[v]);
}
if(low[u] == dfn[u]){
scc_clock++;
scc[scc_clock].clear();
do{
v = Stack[--top];
Instack[v] = false;
Belong[v] = scc_clock;
scc[scc_clock].push_back(v);
}while(u != v);
}
} void find(){
memset(low, 0, sizeof(low));
memset(dfn, 0, sizeof(dfn));
memset(Belong, 0, sizeof(Belong));
memset(Stack, 0, sizeof(Stack));
memset(Instack, false, sizeof(Instack));
dfs_clock = scc_clock = top = 0;
for(int i = 1; i <= n; ++i)
if(!dfn[i]) tarjan(i);
} void suodian(){//缩点新建图
for(int i = 1; i <= scc_clock; ++i){
Map[i].clear();
in[i] = 0;
}
for(int i = 0; i < cnt; ++i){
int u = Belong[edge[i].u];
int v = Belong[edge[i].v];
if(u != v){
Map[v].push_back(u);//反向建边
in[u]++;
}
}
} int ans;
void DFS(int u){
vis[u] = true;
ans += scc[u].size();
for(int i = 0; i < Map[u].size(); ++i){
int v = Map[u][i];
if(!vis[v]){
DFS(v);
}
}
} void solve(){
int sum = -1;
for(int i = 1; i <= scc_clock; ++i){
num[i] = 0;
if(in[i] == 0){
memset(vis, false, sizeof(vis));
ans = 0;
DFS(i);
num[i] = ans;
sum = max(num[i], sum);
}
}
printf("Case %d: %d\n", k++, sum - 1);//多算1,要减去1
int flag = 0;
for(int i = 1; i <= n; ++i){
if(num[Belong[i]] == sum){
if(!flag) printf("%d", i - 1);
else printf(" %d", i - 1);
flag = 1;
}
}
printf("\n");
} int main(){
int T;
scanf("%d", &T);
k = 1;
while(T--){
init();
getmap();
find();
suodian();
solve();
}
return 0;
}
HPU 3639--Hawk-and-Chicken【SCC缩点反向建图 && 求传递的最大值】的更多相关文章
- HDU 3639 Hawk-and-Chicken(强连通缩点+反向建图)
http://acm.hdu.edu.cn/showproblem.php?pid=3639 题意: 有一群孩子正在玩老鹰抓小鸡,由于想当老鹰的人不少,孩子们通过投票的方式产生,但是投票有这么一条规则 ...
- HDU 3861--The King’s Problem【scc缩点构图 && 二分匹配求最小路径覆盖】
The King's Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- [CERC2017]Intrinsic Interval[scc+线段树优化建图]
题意 给定一个长度为 \(n\) 的排列,有 \(q\) 次询问,每次询问一个区间 \([l,r]\) ,找到最小的包含 \([l,r]\) 的区间,满足这个区间包含了一段连续的数字. \(n\leq ...
- BZOJ 2707: [SDOI2012]走迷宫 [高斯消元 scc缩点]
2707: [SDOI2012]走迷宫 题意:求s走到t期望步数,\(n \le 10^4\),保证\(|SCC| \le 100\) 求scc缩点,每个scc高斯消元,scc之间直接DP 注意每次清 ...
- cf949C 建模,SCC缩点
/* 给定n个数据中心,m份资料,每份资料在其中的两个中心备份,一天可供下载的时间是h小时 中心i在第hi小时需要维护,无法下载 现在要将一些中心的维护时间往后推1小时,使得任意时刻每份资料都可以被下 ...
- POJ 3126 --Father Christmas flymouse【scc缩点构图 && SPFA求最长路】
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 3007 Accep ...
- bzoj1093: [ZJOI2007]最大半连通子图 scc缩点+dag上dp
一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u的有向路径.若G'=(V ...
- poj 3592 Instantaneous Transference 【SCC +缩点 + SPFA】
Instantaneous Transference Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6204 Accep ...
- HDU 3072--Intelligence System【SCC缩点新构图 && 求连通全部SCC的最小费用】
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
随机推荐
- sqlserver导入dbf文件
select top 10000 * from OPENROWSET('MICROSOFT.JET.OLEDB.4.0','dBase IV;HDR=NO;IMEX=2;DATABASE=E:/日常/ ...
- Spring Cloud Eureka简单入门
步骤: 1.创建父工程 2.创建EurekaServer工程 3.创建EurekaClient工程 父工程pom.xml <?xml version="1.0" encodi ...
- maven学习(十一)——maven中的聚合与继承
一.聚合 如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合 1.1.聚合配置代码 <modules> <module>模块一</module> & ...
- struts ValueStack 详解
一.ValueStack 1.ValueStack是一个接口,在struts2中使用OGNL(Object-Graph Navigation Language)表达式实际上是使用 ...
- knockout,change事件
knockout,change事件 不知道为啥公司要用ko,就这样吧 大概需求,动态绑定的预算类别,然后预算类别切换时候,根据预算类别中的value去找上级编号.
- diea
http://name.vip.int ellig.top/name
- 关于JavaWeb开发的一些感悟
从事JavaWeb的开发已经三年了,从最开始的啥都不会,到慢慢的能够独立做项目,从一开始的一片茫然,到现在的心中有数.对于技术.业务也有了自己的看法. JavaWeb开发所涉及到的知识点非常多,涉及到 ...
- 【bzoj3217】ALOEXT 替罪羊树套Trie树
题目描述 taorunz平时最喜欢的东西就是可移动存储器了……只要看到别人的可移动存储器,他总是用尽一切办法把它里面的东西弄到手. 突然有一天,taorunz来到了一个密室,里面放着一排可移动存储器, ...
- [SPOJ839]Optimal Marks
[SPOJ839]Optimal Marks 试题描述 You are given an undirected graph \(G(V, E)\). Each vertex has a mark wh ...
- codevs 1081 线段树练习 2 区间更新 单点查询 无lazy
题目描述 Description 给你N个数,有两种操作 1:给区间[a,b]的所有数都增加X 2:询问第i个数是什么? 输入描述 Input Description 第一行一个正整数n,接下来n行n ...