The King’s Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Problem Description
In the Kingdom of Silence, the king has a new problem. There are N cities in the kingdom and there are M directional roads between the cities. That means that if there is a road from u to v, you can only go from city u to city v, but can’t go from city v to city u. In order to rule his kingdom more effectively, the king want to divide his kingdom into several states, and each city must belong to exactly one state. What’s more, for each pair of city (u, v), if there is one way to go from u to v and go from v to u, (u, v) have to belong to a same state. And the king must insure that in each state we can ether go from u to v or go from v to u between every pair of cities (u, v) without passing any city which belongs to other state.
  Now the king asks for your help, he wants to know the least number of states he have to divide the kingdom into.
 
Input
The first line contains a single integer T, the number of test cases. And then followed T cases.

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.

 
Output
The output should contain T lines. For each test case you should just output an integer which is the least number of states the king have to divide into.
 
Sample Input
1
3 2
1 2
1 3
 
Sample Output
2
 
Source

题意:给你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缩点+二分图匹配的更多相关文章

  1. HDU 3861 The King’s Problem (强连通缩点+DAG最小路径覆盖)

    <题目链接> 题目大意: 一个有向图,让你按规则划分区域,要求划分的区域数最少. 规则如下:1.所有点只能属于一块区域:2,如果两点相互可达,则这两点必然要属于同一区域:3,区域内任意两点 ...

  2. HDU 3861 The King’s Problem(强连通+二分图最小路径覆盖)

    HDU 3861 The King's Problem 题目链接 题意:给定一个有向图,求最少划分成几个部分满足以下条件 互相可达的点必须分到一个集合 一个对点(u, v)必须至少有u可达v或者v可达 ...

  3. 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 ...

  4. HDU 3861.The King’s Problem 强联通分量+最小路径覆盖

    The King’s Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. hdu——3861 The King’s Problem

    The King’s Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. HDU 3861 The King’s Problem 最小路径覆盖(强连通分量缩点+二分图最大匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 最小路径覆盖的一篇博客:https://blog.csdn.net/qq_39627843/ar ...

  7. HDU 3861 The King's Problem(强连通分量缩点+最小路径覆盖)

    http://acm.hdu.edu.cn/showproblem.php?pid=3861 题意: 国王要对n个城市进行规划,将这些城市分成若干个城市,强连通的城市必须处于一个州,另外一个州内的任意 ...

  8. HDU 3861 The King’s Problem(强连通分量+最小路径覆盖)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题目大意: 在csdn王国里面, 国王有一个新的问题. 这里有N个城市M条单行路,为了让他的王国 ...

  9. hdu 3861 The King’s Problem

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

随机推荐

  1. 算法训练 P0505

    一个整数n的阶乘可以写成n!,它表示从1到n这n个整数的乘积.阶乘的增长速度非常快,例如,13!就已经比较大了,已经无法存放在一个整型变量中:而35!就更大了,它已经无法存放在一个浮点型变量中.因此, ...

  2. What Would you Find out about MS908CV ?

    The Autel MaxiSYS commercial car diagnostics scan device, No. MS908CV, performs increased technique ...

  3. 大数据学习路线:Zookeeper集群管理与选举

    大数据技术的学习,逐渐成为很多程序员的必修课,因为趋势也是因为自己的职业生涯.在各个技术社区分享交流成为很多人学习的方式,今天很荣幸给我们分享一些大数据基础知识,大家可以一起学习! 1.集群机器监控 ...

  4. spark与kafka集成进行实时 nginx代理 这种sdk埋点 原生日志实时解析 处理

    日志格式202.108.16.254^A1546795482.600^A/cntv.gif?appId=3&areaId=8213&srcContId=2535575&area ...

  5. easyui form提交和formdata提交记录

    1  easyui form提交 $('form').form('submit',{ url:''; onSubmit:''; success:function(data){ //这种方法获取到的da ...

  6. Oracle之表的相关操作

    #添加字段 格式: alter table table_name add column_name datatype; 例子: alter table userinfo ); desc userinfo ...

  7. kivy 小demo

    from kivy.lang.builder import Builder from kivy.uix.boxlayout import BoxLayout from kivy.app import ...

  8. 一个用python简单的封装了aria2的jsonrpc中adduri的脚本

    aria2是一个十分牛逼的下载神器,有时候项目需要一个很牛逼的下载中间件的话,aria2是一个不错的选择.其中支持jsonrpc和websocket的特性尤其诱人.但是python用起来还是有点不爽, ...

  9. Java学习笔记之Linux下的Java安装和配置

    0x00 概述 由于使用 yum 或者 apt-get 命令 安装 openjdk 可能存在类库不全,从而导致用户在安装后运行相关工具时可能报错的问题,所以此处我们推荐采用手动解压安装的方式来安装 J ...

  10. 深入理解softmax函数

    Softmax回归模型,该模型是logistic回归模型在多分类问题上的推广,在多分类问题中,类标签  可以取两个以上的值.Softmax模型可以用来给不同的对象分配概率.即使在之后,我们训练更加精细 ...