HDU 3861.The King’s Problem 强联通分量+最小路径覆盖
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
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.
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 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.
转载一篇苣苣的博客:有向无环图(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 强联通分量+最小路径覆盖的更多相关文章
- HDU 3861 The King’s Problem(强连通分量+最小路径覆盖)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题目大意: 在csdn王国里面, 国王有一个新的问题. 这里有N个城市M条单行路,为了让他的王国 ...
- HDU 3861 The King’s Problem(强连通+二分图最小路径覆盖)
HDU 3861 The King's Problem 题目链接 题意:给定一个有向图,求最少划分成几个部分满足以下条件 互相可达的点必须分到一个集合 一个对点(u, v)必须至少有u可达v或者v可达 ...
- HDU 3861 The King's Problem(强连通分量缩点+最小路径覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=3861 题意: 国王要对n个城市进行规划,将这些城市分成若干个城市,强连通的城市必须处于一个州,另外一个州内的任意 ...
- 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 最小路径覆盖(强连通分量缩点+二分图最大匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 最小路径覆盖的一篇博客:https://blog.csdn.net/qq_39627843/ar ...
- hdu——3861 The King’s Problem
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- POJ 1904 King's Quest 强联通分量+输入输出外挂
题意:国王有n个儿子,现在这n个儿子要在n个女孩里选择自己喜欢的,有的儿子可能喜欢多个,最后国王的向导给出他一个匹配.匹配有n个数,代表某个儿子和哪个女孩可以结婚.已知这些条件,要你找出每个儿子可以和 ...
- HDU 4606 Occupy Cities (计算几何+最短路+二分+最小路径覆盖)
Occupy Cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 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 ...
随机推荐
- WEB常用前端开发调试工具介绍
只要是设计开发,就需要进行调试,尽管相对来说,前端的调试要简单一些,但使用一些调试工具或插件还是能提高你的工作效率.下面是一些主要用于IE浏览器环境和Firefox浏览器环境等的调试工具简介. 一.I ...
- bootstrap datatable
<table id="screenTable" data-toggle="table"> <thead> ... </thead& ...
- java和c#中String
java中: c#中: 1.拼接字符串 sql语句中 in() str="'001','002','003'";至于产生string就这样 str1="'001'&qu ...
- metasploit framework(二):记一次入侵
msfconsole use 其中一个 exploit前台执行注入 后台执行shell 加-j 通过sessions查看后台执行的shell,可以看到这个会话的id号为2 进入会话,sessions ...
- 解决不联网无法启动struts2问题
Unable to load configuration. - Class: java.net.PlainSocketImplFile: PlainSocketImpl.javaMethod: con ...
- JPA in
CriteriaBuilder.In in = criteriaBuilder.in(root.get("field1")); for (String str : arr) { i ...
- layui复选框
效果图 layui复选框,一个主的复选框控制多个从复选框,主复选框和从复选框的颜色不一样 layui复选框的样式,都是在选然后才会有的,所以直接通过css设置就实现不了了.只可以通过js动态设置 ht ...
- httpclient和httpUrlConnect区别
HttpURLConnection的用法 一.创建HttpURLConnection对象 URL url = new URL("http://localhost:8080/TestHttpU ...
- stm32 启动文件 C和汇编交叉嵌入
在嵌入式系统开发中,目前使用的主要编程语言是C和汇编,C++已经有相应的编译器,但是现在使用还是比较少的.在稍大规模的嵌入式软件中,例如含有OS,大部分的代码都是用C编写的,主要是因为C语言的结构比较 ...
- @RequestParam使用须知
--------------------------siwuxie095 @RequestParam 使用须知 使用 @Requ ...