hdu 3861 The King’s Problem trajan缩点+二分图匹配
The King’s Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Now the king asks for your help, he wants to know the least number of states he have to divide the kingdom into.
The first line for each case contains two integers n, m(0 < n <= 5000,0 <= m <= 100000), the number of cities and roads in the kingdom. The next m lines each contains two integers u and v (1 <= u, v <= n), indicating that there is a road going from city u to city v.
3 2
1 2
1 3
题意:给你n个点,m条边,可以将一个单联通分量缩成一个点,最少能分成几个点;
思路:先将强连通分量缩点,强连通肯定是可以合并成一个点,然后求无环DAG图的最小路径覆盖即可;
#include<iostream>
#include<cstdio>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<bitset>
using namespace std;
#define LL unsigned long long
#define pi (4*atan(1.0))
#define eps 1e-4
#define bug(x) cout<<"bug"<<x<<endl;
const int N=5e3+,M=1e5+,inf=1e9+;
const LL INF=1e18+,mod=; struct is
{
int u,v;
int next;
}edge[M];
int head[N];
int belong[N];
int dfn[N];
int low[N];
int stackk[N<<];
int instack[N];
int number[N];
int n,m,jiedge,lu,bel,top;
void update(int u,int v)
{
jiedge++;
edge[jiedge].u=u;
edge[jiedge].v=v;
edge[jiedge].next=head[u];
head[u]=jiedge;
}
void dfs(int x)
{
dfn[x]=low[x]=++lu;
stackk[++top]=x;
instack[x]=;
for(int i=head[x];i;i=edge[i].next)
{
if(!dfn[edge[i].v])
{
dfs(edge[i].v);
low[x]=min(low[x],low[edge[i].v]);
}
else if(instack[edge[i].v])
low[x]=min(low[x],dfn[edge[i].v]);
}
if(low[x]==dfn[x])
{
int sum=;
bel++;
int ne;
do
{
sum++;
ne=stackk[top--];
belong[ne]=bel;
instack[ne]=;
}while(x!=ne);
number[bel]=sum;
}
}
void tarjan()
{
memset(dfn,,sizeof(dfn));
bel=lu=top=;
for(int i=;i<=n;i++)
if(!dfn[i])
dfs(i);
}
void init()
{
memset(head,,sizeof(head));
jiedge=;
}
vector<int> g[N];
int cy[N];
bool vis[N];
bool dfs1(int u){
for(int i=; i<g[u].size(); ++i){
int v = g[u][i];
if(vis[v]) continue;
vis[v] = true;
if(cy[v]==- || dfs1(cy[v])){
cy[v] = u;
return true;
}
}
return false;
}
int solve(int n){
int ret = ;
memset(cy, -, sizeof(cy));
for(int i=;i<=n;++i){
memset(vis, , sizeof(vis));
ret += dfs1(i);
}
return n - ret;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
g[i].clear();
for(int i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
update(u,v);
}
tarjan();
for(int i=;i<=jiedge;i++)
{
if(belong[edge[i].v]!=belong[edge[i].u])
{
g[belong[edge[i].u]].push_back(belong[edge[i].v]);
}
}
int ans=solve(bel);
printf("%d\n",ans);
}
return ;
}
hdu 3861 The King’s Problem trajan缩点+二分图匹配的更多相关文章
- HDU 3861 The King’s Problem (强连通缩点+DAG最小路径覆盖)
<题目链接> 题目大意: 一个有向图,让你按规则划分区域,要求划分的区域数最少. 规则如下:1.所有点只能属于一块区域:2,如果两点相互可达,则这两点必然要属于同一区域:3,区域内任意两点 ...
- HDU 3861 The King’s Problem(强连通+二分图最小路径覆盖)
HDU 3861 The King's Problem 题目链接 题意:给定一个有向图,求最少划分成几个部分满足以下条件 互相可达的点必须分到一个集合 一个对点(u, v)必须至少有u可达v或者v可达 ...
- 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.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
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 最小路径覆盖的一篇博客:https://blog.csdn.net/qq_39627843/ar ...
- HDU 3861 The King's Problem(强连通分量缩点+最小路径覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=3861 题意: 国王要对n个城市进行规划,将这些城市分成若干个城市,强连通的城市必须处于一个州,另外一个州内的任意 ...
- HDU 3861 The King’s Problem(强连通分量+最小路径覆盖)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题目大意: 在csdn王国里面, 国王有一个新的问题. 这里有N个城市M条单行路,为了让他的王国 ...
- hdu 3861 The King’s Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
随机推荐
- 翻唱 - shape of you - 个个版本
翻唱: http://7j1xky.com1.z0.glb.clouddn.com/1525514286196.mp4 乐队版-我的翻唱-混合 http://7j1xky.com1.z0.glb.cl ...
- Solr基本操作
/update 使用/update进行索引维护,进入Solr管理界面SolrCore下的Document下: 我们进行更新操作可以用json和xml多种格式,这里以xml格式为例说明.先来看看界面上的 ...
- roon
http://www.sthifi.com/Article/ViewArticle.asp?id=10895 http://kb.roonlabs.com/LinuxInstall https://c ...
- JavaScript修改元素
案例1 删除元素 如需删除 HTML 元素,需要清楚该元素的父元素 该js函数代码如下 function remove(){ var parent=document.getElementById(&q ...
- linux判断日志文件大小进行清理
脚本写了一个死循环,根据nohup产生的日志多大, 这里表示日志超过500M之后清理, 具体数字可自定义 睡眠数可自定义 #!/usr/bin/bash while true do s=`du -k ...
- 20145320《WEB基础实践》
20145320WEB基础实践 实验问题回答 1.什么是表单 表单可以收集用户的信息和反馈意见,是网站管理者与浏览者之间沟通的桥梁. 一个表单有三个基本组成部分: 表单标签 表单域:包含了文本框.密码 ...
- tf.nn.max_pool
tf.nn.max_pool(value, ksize, strides, padding, name=None) 参数是四个,和卷积很类似: Args Annotation 第一个参数value ...
- Codeforces 711D Directed Roads - 组合数学
ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it co ...
- Auth组件,Forms组件
一.Auth组件默认auth_user表常用操作 #1.配置settings,使django与数据库连接 DATABASES = { 'default': { 'ENGINE': 'django.db ...
- vector at()函数比 []运算符操作安全
转载:https://blog.csdn.net/chenjiayi_yun/article/details/18507659 []操作符的源码 reference operator[](size_t ...