链接: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. 时尚创意VI矢量设计模板

    时尚创意VI矢量设计模板 创意VI VI设计 企业VI 时尚背景 信封设计 封面设计 杯子 桌旗 帽子 EPS矢量素材下载 http://www.huiyi8.com/vi/

  2. linux应用之yum命令

    yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器.基於RPM包管理,能够从指定的服务器自动下载RP ...

  3. storm源码剖析(3):topology启动过程

    storm的topology启动过程是执行strom jar topology1.jar MAINCLASS ARG1 ARG2 鉴于前面已经分析了脚本的解析过程,现在重点分析topology1.ja ...

  4. ACM学习历程——POJ3321 Apple Tree(搜索,线段树)

          Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will ...

  5. Python基础入门知识

    本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...

  6. [转]sql where 1=1和 0=1 的作用

    sql where 1=1和 0=1 的作用 原文地址:http://www.cnblogs.com/junyuz/archive/2011/03/10/1979646.html where 1=1; ...

  7. IP简介2

    本笔记介绍java net IP A 类 主要是大型的交换机 1-126  B类 128-191 分配给各个国家的典型的服务器 每个网段支持255*255个设备  每一个B类都可以分配444416个C ...

  8. CF-811A

    A. Vladik and Courtesy time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  9. 7.22实习培训日志-JSP Servlet

    周末总结 今天下午在学习servlet,想自己做一个简单的例子,于是用idea新建一个maven项目,为了后文叙述方便,我们取名为项目1,点击create from archetype,我先选择org ...

  10. C#——委托(1)

    什么是委托?还记得C/C++语言里的函数指针吗?委托就是他的”升级版“.先看一个简单的小程序: # include<stdio.h> typedef int(*Calc)(int a, i ...