The King’s Problem 强连通
题意 有n个城市 m条有向边
将n个城市分成几个州
1.强连通必定在一个州里
2.州里的任意两个城市 u,v 满足u到v 或者v到u 其一即可
先缩点 然后求最小路就覆盖
#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define pb push_back
#define REP(i,N) for(int i=0;i<(N);i++)
#define CLR(A,v) memset(A,v,sizeof A)
//////////////////////////////////
#define inf 0x3f3f3f3f
const int N=+;
int head[*N],pos;
struct Edge
{
int to,nex;
}edge[*N];
void add(int a,int b)
{
edge[++pos].nex=head[a];
head[a]=pos;
edge[pos].to=b;
}
int low[N],dfn[N],inde,Stack[N],vis[N],tot,cnt,belong[N],out[N]; vector<int>G[N];
int used[N];
void init()
{
CLR(dfn,);
CLR(vis,);
CLR(low,);
CLR(out,);
pos=inde=tot=cnt=;
CLR(head,); }
void tarjan(int x)
{
dfn[x]=low[x]=++tot;
Stack[++inde]=x;
vis[x]=;
for(int i=head[x];i;i=edge[i].nex)
{
int v=edge[i].to;
if(!dfn[v])
{
tarjan(v);
low[x]=min(low[x],low[v]);
}
else if(vis[v])
low[x]=min(low[x],low[v]);
}
if(dfn[x]==low[x])
{
cnt++;int v;
do
{
v=Stack[inde--];
vis[v]=;
belong[v]=cnt;
}
while(v!=x);
}
} bool dfs(int x)
{
if(G[x].size())
rep(j,,G[x].size()-)
{
int t=G[x][j];
if(!used[t])
{
used[t]=;
if(!vis[t]||dfs(vis[t]))
{
vis[t]=x;
return true;
}
}
}
return false;
} int find1(void )
{
int ans=;
CLR(vis,);
rep(i,,cnt)
{
CLR(used,);
if(dfs(i))ans++;
}
return ans;
} int main()
{
int cas;
RI(cas);
while(cas--)
{ int n,m;
RII(n,m);
rep(i,,m)
{
int a,b;RII(a,b);
add(a,b);
}
rep(i,,n)
if(!dfn[i])
tarjan(i);
rep(i,,n)
{
int u=belong[i];
for(int j=head[i];j;j=edge[j].nex)
{
int v=belong[ edge[j].to ];
if(u!=v)
G[u].pb(v);
}
}
cout<<cnt-find1()<<endl;
rep(i,,cnt)
G[i].clear();
init();
}
return ;
}
The King’s Problem 强连通的更多相关文章
- HDU 3861 The King’s Problem 强连通分量 最小路径覆盖
先找出强连通分量缩点,然后就是最小路径覆盖. 构造一个二分图,把每个点\(i\)拆成两个点\(X_i,Y_i\). 对于原图中的边\(u \to v\),在二分图添加一条边\(X_u \to Y_v\ ...
- hdu3861 The King’s Problem 强连通缩点+DAG最小路径覆盖
对多校赛的题目,我深感无力.题目看不懂,英语是能懂的,题目具体的要求以及需要怎么做没有头绪.样例怎么来的都不明白.好吧,看题解吧. http://www.cnblogs.com/kane0526/ar ...
- HDU 3861 The King’s Problem (强连通缩点+DAG最小路径覆盖)
<题目链接> 题目大意: 一个有向图,让你按规则划分区域,要求划分的区域数最少. 规则如下:1.所有点只能属于一块区域:2,如果两点相互可达,则这两点必然要属于同一区域:3,区域内任意两点 ...
- hdoj 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(强连通+二分图最小路径覆盖)
HDU 3861 The King's Problem 题目链接 题意:给定一个有向图,求最少划分成几个部分满足以下条件 互相可达的点必须分到一个集合 一个对点(u, v)必须至少有u可达v或者v可达 ...
- 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【scc缩点构图 && 二分匹配求最小路径覆盖】
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 ...
随机推荐
- java中List、Map、Set、Collection、Stack、Queue等的使用
java中这几个东西是比较常用的,虽然我用的不多,也正是因为用的不多,所以我一直搞不清楚他们之间的具体用法以及相互之间的关系,现在特单独作为一个东西来总结一下. 本文参考一下资料: 1.<jav ...
- yum 使用笔记
yum 重新配置了源以后,用 yum clean all 先clean一下,才能用新的.
- 【转】ruby中nil?, empty? and blank?的选择
In Ruby, you check with nil? if an object is nil:article = nil article.nil? # => true empty? chec ...
- Nginx配置文件中文注释详解
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev ...
- 使用 Github 和 Hexo 快速搭建个人博客
导语 个人兴趣爱好特别广泛,喜欢捣鼓各种小东西自娱自乐.虽然都没能深入研究,但是自己的“孩子”还是很想拿出来遛遛得人一句夸奖的.所以刚学 Markdown 的时候很是有想过要搭个个人博客来玩玩,一来激 ...
- javascript——屏蔽右键快捷菜单
JS: function menufalse(){ return false; } document.oncontextmenu = menufalse; //禁用快捷菜单 Jquery: $(&qu ...
- 初识DDD
DDD强调专注于业务问题域的需要:其专业术语.为何开发该软件的关键原因,以及对于业务来说什么才是成功 问题域涉及你当前正在构建软件的主题领域 DDD强调的是,在致力于为大型复杂业务系统创建软件时,专注 ...
- mysql如何开启远程连接(默认未开启,即使密码正确,仍然无法访问)
mysql如何开启远程连接 | 浏览:1846 | 更新:2015-03-11 20:19 1 2 3 4 5 6 分步阅读 百度经验:jingyan.baidu.com 大家在公司工作中,经常会遇到 ...
- elasticsearch(4) 安装 (两台)
环境: centos7 jdk8 elasticsearch1.7.1 安装JDK 确认现有JDK版本 # java –version 安装以及配置环境变量 # tar zxvf jdk-8u6 ...
- DAY11-MYSQL多表查询
一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 准备表 #建表 create table department( id int, name ) ); create table employ ...