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. lunux开放80端口(本地访问不了linux文件可能是这个原因)

    /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT #开启80端口  /etc/rc.d/init.d/iptables save #保存配置  / ...

  2. shell脚本-删除当天日期前3个月的数据表

    #!/bin/bash #author:skycheng #get current date string datestr=`date +'%Y-%m-%d'` start_time=`date +' ...

  3. java中继承thread类的其他类的start()方法与run()方法

    java中继承thread或者实现runnable接口的类必须重写run()方法. 如果其执行了start()方法,其实就是启动了线程的run()方法. 注意:如果是实现runnable接口的类是没有 ...

  4. Android笔记:Button

    示例代码摘自<第一行代码> ButtonDemo.java的代码: public class ButtonDemo extends Activity { @Override protect ...

  5. ucore-lab1-练习4report

    练习四:分析bootloader加载ELF格式的OS的过程  1.bootloader如何读取硬盘扇区? (1)在练习3中实现了bootloader让CPU进入保护模式,下一步的工作就是从硬盘上加载并 ...

  6. centos 7 下 TFTP服务器安装

    一.介绍 TFTP(Trivial File Transfer Protocol,简单文件传输协议)),是一个基于UDP 协议 69端口 实现的用于在客户机和服务器之间进行简单文件传输的协议提供不复杂 ...

  7. monobehaviour生命周期完整版

  8. MDK生成.bin

    方法1: 默认选择编译输出的路径输出bin fromelf.exe --bin -o "$L@L.bin" "#L" 保存编译 方法2: 在要输出的目录下,新建 ...

  9. Android模拟器故障:waiting for target deviceto come online

    关闭再打开模拟器.删除再新建模拟器均无效. 解决办法:在AVD Manager中,选择立即冷启动(Cold Boot Now)模拟器.

  10. ActiveMQ之HelloWorld

    JMS实现JMS接口的消息中间件 Provider:生产者 Consumer:消费者 PTP:Point to Point:点对点的消息模型 Pub/Sub:Publish/Subscribe:发布订 ...