【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 ...
随机推荐
- hibernate常用配置
核心配置 核心配置有两种方式进行配置 1:属性文件的配置:hibernate.properties 格式:key=value hibernate.connection.driver_class=com ...
- HDU 4162 最小表示法
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4162 题意:给定一个只有0-7数字组成的串.现在要由原串构造出一个新串,新串的构造方法:相邻2个位置的数字 ...
- [工作中的设计模式]建造者模式builder
一.模式解析 建造模式是将复杂的内部创建封装在内部,对于外部调用的人来说,只需要传入建造者和建造工具,对于内部是如何建造成成品的,调用者无需关心. 以上是对建造者模式的官方定义,简单说就是对于复杂对象 ...
- BZOJ 2648 SJY摆棋子 ——KD-Tree
[题目分析] KD-Tree第一题,其实大概就是搜索剪枝的思想,在随机数据下可以表现的非常好NlogN,但是特殊数据下会达到N^2. 精髓就在于估价函数get以及按照不同维度顺序划分的思想. [代码] ...
- Tomcat 配置 Context
在 conf/Catalina/localhost/ 下添加 xml配置文件,文件名和站点名一致. 配置文件示例 <?xml version='1.0' encoding='utf-8'?> ...
- HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...
- POJ1815 Friendship(字典序最小最小割割边集)
看了题解.当时也觉得用邻接矩阵挺好写的,直接memset:然而邻接矩阵不懂得改,于是就放开那个模板,写了Dinic.. 方法是,按字典序枚举每一条满流的边,然后令其容量减1,如果最大流改变了,这条边就 ...
- NUC_TeamTEST_B(贪心)
B - B Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- hbase 问题记录
hbase regionserver挂掉~ org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.hdfs.server.namenode.L ...
- Leetcode Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...