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: ...
随机推荐
- html5 canvas画饼
1. [图片] lxdpie.jpg 2. [文件] lqdpie.html ~ 801B 下载(7) <!DOCTYPE HTML PUBLIC "-//W3C//DTD ...
- 分享知识-快乐自己:Struts2文件上传及文件下载
1)Struts2单文件上传 action:类文件 package com.mlq.action; import com.opensymphony.xwork2.ActionSupport; impo ...
- linux应用之Lamp(apache+mysql+php)的源码安装(centos)
Linux+Apache+Mysql+Php源码安装 一.安装环境: 系统:Centos6.5x64 Apache: httpd-2.4.10.tar.gz Mysql: mysql-5.6.20-l ...
- C#多线程编程介绍——使用thread、threadpool、timer
C#多线程编程介绍——使用thread.threadpool.timer 在system.threading 命名空间提供一些使得能进行多线程编程的类和接口,其中线程的创建有以下三种方法:thread ...
- Eclipse_插件_03_反编译插件_Eclipse Class Decompiler
一.插件优势 此插件比jd-eclipse更加强大,反编译之后不会像jd-eclipse一样出现注释符号. 二.插件下载地址 1.github https://github.com/cnfree/Ec ...
- java面试题07
1.重载和重写的区别? 重载(Overload):(1)方法重载是让类以统一的方式处理不同类型数据的一种手段.多个同名函数同时存在,具有不同的参数个数/类型.重载Overloading是一个类中多态性 ...
- k8s-集群状态及部署一个实例
[root@k8s-master ~]# kubectl get csNAME STATUS MESSAGE ERRORcontroller-manager Healthy ok scheduler ...
- 【Lintcode】153.Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- [转载]Windows网络编程系列教程之四:Select模型
原文:http://www.51see.com/asp/bbs/public/bp_show.asp?t_id=200308131152297103 讲一下套接字模式和套接字I/O模型的区别.先说明一 ...
- Linux命令汇总(二)
1.登录用户设置 新创建了一个用户,用useradd指令,但是发现通过终端无法登陆: echo password | passwd --stdin username 或者 passwd --st ...