hdu3861The King’s Problem (强连通 缩点+最小路径覆盖)
来源:互联网 发布:贾真的淘宝店 编辑:程序博客网 时间:2024/05/22 04:25
The King’s Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1606 Accepted Submission(s): 584
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
13 21 21 3
Sample Output
2
Source
2011 Multi-University Training Contest 3 - Host by BIT
题意:有n个城市和m条有向通道。如果从某个城市一条道路一直走到没有路可走,那么在这条首路上的所有城市划分成一个国家,每个城市只能属于一个国家,一个环形上的城市当然也是属于同一国家。问这些城市最少分为多少个国家。
解题:很明显是最小路径覆盖,但在这有向图中存在环,而二分图是在有向无环图时才正确,所以先用强连通缩点重新构成有向无环图。再二分匹配。
#include<stdio.h>#include<iostream>#include<vector>using namespace std;int vist[5005],match[5005];vector<int>map[5005];int find(int i){ for(int j=0;j<map[i].size();j++) if(vist[map[i][j]]==0) { vist[map[i][j]]=1; if(match[map[i][j]]==0||find(match[map[i][j]])) { match[map[i][j]]=i; return 1; } } return 0;}//-----下方是用强连通把环缩成一个点,使整个图变成有向无环图------int stack[5005],sn,low[5005],dfn[5005],deep,node[5005],k;int Min(int a,int b){ return a>b?b:a;}void dfs(int i){ stack[++sn]=i; vist[i]=1; deep++; low[i]=dfn[i]=deep; for(int j=0;j<map[i].size();j++) if(vist[map[i][j]]==0) { dfs(map[i][j]); low[i]=Min(low[i],low[map[i][j]]); } else if(vist[map[i][j]]==1)//这个点在栈中 low[i]=Min(low[i],dfn[map[i][j]]); if(low[i]==dfn[i])//在栈中点i及到栈顶的点是一个强连通分量 { k++; while(stack[sn]!=i) { node[stack[sn]]=k; //表示原先的点stack[sn]在缩点后的图中是第k类点。 vist[stack[sn]]=2; //走过了,但己经出栈 sn--; } node[stack[sn]]=k; vist[stack[sn]]=2; sn--; }}int reSetMap(int n){ vector<int>mp[5005]; sn=0; deep=0; k=0; for(int j=1;j<=n;j++) vist[j]=0; for(int i=1;i<=n;i++) if(vist[i]==0) dfs(i); for(int i=1;i<=n;i++)//缩点构成新图 for(int j=0;j<map[i].size();j++) if(node[i]!=node[map[i][j]])//在新图中不是同一个点时 mp[node[i]].push_back(node[map[i][j]]); for(int i=1;i<=k;i++) map[i]=mp[i]; return k;//反回新图的点的个数}int main(){ int t,n,m,a,b,ans; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) map[i].clear(),match[i]=0; while(m--) { scanf("%d%d",&a,&b); map[a].push_back(b); } n=reSetMap(n);//缩点构成新的有向无环图 ans=0; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++)vist[j]=0; ans+=find(i); } printf("%d\n",n-ans); }}
0 0
- hdu3861The King’s Problem (强连通 缩点+最小路径覆盖)
- hdu 1269 The King’s Problem(强连通分量+缩点+最小路径覆盖)
- hdu3861 The King’s Problem【强连通+最小路径覆盖】
- HDU 3861 The King’s Problem (强连通分量缩点+二分图匹配最小路径覆盖)
- HDU 3861 The King’s Problem 强联通缩点 + 最小路径覆盖
- HDU --- 3861 The King’s Problem 【强联通缩点 + 最小路径覆盖】
- The King’s Problem(tarjan求强连通分量缩点+匈牙利求有向无环图的最小路径覆盖)
- hdu3861 The King’s Problem (强连通+最小路径覆盖)
- HDU 3861 The King’s Problem 强连通+最小路径覆盖
- hdu 3861 The King’s Problem (强连通+最小路径覆盖)
- HDU 3861 The King’s Problem(强连通+二分图最小路径覆盖)
- hdu 3681The King’s Problem(强连通分量+最小路径覆盖)
- HDU - 3861 The King’s Problem(强连通分量+最小路径覆盖)
- HDU 3861The King’s Problem 强连通分量分解 + 二分图最小路径覆盖
- hdu 3861 The King’s Problem【强连通Kosaraju+最小路径覆盖】
- HDOJ题目3861 The King’s Problem(强连通,最小点覆盖)
- The King’s Problem 【scc+缩点+最小路径覆盖】
- hdu3861 The King’s Problem (强连通缩点+最小路径覆盖)
- ios常用第三方库git下载地址
- objc中国-1.更轻量的 View Controllers 总结
- Mali GPU OpenGL ES 应用性能优化--基本概念
- JAVA 中日期 String int 类型互转
- MySQL索引背后的数据结构及算法原理
- hdu3861The King’s Problem (强连通 缩点+最小路径覆盖)
- Java学习笔记(七)Java的封装继承和多态
- IOS中系统唯一标识ID<一>
- 情人节专场 B题 最短路
- 电动机保护常识_巨隆电动机保护器
- 中西医颅神经平衡疗法的作用
- Java web 开发笔记 第五章 Servlet编程三
- 电动机综合保护器_巨隆电动机保护器
- Investment(完全背包)