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 ...
随机推荐
- 第二篇 Mysql常用操作记录(转载)
我们在创建网站的时候,一般需要用到数据库.考虑到安全性,建议使用非root用户.常用命令如下: 1.新建用户 //登录MYSQL@>mysql -u root -p@>密码//创建用户my ...
- (转)通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号
最近由于项目的需要,需要在程序中获取机器的硬盘序列号和MAC地址等信息,在C#下,可以很容易的获得这些信息,但是在C++程序中感觉比较麻烦.经过百度,发现很多大虾都是通过WMI来获取这些硬件信息的,网 ...
- thinkphp <volist>标签中 <if> 判断的写法
thinkphp <volist>标签中 <if> 判断的写法 <volist name="data" id="vo"> & ...
- Angular5学习笔记 - 虚拟RestfulApi配置与使用(六)
一.安装json-server功能 #windows cnpm install json-server -g #Mac & Linux sudo npm install json-server ...
- Java基础--CountDownLatch
CountDownLatch是线程同步辅助类,它允许一个或多个线程wait直到countdown被调用使count为0. CountDownLatch是在java1.5被引入,存在于java.util ...
- (转)C#用Linq实现DataTable的Group by数据统计
本文转载自:http://www.cnblogs.com/sydeveloper/archive/2013/03/29/2988669.html 1.用两层循环计算,前提条件是数据已经按分组的列排好序 ...
- 二 Istio设计的核心原则
Istio架构关键目标 最大化透明度:Istio将自身自动注入到服务间所有的网络路径中.Istio使用sidecar代理来捕获流量,并且在尽可能的地方自动编程网络层,通过代理来路由流量,无需改动应用程 ...
- spring学习十二 application/x-www-form-urlencoded还是application/json
application/x-www-form-urlencoded还是application/json get. POST 用哪种格式? 后台如何得到这些值? 如何用ajax 或者是 postman ...
- C# Dynamic通用反序列化Json类型并遍历属性比较
背景 : 最近在做JAVA 3D API重写,重写的结果需要与原有的API结果进行比较,只有结果一致时才能说明接口是等价重写的,为此需要做一个API结果比较的工具,比较的内容就是Json内容,但是为了 ...
- 11-28 网页基础--JavaScript(DOM)
网页基础 第二部分--HTMLDOM操作 一.定义:htmlDOM是一种面向对象的树的模型,它包含html中的所有元素:通过html可以找到所有包含在dom中的元素. 二.作用: 1.查找html元素 ...