链接: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)的更多相关文章

  1. 【HDU 5934】Bomb(强连通缩点)

    Problem Description There are N bombs needing exploding. Each bomb has three attributes: exploding r ...

  2. HDU 3639 Hawk-and-Chicken (强连通缩点+DFS)

    <题目链接> 题目大意: 有一群孩子正在玩老鹰抓小鸡,由于想当老鹰的人不少,孩子们通过投票的方式产生,但是投票有这么一条规则:投票具有传递性,A支持B,B支持C,那么C获得2票(A.B共两 ...

  3. HDU 5934:Bomb(强连通缩点)

    http://acm.hdu.edu.cn/showproblem.php?pid=5934 题意:有N个炸弹,每个炸弹有一个坐标,一个爆炸范围和一个爆炸花费,如果一个炸弹的爆炸范围内有另外的炸弹,那 ...

  4. Codeforces 1027D Mouse Hunt (强连通缩点 || DFS+并查集)

    <题目链接> 题目大意: 有n个房间,每个房间都会有一只老鼠.处于第i个房间的老鼠可以逃窜到第ai个房间中.现在要清理掉所有的老鼠,而在第i个房间中防止老鼠夹的花费是ci,问你消灭掉所有老 ...

  5. HDU 3639 Hawk-and-Chicken(强连通分量+缩点)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013480600/article/details/32140501 HDU 3639 Hawk-a ...

  6. hdu 4635 Strongly connected 强连通缩点

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4635 题意:给你一个n个点m条边的图,问在图不是强连通图的情况下,最多可以向图中添多少条边,若图为原来 ...

  7. hdu 2767 Proving Equivalences 强连通缩点

    给出n个命题,m个推导,问最少添加多少条推导,能够使全部命题都能等价(两两都能互推) 既给出有向图,最少加多少边,使得原图变成强连通. 首先强连通缩点,对于新图,每一个点都至少要有一条出去的边和一条进 ...

  8. HDU 3639 Hawk-and-Chicken(良好的沟通)

    HDU 3639 Hawk-and-Chicken 题目链接 题意:就是在一个有向图上,满足传递关系,比方a->b, b->c,那么c能够得到2的支持,问得到支持最大的是谁,而且输出这些人 ...

  9. HDU 1827 Summer Holiday(强连通)

    HDU 1827 Summer Holiday 题目链接 题意:中文题 思路:强连通缩点,每一个点的权值为强连通中最小值,然后入度为0的点就是答案 代码: #include <cstdio> ...

  10. poj2553 强连通缩点

    The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10114   Accepted: ...

随机推荐

  1. 金色酒类企业dedecms模板

    金色酒类企业dedecms模板,网站模板,dedecms模板. 模板地址:http://www.huiyi8.com/sc/7276.html

  2. 取分组TOPN好理解案例

  3. python输入空格间隔的一行int

    str = input() list = [int(x) for x in str.split()] print(list) 用py刷题肯定得遇到空格间隔的键入,先str接收键入的一行字串,然后把st ...

  4. MySQL交叉表处理_20160923

    交叉表处理,在二维表中例如下面表 想把年月字段放到列字段,在sql中可以使用sum(if(条件,求和字段,null)) 函数来进行行列的转置 1.首先是上篇的年月字段在一列 SELECT city A ...

  5. ACM学习历程——UVA10112 Myacm Triangles(计算几何,多边形与点的包含关系)

    Description   Problem B: Myacm Triangles Problem B: Myacm Triangles Source file: triangle.{c, cpp, j ...

  6. PHP调用Python快速发送高并发邮件

    1 简介 在PHP中发送邮件,通常都是封装一个php的smtp邮件类来发送邮件.但是PHP底层的socket编程相对于Python来说效率是非常低的.CleverCode同时写过用python写的爬虫 ...

  7. Linux 运维面试问题总结

    Linux 运维面试问题总结 1.详细描述mysql主从复制的方式?(类似跟DRBD高可用的协议类型) 答: (1)同步复制(C协议:sync):只有在本地和远程磁盘都确定写入已完成时,主节点才会认为 ...

  8. Docker入门(一):简介

    这个<Docker入门>系列文档,是我根据Docker官网(https://docs.docker.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家 ...

  9. SQLServer中连接个数及超时问题

    超时时间已到.超时时间已到,但是尚未从池中获取连接.出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小. 解决办法1.在代码里面,把未关闭的连接关闭2.扩大共享池,方法如下:解决方法可以是 ...

  10. Maven查找添加方式

    可以通过以下链接 : https://mvnrepository.com/