hdu——3861 The King’s Problem
The King’s Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3244 Accepted Submission(s): 1148
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
问题描述
在寂静的Kingdom,国王遇到了一个新问题。王国里有N个城市,城市之间有许多方向性的道路。这意味着,如果有一条从U到V的道路,你只能从城市到第五城,但不能从第五城到美国城市,为了更有效地统治他的王国,国王想要把他的王国分割成几个州,每个城市必须属于一个州。更重要的是,对于每一对城市(U,V),如果有一种从u到V,从V到u,(u,v)必须属于同一状态的方法。国王必须保证,在每一个州,我们都可以从U到v,或者在每一对城市(u,V)之间从v到u,而不经过任何属于另一个州的城市。
现在国王请求你的帮助,他想知道他必须把王国分成多少个州。
输入
第一行包含一个整数T,测试用例的个数。然后随访t病例。
每个案例的第一行包含两个整数n,m(0<n<= 5000,0 < = M = 100000),在英国的城市和道路的数量。下一行m每一行包含两个整数u和v(1 < u = v,v=n),表示有一条从城市u到城市v的道路。
输出
输出应该包含T线。对于每个测试用例,您只需输出一个整数,这是国王必须划分的最小数目。
题意是说一个有向图将n个点划分成最少的集合个数,要求任意两个相互到达的点在同一个几何中,且属于同一个集合的任意俩个点对(u,v),至少存在一条路径,使得v对于u 可达 或者 u 对于v 可达。首先要缩点求集合的个数,然后重新构图,任意俩点,只要在同一条有向路径上,则可以属于一个集合,所以要在缩点后的图上用二分匹配找最小路径覆盖即可。
最小路径覆盖=点数-最大匹配。这里点数是缩点之后的点数。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 5100
using namespace std;
bool vis[N];
int n,m,t,x,y,tot,tim,ans,sum,top;
],head1[],girl[N],stack[N],belong[N];
struct Edge
{
int to,from,next;
}edge[];
int add(int x,int y)
{
tot++;
edge[tot].to=y;
edge[tot].next=head[x];
head[x]=tot;
}
struct Edde
{
int to,from,next;
}edde[];
int add1(int x,int y)
{
tot++;
edde[tot].to=y;
edde[tot].next=head1[x];
head1[x]=tot;
}
int read()
{
,f=; char ch=getchar();
; ch=getchar();}
+ch-'; ch=getchar();}
return x*f;
}
int tarjan(int now)
{
dfn[now]=low[now]=++tim;
stack[++top]=now,vis[now]=true;
for(int i=head[now];i;i=edge[i].next)
{
int t=edge[i].to;
if(vis[t]) low[now]=min(low[now],dfn[t]);
else if(!dfn[t]) tarjan(t),low[now]=min(low[now],low[t]);
}
if(low[now]==dfn[now])
{
sum++,belong[now]=sum;
for(;stack[top]!=now;top--)
belong[stack[top]]=sum,vis[stack[top]]=false;
vis[now]=false,top--;
}
}
int shink_point()
{
tot=;
;i<=n;i++)
for(int j=head[i];j;j=edge[j].next)
if(belong[i]!=belong[edge[j].to])
add1(belong[i],belong[edge[j].to]);
}
int find(int x)
{
for(int i=head1[x];i;i=edde[i].next)
if(!vis[edde[i].to])
{
int t=edde[i].to;
vis[t]=true;
||find(girl[t])) {girl[t]=x; ;}
}
;
}
int begin()
{
top=,tim=,tot=,ans=,sum=;
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(vis,,sizeof(vis));
memset(head,,sizeof(head));
memset(girl,-,sizeof(girl));
memset(head1,,sizeof(head1));
memset(stack,,sizeof(stack));
memset(belong,,sizeof(belong));
}
int main()
{
t=read();
while(t--)
{
n=read(),m=read();
begin();
;i<=m;i++)
x=read(),y=read(),add(x,y);
;i<=n;i++)
if(!dfn[i]) tarjan(i);
shink_point();
;i<=sum;i++)
{
memset(vis,,sizeof(vis));
ans+=find(i);
}
printf("%d\n",sum-ans);
}
}
hdu——3861 The King’s Problem的更多相关文章
- HDU 3861 The King’s Problem(强连通+二分图最小路径覆盖)
HDU 3861 The King's Problem 题目链接 题意:给定一个有向图,求最少划分成几个部分满足以下条件 互相可达的点必须分到一个集合 一个对点(u, v)必须至少有u可达v或者v可达 ...
- 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 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 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
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- HDU 3861 The King’s Problem(tarjan连通图与二分图最小路径覆盖)
题意:给我们一个图,问我们最少能把这个图分成几部分,使得每部分内的任意两点都能至少保证单向连通. 思路:使用tarjan算法求强连通分量然后进行缩点,形成一个新图,易知新图中的每个点内部的内部点都能保 ...
随机推荐
- http://blog.chinaunix.net/uid-9845710-id-1996675.html snmpd配置
http://blog.chinaunix.net/uid-9845710-id-1996675.html http://lihuipeng.blog.51cto.com/3064864/643960 ...
- JSP 错误处理方法
web.xml中配置error-page标签 1.WEB工程中打开 web.xml 文件
- Javascript异步编程的常用方法
Javascript语言的执行环境是"单线程"(single thread).所谓"单线程",就是指一次只能完成一件任务.如果有多个任务,就必须排队,前面一个任 ...
- 微软将于12月起开始推送Windows 10 Mobile
[环球科技报道 记者 陈薇]据瘾科技网站10月8日消息,根据微软Lumia官方Faceboo发布的消息,新版系统Windows 10 Mobile 将会12月起陆续开始推送. 推送的具体时程根据地区. ...
- phpstorm 格式化代码
MAC 安装phpcs.phpcbf composer global require 'squizlabs/php_codesniffer=*' Changed current directory t ...
- 微信小程序开发系列四:微信小程序之控制器的初始化逻辑
微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 这个教程的前两篇文章,介绍了如何 ...
- MySQL-01 MySQL数据库安装指南
学习要点 MySQL数据库的安装和设置 下载mysql mysql官网:https://www.mysql.com/downloads/ 主要版本: Oracle MySQL Cloud Servic ...
- 解决Invalid bound statement (not found)(Mybatis的Mapper绑定问题)
一.问题描述 使用mybatis的项目在本地可以正常运行,但当使用maven或Jenkins打包部署到服务器上时出现了绑定错误,异常信息为: org.apache.ibatis.binding.Bin ...
- IOS学习笔记37——ViewController生命周期详解
在我之前的学习笔记中讨论过ViewController,过了这么久,对它也有了新的认识和体会,ViewController是我们在开发过程中碰到最多的朋友,今天就来好好认识一下它.ViewContro ...
- 关于python字符串拼接的几种方法
当时看完python的基本语法后 给朋友写了个美元概率换算 写完后拼接结果时候 发现压根不知道python怎么拼接字符串 看了些资料自己做了个总结 首先就是和JavaScript一样的拼接方式 nam ...