【HDU3861 强连通分量缩点+二分图最小路径覆盖】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861
题目大意:一个有向图,让你按规则划分区域,要求划分的区域数最少。
规则如下:1、有边u到v以及有边v到u,则u,v必须划分到同一个区域内。2、一个区域内的两点至少要有一方能到达另一方。3、一个点只能划分到一个区域内。
解题思路:根据规则1可知必然要对强连通分量进行缩点,缩点后变成了一个弱连通图。根据规则2、3可知即是要求图的最小路径覆盖。
定义:
最小路径覆盖:在图中找一些路径(路径数最少),使之覆盖了图中所有的顶点,且每个顶点有且仅和一条路径有关联。
最小顶点覆盖:在图中找一些点(顶点数最少),使之覆盖了图中所有的边,每条边至少和一个顶点有关联。
二分图:最小顶点覆盖=最大匹配数。
最小路径覆盖=顶点数-最大匹配数。
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std; const int maxn=;
int dfn[maxn], low[maxn], stack[maxn], belong[maxn], visit[maxn], match[maxn];
bool instack[maxn];
int top, scnt, Index, n, m, T;
vector<int>vt[maxn]; struct Node
{
int u, v;
}f[*maxn]; void Init_tarjan()
{
top=scnt=Index=;
for(int i=; i<=n; i++) dfn[i]=low[i]=instack[i]=;
} void tarjan(int u)
{
stack[++top]=u;
dfn[u]=low[u]=++Index;
instack[u]=;
for(int i=; i<vt[u].size(); i++)
{
int v=vt[u][i];
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
{
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u])
{
int v;
scnt++;
do
{
v=stack[top--];
instack[v]=;
belong[v]=scnt;
}
while(u!=v);
}
} bool find(int u)
{
for(int i=; i<vt[u].size(); i++)
{
int v=vt[u][i];
if(!visit[v])
{
visit[v]=;
if(match[v]==-||find(match[v]))
{
match[v]=u;
return true;
}
}
}
return false;
} int hungary()
{
int cnt=;
memset(match,-,sizeof(match));
for(int i=; i<=scnt; i++)
{
for(int j=; j<=scnt; j++) visit[j]=;
if(find(i)) cnt++;
}
return cnt;
} int main()
{
cin >> T;
while(T--)
{
cin >> n >> m;
for(int i=; i<=n; i++) vt[i].clear();
for(int i=; i<m; i++)
{
scanf("%d%d",&f[i].u,&f[i].v);
vt[f[i].u].push_back(f[i].v);
}
Init_tarjan();
for(int i=; i<=n; i++)
if(!dfn[i]) tarjan(i);
for(int i=; i<=n; i++) vt[i].clear();
for(int i=; i<m; i++)
{
int u=belong[f[i].u], v=belong[f[i].v];
if(u==v) continue;
vt[u].push_back(v);
}
int ans=hungary();
cout << scnt-ans <<endl;
}
return ;
} /*
10
10 11
1 2
2 3
3 1
3 4
5 6
6 7
7 5
4 5
10 9
9 8
8 4
2
*/
【HDU3861 强连通分量缩点+二分图最小路径覆盖】的更多相关文章
- hdu3861 强连通分量缩点+二分图最最小路径覆盖
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- [bzoj2150]部落战争_二分图最小路径覆盖
部落战争 bzoj-2150 题目大意:题目链接. 注释:略. 想法: 显然是最小路径覆盖,我们知道:二分图最小路径覆盖等于节点总数-最大匹配. 所以我们用匈牙利或者dinic跑出最大匹配,然后用总结 ...
- Taxi Cab Scheme POJ - 2060 二分图最小路径覆盖
Running a taxi station is not all that simple. Apart from the obvious demand for a centralised coord ...
- HDU 3861 The King’s Problem 最小路径覆盖(强连通分量缩点+二分图最大匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 最小路径覆盖的一篇博客:https://blog.csdn.net/qq_39627843/ar ...
- hdu3861 The King’s Problem 强连通缩点+DAG最小路径覆盖
对多校赛的题目,我深感无力.题目看不懂,英语是能懂的,题目具体的要求以及需要怎么做没有头绪.样例怎么来的都不明白.好吧,看题解吧. http://www.cnblogs.com/kane0526/ar ...
- HDU 3861 The King’s Problem(强连通+二分图最小路径覆盖)
HDU 3861 The King's Problem 题目链接 题意:给定一个有向图,求最少划分成几个部分满足以下条件 互相可达的点必须分到一个集合 一个对点(u, v)必须至少有u可达v或者v可达 ...
- HDU 3861 The King’s Problem (强连通缩点+DAG最小路径覆盖)
<题目链接> 题目大意: 一个有向图,让你按规则划分区域,要求划分的区域数最少. 规则如下:1.所有点只能属于一块区域:2,如果两点相互可达,则这两点必然要属于同一区域:3,区域内任意两点 ...
- HDU 3861 The King’s Problem(tarjan连通图与二分图最小路径覆盖)
题意:给我们一个图,问我们最少能把这个图分成几部分,使得每部分内的任意两点都能至少保证单向连通. 思路:使用tarjan算法求强连通分量然后进行缩点,形成一个新图,易知新图中的每个点内部的内部点都能保 ...
- hdu 1151 Air Raid(二分图最小路径覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=1151 Air Raid Time Limit: 1000MS Memory Limit: 10000K To ...
随机推荐
- Java反射详解及应用示例
反射是Java中最重要的内容之一,了解反射原理对我们学习各种框架具有很大的帮助 反射的原理: 反射应用示例: import java.lang.reflect.Constructor; import ...
- hdu 5692 Snacks 线段树+dfs
Snacks Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- Liferay 6.2 改造系列之一:源码编译和服务启动
一.导入源码 源码可以从Liferay的官方网站上下载.http://www.liferay.com/downloads/liferay-portal/available-releases 下载后解压 ...
- ember.js:使用笔记2-数据删除与存储
在模版中写好响应操作触发的action之后,可以在controller:actions中设置了,需要注意的是对数据的操作一般都是对单个object进行操作,所以先要使用笔记1中的方法使用ObjectC ...
- python 线程之_thread
python 线程之_thread _thread module: 基本用法: def child(tid): print("hello from child",tid) _thr ...
- C#函数与SQL储存过程
一点点小认识作为memo,求指正. C#的函数与SQL的储存过程有很多的相似性, 它们都是一段封闭的代码块,来提高代码的重用性,虽然现在复制粘贴很方便,但是我们在写多个函数的时候频繁的复制粘贴相同的内 ...
- canvas 3D运动球效果 多球
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- CSS 样式使用
[attribute] 用于选取带有指定属性的元素. [attribute=value] 用于选取带有指定属性和值的元素. [attribute~=value] 用于选取属性值中包含指定词汇的元素. ...
- checkbox 的全选与全不选
checkbox 的全选与全不选 只需要调用 cekAll.check();方法,这个方法接收两个参数: 参数一: 全选按钮的 id 以字符串的形式写 参数二: 其他 checkbox 的 name ...
- 链表 UVA 11988 Broken Keyboard (a.k.a. Beiju Text)
题目传送门 题意:训练指南P244 分析:链表模拟,维护链表的head和tail指针 #include <bits/stdc++.h> using namespace std; const ...