hdu—3861(tarjan+二分图)
题意:给你n个城市,每个城市之间有一条有向边,将城市划分为几个区域,问你最小的划分方法,
划分规则为:能相互到达的放在一个区域;然后区域内的a,b两点肯定存在某种方式,使得a能到b或者b能到a(注意,这里没说一定是相互能到);
解题思路:这道题其实就是DAG上的对应二分图的最小路径覆盖;
因为DAG,所以先缩点,然后对应二分图的最小路径覆盖=顶点数-对应二分图的最大匹配;
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#define N 5005
#define M 100005
using namespace std;
struct node
{
int x;
int y;
}a[M];
struct Edge
{
int next;
int to;
}edge[M];
int ans=0;
int match[M];
int head[M];
int instack[M];
int visit[M];
int low[M];
int dfn[M];
int sccno[M];
int book[M];
int cnt;
int step;
int index;
int scc_cnt;
bool e[N][N];
int n,m;
vector<int>scc[N];
void add(int u,int v)
{
edge[cnt].next=head[u];
edge[cnt].to=v;
head[u]=cnt++;
}
void tarjan(int u)
{
low[u]=dfn[u]=++step;
visit[u]=1;
instack[++index]=u;
for(int i=head[u];i!=-1;i=edge[i].next)
{
if(!dfn[edge[i].to])
{
tarjan(edge[i].to);
low[u]=min(low[u],low[edge[i].to]);
}
else if(visit[edge[i].to])
{
low[u]=min(low[u],dfn[edge[i].to]);
}
}
if(low[u]==dfn[u])
{
scc_cnt++;
scc[scc_cnt].clear();
do
{
scc[scc_cnt].push_back(instack[index]);
sccno[instack[index]]=scc_cnt;
visit[instack[index]]=0;
index--;
}
while(u!=instack[index+1]);
}
return;
} bool dfs(int u)
{
for(int i=1;i<=scc_cnt;i++)
{
if(book[i]==0&&e[u][i])
{
book[i]=1;
if(match[i]==-1||dfs(match[i]))
{
match[i]=u;
return true;
}
}
}
return false;
}
void init()
{
memset(instack,0,sizeof(instack));
memset(match,-1,sizeof(match));
memset(e,0,sizeof(e));
memset(visit,0,sizeof(visit));
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(head,-1,sizeof(head));
memset(book,0,sizeof(book));
cnt=step=scc_cnt=index=ans=0;
}
int main()
{
int t;
init();
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
add(a[i].x,a[i].y);
}
for(int i=1;i<=n;i++)
{
if(!dfn[i])
tarjan(i);
}
for(int i=1;i<=m;i++)
{
if(sccno[a[i].x]!=sccno[a[i].y])
{
e[sccno[a[i].x]][sccno[a[i].y]]=1;
// e[sccno[a[i].y]][sccno[a[i].x]]=1;
}
}
for(int i=1;i<=scc_cnt;i++)
{
memset(book,0,sizeof(book));
if(dfs(i))
ans++;
}
printf("%d\n",scc_cnt-ans);
init();
}
return 0;
}
hdu—3861(tarjan+二分图)的更多相关文章
- HDU 3861 The King’s Problem(强连通+二分图最小路径覆盖)
HDU 3861 The King's Problem 题目链接 题意:给定一个有向图,求最少划分成几个部分满足以下条件 互相可达的点必须分到一个集合 一个对点(u, v)必须至少有u可达v或者v可达 ...
- HDU 3861 The King’s Problem(tarjan连通图与二分图最小路径覆盖)
题意:给我们一个图,问我们最少能把这个图分成几部分,使得每部分内的任意两点都能至少保证单向连通. 思路:使用tarjan算法求强连通分量然后进行缩点,形成一个新图,易知新图中的每个点内部的内部点都能保 ...
- 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 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(tarjan缩点+最小路径覆盖:sig-最大二分匹配数,经典题)
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 缩点+最小路径覆盖 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 3861 The King’s Problem(强连通分量+最小路径覆盖)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题目大意: 在csdn王国里面, 国王有一个新的问题. 这里有N个城市M条单行路,为了让他的王国 ...
- hdu 2119(简单二分图) Matrix
http://acm.hdu.edu.cn/showproblem.php?pid=2119 一个由0和1构成的矩阵,每次选取一行或者一列将其中的1变成0,求最小删除次数 简单的二分图应用,矩阵的横坐 ...
随机推荐
- springboot 集成 jpa/hibernate
pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- 重构JS代码 - 让JS代码平面化
js中的嵌套函数用的很多,很牛叉,那为何要平面化? 易懂(自己及他人) 易修改(自己及他人) 平时Ajax调用写法(基于jQuery) $.post('url', jsonObj, function ...
- Google 宣布在 4 月 1 日关闭站内搜索
今春,Google 计划终止又一项产品,它就是“站内搜索”(Site Search)功能.这项产品主要出售给 web 出版商,让它们可以在自家网站内运用业内领先的搜索技术.虽然该公司并未公开宣布此事, ...
- SQL 显示表名显示列名
显示表名:show 表名: 显示列(Field)名:show columns from 表名:
- K进制数
题目描述 考虑包含N位数字的K-进制数. 定义一个数有效, 如果其K-进制表示不包含两连续的0. 考虑包含N位数字的K-进制数. 定义一个数有效, 如果其K-进制表示不包含两连续的0. 例: 1010 ...
- D. Nastya Is Buying Lunch
链接 [https://codeforces.com/contest/1136/problem/D] 题意 有N个人,a[i]表示第i个人的编号,m个二元组. 当前一个在后一个的前面一个位置时二者可以 ...
- Day1 Numerical simulation of optical wave propagation之标量衍射理论基本原理(一)
<Numerical simulation of optical wave propagation>内容 1. 介绍光波传输的基础理论.离散采样方法.基于MATLAB平台的编码实例以及具 ...
- 我们为什么要使用List和Set(List,Set详解)
1.集合概述 类图 集合和数组的区别? 集合基本方法 集合特有的遍历方式? public static void main(String[] args) { //创建集合对象 Collection c ...
- 迁移 VMware 虚拟机到 KVM
虚拟机转换| VMware vCenter Converterhttps://www.vmware.com/cn/products/converter.html 迁移 VMware 虚拟机到 KVMh ...
- vue-router的简单实现原理
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...