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
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.
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.
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+100,maxm=1e5+100,inf=0x3f3f3f3f,mod=1e9+7; const ll INF=1e13+7; 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=0; 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=0; memset(sccno,0,sizeof(sccno)); memset(pre,0,sizeof(pre)); for(int i=1; i<=n; i++) if(!pre[i]) dfs(i); } void build(int m) { for(int i=1; i<=scc_cnt; i++) T[i].clear(); for(int i=1; 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=0; i<T[u].size(); i++) { int v=T[u][i]; if(vis[v]) continue; vis[v]=true; if(cy[v]==-1||dfs2(cy[v])) { cy[v]=u; return true; } } return false; } int solve(int n) { int ret=0; memset(cy,-1,sizeof(cy)); for(int i=1; i<=n; i++) { memset(vis,0,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=1;i<=n;i++) G[i].clear(); for(int i=1; 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 0; }
I am a slow walker,but I never walk backwards.
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· 程序员常用高效实用工具推荐,办公效率提升利器!
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 【译】WinForms:分析一下(我用 Visual Basic 写的)