强连通 HDU 3861
t个样例
n个点m条边
分成一些区
2个点互相能到达必须分在一个区
一个区中任何2个点可以u->v 或者v->u
任何点都要有自己的区
求最小的区的数目
强联通缩点
成新图
二分匹配 求最大匹配
最小路径覆盖=点数-最大匹配数
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<stack> using namespace std; #define MAXN 5010
#define MAXN1 100010
int head[MAXN],dfn[MAXN],low[MAXN],cou[MAXN],fa[MAXN];
int cnt,k,num;
bool vis[MAXN],mark[MAXN]; struct edg
{
int next,to,fr;
}x[MAXN1];
void add(int u,int v)
{
x[cnt].next=head[u];
x[cnt].fr=u;
x[cnt].to=v;
head[u]=cnt++;
}
stack<int>s; void dfs(int u)
{
low[u]=dfn[u]=k++;
vis[u]=;
s.push(u);
int i;
for(i=head[u];i!=-;i=x[i].next)
{
int v=x[i].to;
if(!dfn[v])
{
dfs(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v])
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
num++;
while(!s.empty())
{
int now=s.top();
s.pop();
vis[now]=;
fa[now]=num;
if(now==u)break;
}
}
}
int pa[MAXN]; bool dfs1(int u)
{
int i;
for(i=head[u];i!=-;i=x[i].next)
{
if(mark[x[i].to])
continue;
mark[x[i].to]=;
if(pa[x[i].to]==-||dfs1(pa[x[i].to]))
{
pa[x[i].to]=u;
return ;
}
}
return ; //很重要
}
int main()
{
int t;
scanf("%d",&t); while(t--)
{
int n,m,i;
scanf("%d%d",&n,&m);
cnt=;
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low)); for(i=;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
}
k=;
num=;
for(i=;i<=n;i++) //强联通
if(!dfn[i])
dfs(i);
memset(head,-,sizeof(head));
memset(pa,-,sizeof(pa));
int en=cnt;
cnt=;
for(i=;i<en;i++)
{
int u,v;
u=fa[x[i].fr];
v=fa[x[i].to];
if(u!=v)
{
add(u,v);
}
}
int ans=; for(i=;i<=num;i++) //二分匹配
{
memset(mark,,sizeof(mark));
if(dfs1(i))
ans++;
}
printf("%d\n",num-ans);
} return ;
}
强连通 HDU 3861的更多相关文章
- HDU 3861 The King’s Problem(强连通+二分图最小路径覆盖)
HDU 3861 The King's Problem 题目链接 题意:给定一个有向图,求最少划分成几个部分满足以下条件 互相可达的点必须分到一个集合 一个对点(u, v)必须至少有u可达v或者v可达 ...
- 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 题目大意: 在csdn王国里面, 国王有一个新的问题. 这里有N个城市M条单行路,为了让他的王国 ...
- HDU 3861 The King's Problem(强连通分量缩点+最小路径覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=3861 题意: 国王要对n个城市进行规划,将这些城市分成若干个城市,强连通的城市必须处于一个州,另外一个州内的任意 ...
- HDU 3861 The King’s Problem 强连通分量 最小路径覆盖
先找出强连通分量缩点,然后就是最小路径覆盖. 构造一个二分图,把每个点\(i\)拆成两个点\(X_i,Y_i\). 对于原图中的边\(u \to v\),在二分图添加一条边\(X_u \to Y_v\ ...
- HDU 3861 The King’s Problem (强连通缩点+DAG最小路径覆盖)
<题目链接> 题目大意: 一个有向图,让你按规则划分区域,要求划分的区域数最少. 规则如下:1.所有点只能属于一块区域:2,如果两点相互可达,则这两点必然要属于同一区域:3,区域内任意两点 ...
- 缩点+最小路径覆盖 hdu 3861
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题意:输入t,表示t个样例.接下来每个样例第一行有两个数n,m表示点数和有向边的数量,接下来输入 ...
- 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 3639
t个样例 n个点 m条边 求有手帕最多的人 A->B B->C C 2块 可以传递 先强联通一下 这里的权是强连通分量中有几个点 然后要建一下反图 入度为0的点就有可能是最大的点 #inc ...
随机推荐
- Codeforces Round #371 (Div. 2)E. Sonya and Problem Wihtout a Legend[DP 离散化 LIS相关]
E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...
- ural Infernal Work
Infernal Work Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descr ...
- 精通CSS version2笔记2.小知识
添加背景:background-img:url(URL)+属性; 圆角框:选择器{border-radius:单位;}创建X单位的圆角框在元素周围 投影:box-shadow:垂直和水平偏移.投影的宽 ...
- linux进入软连接所指向的原目录
软连接就是一个快捷方式,建立软连接的方法 ln -s source-path-or-file link-file 建立硬连接 ln source-path-or-file link-file linu ...
- css3属性选择器
- web cache server方案比较:varnish、squid、nginx
linux运维中,web cache server方案的部署是一个很重要的环节,选择也有很多种比如:varnish.squid.nginx.下面就对当下常用的这几个web cache server做一 ...
- 如何将matlab画出的图片保存为要求精度
· 来源:http://emuch.net/bbs/viewthread.php?tid=2705843 杂志社对投稿图片的分辨率通常有如下要求: TIFF: Colour or greyscale ...
- 临时表之IF-ELSE
1.解决输出单列到临时表 场景:存储过程传入id,id为缺省的过滤条件,如果id为0,则查找出tt表中的所有id作为过滤条件 目的:id不为0时,过滤id 解决:用case when来代替if els ...
- CodeGenerator.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CiCe ...
- sqlserver数据库附加分离备份还原命令
--获取所有数据库的名称 select [name] from master.dbo.sysdatabases where [name]='master' --判断数据库是否存在 if exists( ...