首页 技术 正文
技术 2022年11月10日
0 收藏 667 点赞 2,274 浏览 1713 个字

题目链接: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
*/
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295