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. webpack系统配置

    简言之,webpack 是一个模块打包器 (module bundler),能够将任何资源如 JavaScript 文件.CSS 文件.图片等打包成一个或少数文件. 为什么要用Webpack? 首先, ...

  2. HDU 2077 汉诺塔IV (递推)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2077 还记得汉诺塔III吗?他的规则是这样的:不允许直接从最左(右)边移到最右(左)边(每次移动一定是 ...

  3. ES6知识整理(8)--Promise对象

    (关于promise,以前并不知道是什么,没这个概念.现在来学习总结下) promise含义 es6的异步编程解决方案.需要new新对象操作api. promise对象特点 有3中状态:pending ...

  4. Eloquent JavaScript #12# Handling Events

    索引 Notes onclick removeEventListener Event objects stopPropagation event.target Default actions Key ...

  5. Prometheus监控学习笔记之PromQL操作符

    0x00 二元运算符 Prometheus 的查询语言支持基本的逻辑运算和算术运算.对于两个瞬时向量, 匹配行为可以被改变. 算术二元运算符 在 Prometheus 系统中支持下面的二元算术运算符: ...

  6. Linux学习笔记之Linux环境变量总结

    0x00 概述 Linux是一个多用户多任务的操作系统,可以在Linux中为不同的用户设置不同的运行环境,具体做法是设置不同用户的环境变量. 0x01 Linux环境变量分类 按照生命周期来分,Lin ...

  7. ora-24550 signo=6 signo=11解决

    我们有台测试服务器pro*c/oci应用总是发生各种比较奇葩的现象,就这一台机器会发生,其他几十台都不会发生. sig 11的原因,内存地址访问越界.各signo的si_code含义可参考http:/ ...

  8. 我是这样做APP的:击中用户的痛点(转)

    击中用户的痛点 点评,感觉取名叫做“用户痛点的取舍”更加合适.很多公司.项目的失败完全取决于决策人取舍的失败,一味地追求大而全.迎合上级领导,专断而没有和团队做客观的分析.本文虽然以一个应该来说并不复 ...

  9. applyColorMap()研究(如果我对现有的colormap不满意,那么如何具体来做)

    cv::applyColorMap()能够实现预定义的伪彩色,这个是众所周知的事情. 并且和matlab提供的很相近 除了这些预置的变换,如果我想实现新的变换,需要做LUT变换     cv::Mat ...

  10. darknet中的若干问题

    2018-04-20: https://github.com/pjreddie/darknet/issues/717 改了一下午,然后/usr/include/c++/4.8/bits/stl_rel ...