HDU-3639-Hawk-and-Chicken(强连通,缩点,DFS)
链接:https://vjudge.net/problem/HDU-3639
题意:
有n个小朋友在一个班级中,现在要选择班长。收集了小朋友们的意见,一条意见表示为A认为B合适。这个是具备传递性的,A认为B合适,B认为C合适。那么A也会认为C合适。
现在需要提供一份候选人名单,这里面的人,是被最多的人,认为合适的。
思路:
tarjan,缩点,再根据缩点之后的点建立反向的新图,用来求每个人的的票数。
不过在找得票数的时候用记忆化搜索wa了,不知道为啥
代码:
#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
#include <cstdio>
#include <set>
#include <iterator>
#include <cstring>
using namespace std; typedef long long LL;
const int MAXN = 5e3+10;
const int INF = 0x3f3f3f3f; vector<int> G[MAXN];
vector<int> Gr[MAXN];
stack<int> St;
int Dfn[MAXN], Low[MAXN];
int Vis[MAXN], Dis[MAXN];
int Fa[MAXN], Fans[MAXN];
int Num[MAXN];
int n, m;
int times, cnt; void Init()
{
for (int i = 1;i <= n;i++)
G[i].clear(), Fa[i] = i, Gr[i].clear();
memset(Dfn, 0, sizeof(Dfn));
memset(Low, 0, sizeof(Low));
memset(Vis, 0, sizeof(Vis));
memset(Dis, 0, sizeof(Dis));
memset(Num, 0, sizeof(Num));
memset(Fans, -1, sizeof(Fans));
times = cnt = 0;
} void Tarjan(int x)
{
Dfn[x] = Low[x] = ++times;
Vis[x] = 1;
St.push(x);
for (int i = 0;i < G[x].size();i++)
{
int node = G[x][i];
if (Dfn[node] == 0)
{
Tarjan(node);
Low[x] = min(Low[x], Low[node]);
}
else if (Vis[node] == 1)
Low[x] = min(Low[x], Dfn[node]);
}
if (Low[x] == Dfn[x])
{
cnt++;
Num[cnt] = 0;
while (St.top() != x)
{
Num[cnt]++;
Fa[St.top()] = cnt;
Vis[St.top()] = 0;
St.pop();
}
Num[cnt]++;
Fa[St.top()] = cnt;
Vis[St.top()] = 0;
St.pop();
}
} int GetFans(int x)
{
Vis[x] = 1;
int sum = 0;
for (int i = 0;i < Gr[x].size();i++)
{
int node = Gr[x][i];
if (Vis[node] == 1)
continue;
sum += Num[node] + GetFans(node);
}
return sum;
} int main()
{
int t, cn = 0;
cin >> t;
while (t--)
{
cin >> n >> m;
Init();
// cin >> Cost[i];
int l, r;
for (int i = 1;i <= m;i++)
{
scanf("%d%d", &l, &r);
l++, r++;
// cin >> l >> r;
G[l].push_back(r);
}
for (int i = 1;i <= n;++i)
if (!Dfn[i])
Tarjan(i);
for (int i = 1;i <= n;i++)
{
for (int j = 0;j < G[i].size();j++)
{
int node = G[i][j];
int l = Fa[i], r = Fa[node];
if (l != r)
{
++Dis[l];
Gr[r].push_back(l);
}
}
}
int number = -1, is = 0;
for (int i = 1;i <= cnt;i++)
{
if (Dis[i] == 0)
{
memset(Vis, 0, sizeof(Vis));
Fans[i] = Num[i]-1 + GetFans(i);
number = max(number, Fans[i]);
}
}
cout << "Case " << ++cn << ": " << number << endl;
for (int i = 1;i <= n;i++)
{
if (Fans[Fa[i]] == number)
{
if (is++ == 0)
cout << i-1;
else
cout << ' ' << i-1 ;
}
}
cout << endl;
} return 0;
}
HDU-3639-Hawk-and-Chicken(强连通,缩点,DFS)的更多相关文章
- 【HDU 5934】Bomb(强连通缩点)
Problem Description There are N bombs needing exploding. Each bomb has three attributes: exploding r ...
- HDU 3639 Hawk-and-Chicken (强连通缩点+DFS)
<题目链接> 题目大意: 有一群孩子正在玩老鹰抓小鸡,由于想当老鹰的人不少,孩子们通过投票的方式产生,但是投票有这么一条规则:投票具有传递性,A支持B,B支持C,那么C获得2票(A.B共两 ...
- HDU 5934:Bomb(强连通缩点)
http://acm.hdu.edu.cn/showproblem.php?pid=5934 题意:有N个炸弹,每个炸弹有一个坐标,一个爆炸范围和一个爆炸花费,如果一个炸弹的爆炸范围内有另外的炸弹,那 ...
- Codeforces 1027D Mouse Hunt (强连通缩点 || DFS+并查集)
<题目链接> 题目大意: 有n个房间,每个房间都会有一只老鼠.处于第i个房间的老鼠可以逃窜到第ai个房间中.现在要清理掉所有的老鼠,而在第i个房间中防止老鼠夹的花费是ci,问你消灭掉所有老 ...
- HDU 3639 Hawk-and-Chicken(强连通分量+缩点)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013480600/article/details/32140501 HDU 3639 Hawk-a ...
- hdu 4635 Strongly connected 强连通缩点
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4635 题意:给你一个n个点m条边的图,问在图不是强连通图的情况下,最多可以向图中添多少条边,若图为原来 ...
- hdu 2767 Proving Equivalences 强连通缩点
给出n个命题,m个推导,问最少添加多少条推导,能够使全部命题都能等价(两两都能互推) 既给出有向图,最少加多少边,使得原图变成强连通. 首先强连通缩点,对于新图,每一个点都至少要有一条出去的边和一条进 ...
- HDU 3639 Hawk-and-Chicken(良好的沟通)
HDU 3639 Hawk-and-Chicken 题目链接 题意:就是在一个有向图上,满足传递关系,比方a->b, b->c,那么c能够得到2的支持,问得到支持最大的是谁,而且输出这些人 ...
- HDU 1827 Summer Holiday(强连通)
HDU 1827 Summer Holiday 题目链接 题意:中文题 思路:强连通缩点,每一个点的权值为强连通中最小值,然后入度为0的点就是答案 代码: #include <cstdio> ...
- poj2553 强连通缩点
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 10114 Accepted: ...
随机推荐
- RQNOJ 140 分配时间:dp
题目链接:https://www.rqnoj.cn/problem/140 题意: 小王参加的考试是几门科目的试卷放在一起考,一共给t分钟来做. 他现在已经知道第i门科目花k分钟可以拿到w[i][k] ...
- 连接并同步windows下的git仓库
1. 需求 电脑A和电脑B本来通过服务器同步工作目录.服务器时linux系统上有个裸仓库,不管在A上还是B上工作,工作完毕后使用git go与服务器仓库同步.A和B都是windows系统,在工作目录下 ...
- 解决Linux Kettle出现闪退问题
linux环境, 运行sh spoon.sh打开图形化界面时经常出现闪退情况. 报错信息如下: cfgbuilder - Warning: The configuration parameter [o ...
- STL中mem_fun和mem_fun_ref的用法
例如:假设有如下的代码: class Employee { public: int DoSomething(){} } std::vector<Employee> Emps; 假设我们要调 ...
- ACM学习历程——HDU4814 Golden Radio Base(数学递推) (12年成都区域赛)
Description Golden ratio base (GRB) is a non-integer positional numeral system that uses the golden ...
- CodeForces - 123E Maze
http://codeforces.com/problemset/problem/123/E 题目翻译:(翻译来自: http://www.cogs.pw/cogs/problem/problem.p ...
- 【Python】File IO
1.numpy.genfromtxt() numpy.genfromtxt() CSV文件很容易被numpy类库的genfromtxt方法解析 2.
- 闪回之 Flashback Data Archive
背景:Oracle 11g 中 Flashback Data Archive 特性.将变化数据另外存储到创建的闪回归档区(Flashback Archive)中,以和 undo 区别开来,这样就可以为 ...
- sublime插件insertDate显示ISO时间
1 下载insertDate插件以及安装完毕 2 把光标放在想插入ISO时间的地方 3 按住:alt+f5,之后,在sublime下面的Date format string输入:iso.之后按ente ...
- BZOJ2028:[SHOI2009]会场预约(平衡树版)
浅谈\(splay\):https://www.cnblogs.com/AKMer/p/9979592.html 浅谈\(fhq\)_\(treap\):https://www.cnblogs.com ...