The King’s Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2947    Accepted Submission(s): 1049

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条有向路径。现在要建一些州,每个城市属于一个州,如果两个城市u,v可以互相到达,那么u,v属于同一个州。如果u,v在同一个州,那么u可以到达v或者v可以到达u,并且不经过其他州的城市。求最少要建几个州。
思路:因为相互可达的城市属于同一个州,进行tarjan缩点。建立的新图是一个DAG。在一个有向图中,找出最少的路径,使得这些路径经过了所有的点,并且每一条路径经过的点各不相同。这是一种最小路径覆盖问题。

转载一篇苣苣的博客:有向无环图(DAG)的最小路径覆盖

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<set>
using namespace std;
#define PI acos(-1.0)
typedef long long ll;
typedef pair<int,int> P;
const int maxn=1e4+,maxm=1e5+,inf=0x3f3f3f3f,mod=1e9+;
const ll INF=1e13+;
struct edge
{
int from,to;
int cost;
};
edge es[maxm];
priority_queue<P,vector<P>,greater<P> >que;
vector<int>G[maxn],T[maxn];
int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;
stack<int>s;
void dfs(int u)
{
pre[u]=lowlink[u]=++dfs_clock;
s.push(u);
for(int i=; i<G[u].size(); i++)
{
int v=G[u][i];
if(!pre[v])
{
dfs(v);
lowlink[u]=min(lowlink[u],lowlink[v]);
}
else if(!sccno[v])
lowlink[u]=min(lowlink[u],pre[v]);
}
if(lowlink[u]==pre[u])
{
scc_cnt++;
while(true)
{
int x=s.top();
s.pop();
sccno[x]=scc_cnt;
if(x==u) break;
}
}
}
void find_scc(int n)
{
dfs_clock=scc_cnt=;
memset(sccno,,sizeof(sccno));
memset(pre,,sizeof(pre));
for(int i=; i<=n; i++)
if(!pre[i]) dfs(i);
}
void build(int m)
{
for(int i=; i<=scc_cnt; i++) T[i].clear();
for(int i=; i<=m; i++)
{
int u=es[i].from,v=es[i].to;
if(sccno[u]==sccno[v]) continue;
T[sccno[u]].push_back(sccno[v]);
}
}
int cy[maxn],vis[maxn];
bool dfs2(int u)
{
for(int i=; i<T[u].size(); i++)
{
int v=T[u][i];
if(vis[v]) continue;
vis[v]=true;
if(cy[v]==-||dfs2(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+=dfs2(i);
}
return n-ret;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
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);
es[i].from=u,es[i].to=v;
G[u].push_back(v);
}
find_scc(n);
build(m);
cout<<solve(scc_cnt)<<endl;
}
return ;
}

tarjan缩点+最小路径覆盖

 

HDU 3861.The King’s Problem 强联通分量+最小路径覆盖的更多相关文章

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

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

  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(强连通分量缩点+最小路径覆盖)

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

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

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

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

  6. hdu——3861 The King’s Problem

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

  7. POJ 1904 King's Quest 强联通分量+输入输出外挂

    题意:国王有n个儿子,现在这n个儿子要在n个女孩里选择自己喜欢的,有的儿子可能喜欢多个,最后国王的向导给出他一个匹配.匹配有n个数,代表某个儿子和哪个女孩可以结婚.已知这些条件,要你找出每个儿子可以和 ...

  8. HDU 4606 Occupy Cities (计算几何+最短路+二分+最小路径覆盖)

    Occupy Cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

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

随机推荐

  1. 微信小程序---picker

    picker 从底部弹起的滚动选择器,现支持五种选择器,通过mode来区分,分别是普通选择器,多列选择器,时间选择器,日期选择器,省市区选择器,默认是普通选择器. wxml: 普通选择器(mode = ...

  2. node 加载逻辑

    [node 加载逻辑] require(X) from module at path Y . If X is a core module, a. return the core module b. S ...

  3. js实现商品颜色尺码联动以及购买数量的选择

    <script type="text/javascript"> $(function(){ //初始化点击第一个颜色 jquery $("#colors a: ...

  4. vue.js 三种方式安装(转)

    https://blog.csdn.net/m0_37479246/article/details/78836686

  5. pta l2-13(红色警报)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805063963230208 题意:给n个顶点,m条边,问每次删 ...

  6. SSH 连接很慢

    相信很多朋友在使用Linux系统的时候因为安全性的原因摒弃了telnet rlogin 或者 X-window,而把openssh作为自己默认的远程登录方式.然而经常会遇到的一个情况是telnet到s ...

  7. Selenium 定位元素原理,基本API,显示等待,隐式等待,重试机制等等

    Selenium  如何定位动态元素: 测试的时候会遇到元素每次变动的情况,例如: <div id="btn-attention_2030295">...</di ...

  8. jquery 事件委托(利用冒泡)

    将事件绑定在父元素上,格式$(父元素).on("事件名称","子元素选择器",function(方法体){}) <!DOCTYPE html> &l ...

  9. 【js语法】array

    array操作说明 链接:http://www.w3school.com.cn/jsref/jsref_obj_array.asp 函数说明: concat():把两个array连接起来 join() ...

  10. TZOJ 4325 RMQ with Shifts(线段树查询最小,暴力更新)

    描述 In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each que ...