HDU 3861--The King’s Problem【scc缩点构图 && 二分匹配求最小路径覆盖】
The King’s Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2137 Accepted Submission(s): 763
but can’t go from city v to city u. In order to rule his kingdom more effectively, the king want to divide his kingdom into several states, and each city must belong to exactly one state.What’s more, for each pair of city (u, v), if
there is one way to go from u to v and go from v to u, (u, v) have to belong to a same state.And the king must insure that in each state we can ether go from u to v or go from v to u between every pair of cities (u, v) without passing any city which
belongs to other state.
Now the king asks for your help, he wants to know the least number of states he have to divide the kingdom into.
The first line for each case contains two integers n, m(0 < n <= 5000,0 <= m <= 100000), the number of cities and roads in the kingdom. The next m lines each contains two integers u and v (1 <= u, v <= n), indicating that there is a road going from city u to
city v.
1
3 2
1 2
1 3
2
题意:国王要给n个城市进行规划。分成若干个州。有三点要求:1、有边u到v以及有边v到u,则u,v必须划分到同一个州内。
2、一个州内的两点至少要有一方能到达还有一方。3、一个点仅仅能划分到一个州内。问他至少要建多少州
思路:先把能相互两两到达的点用强连通归为一个州,然后再进行缩点。建立新图。然后用匈牙利算法求出最大匹配,答案=强连通求出的联通块-最大匹配(最小路径覆盖=结点数-最大匹配)。
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#define maxn 50000+100
#define maxm 200000+100
using namespace std;
int n, m; struct node {
int u, v, next;
}; node edge[maxm];
int head[maxn], cnt;
int low[maxn], dfn[maxn];
int dfs_clock;
int Stack[maxn], top;
bool Instack[maxn];
int Belong[maxn];
int scc_clock;
vector<int>Map[maxn]; void init(){
cnt = 0;
memset(head, -1, sizeof(head));
} void addedge(int u, int v){
edge[cnt] = {u, v, head[u]};
head[u] = cnt++;
} void getmap(){
scanf("%d%d", &n, &m);
while(m--){
int a, b;
scanf("%d%d", &a, &b);
addedge(a, b);
}
} void Tarjan(int u, int per){
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){
int v = edge[i].v;
if(!dfn[v]){
Tarjan(v, u);
low[u] = min(low[u], low[v]);
}
else if(Instack[v])
low[u] = min(low[u], dfn[v]);
}
if(dfn[u] == low[u]){
scc_clock++;
do{
v = Stack[--top];
Instack[v] = false;
Belong[v] = scc_clock;
}
while( v != u);
}
} 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(false));
dfs_clock = scc_clock = top = 0;
for(int i = 1; i <= n ; ++i){
if(!dfn[i])
Tarjan(i, i);
}
} void suodian(){
for(int i = 1; i <= scc_clock; ++i)
Map[i].clear();
for(int i = 0; i < cnt; ++i){
int u = Belong[edge[i].u];
int v = Belong[edge[i].v];
if(u != v){
Map[u].push_back(v);
}
}
} int used[maxn], link[maxn]; bool dfs(int x){
for(int i = 0; i < Map[x].size(); ++i){
int y = Map[x][i];
if(!used[y]){
used[y] = 1;
if(link[y] == -1 || dfs(link[y])){
link[y] = x;
return true;
}
}
}
return false;
} void hungary(){
int ans = 0;
memset(link, -1, sizeof(link));
for(int j = 1; j <= scc_clock; ++j){
memset(used, 0, sizeof(used));
if(dfs(j))
ans++;
}
printf("%d\n", scc_clock - ans);
} int main (){
int T;
scanf("%d", &T);
while(T--){
init();
getmap();
find();
suodian();
hungary();
}
return 0;
}
HDU 3861--The King’s Problem【scc缩点构图 && 二分匹配求最小路径覆盖】的更多相关文章
- HDU 3861 The King’s Problem (强连通缩点+DAG最小路径覆盖)
<题目链接> 题目大意: 一个有向图,让你按规则划分区域,要求划分的区域数最少. 规则如下:1.所有点只能属于一块区域:2,如果两点相互可达,则这两点必然要属于同一区域:3,区域内任意两点 ...
- hdu 3861 The King’s Problem trajan缩点+二分图匹配
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 3861 The King’s Problem(强连通+二分图最小路径覆盖)
HDU 3861 The King's Problem 题目链接 题意:给定一个有向图,求最少划分成几个部分满足以下条件 互相可达的点必须分到一个集合 一个对点(u, v)必须至少有u可达v或者v可达 ...
- hdu——3861 The King’s Problem
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 3861 The King’s Problem(强连通分量+最小路径覆盖)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题目大意: 在csdn王国里面, 国王有一个新的问题. 这里有N个城市M条单行路,为了让他的王国 ...
- HDU 3861 The King’s Problem 最小路径覆盖(强连通分量缩点+二分图最大匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 最小路径覆盖的一篇博客:https://blog.csdn.net/qq_39627843/ar ...
- HDU 3861 The King's Problem(强连通分量缩点+最小路径覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=3861 题意: 国王要对n个城市进行规划,将这些城市分成若干个城市,强连通的城市必须处于一个州,另外一个州内的任意 ...
- HDU 3861.The King’s Problem 强联通分量+最小路径覆盖
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 3861 The King’s Problem(tarjan连通图与二分图最小路径覆盖)
题意:给我们一个图,问我们最少能把这个图分成几部分,使得每部分内的任意两点都能至少保证单向连通. 思路:使用tarjan算法求强连通分量然后进行缩点,形成一个新图,易知新图中的每个点内部的内部点都能保 ...
随机推荐
- poj--1664--放苹果(递归好体)
放苹果 Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u Submit Status De ...
- mysql查询orderby
--按某一字段分组取最大(小)值所在行的数据 /* 数据如下: name val memo a 2 a2(a的第二个值) a 1 a1--a的第一个值 a 3 a3:a的 ...
- MATLAB 2018a 下载安装
参考链接:https://www.youtube.com/watch?v=BJavEE9KIlY
- sublime3 install python3
链接地址:https://blog.csdn.net/Ti__iT/article/details/78830040
- html5左右滑动页面效果实现
The Demo of h5 slider achiev by Myself 主要思路: 设置一个容器container,然后里面有几个page,获取到屏幕的宽度并将其赋值给page,然后contai ...
- Centos7下git服务器及gogs部署
1.安装git # yum install -y git 2.创建git用户及组 # groupadd git # adduser git -g git # mkdir /home/git # mkd ...
- matplotlib简介-高质量图形输出
Matplotlib 是一个用来绘制二维图形的 Python 模块,它克隆了许多 Matlab 中的函数, 用以帮助 Python 用户轻松获得高质量(达到出版水平)的二维图形. 文章来源:http: ...
- Rx (Reactive Extensions)
The Reactive Extensions (Rx) is a library for composing asynchronous and event-based programs using ...
- 如何激活优动漫PAINT,获取优动漫PAINT序列号
优动漫PAINT也就是我们常说的clip studio paint(CSP)的中文版本,它是一款功能强大的漫画.插画绘制软件,所有动漫和漫画插件使用帮助你更加便捷的创作有质量的二次元素材,是各位喜欢动 ...
- 再谈json
接上一篇,省市三级联动的例子中,引入了1个QQ网站上的js文件.这个js中构造了一个地址对象,页面上我们所有的操作都跟这个对象关联.今天讨论这种对象怎么构造的问题. 前面写过一篇:浅谈Json数据格式 ...