hdoj 3861 The King’s Problem 【有向图tarjan求SCC + 缩点 + 最小路径覆盖】
2015-07-23 19:19
477 查看
The King’s Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2045 Accepted Submission(s): 725
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
题意:国王有一个n个城市,m条单向边的国土。现在他为了治理国家更加有效率,要把国土划分几个州。但是要求是: 若两点v,u有路径(v, u),(u, v)那么这些路径必须要在同一个州中。并且他还要保证建立了州以后,一个区域内的任意两点至少要有单向路径。问他至少要建多少州。
按着题意来,首先求SCC(保证两点间的路径在同一个州里面),然后缩点后构成新图,接下来 就是 求最小路径覆盖
AC代码:
#include <cstdio> #include <cstring> #include <queue> #include <stack> #include <vector> #include <algorithm> #define MAXN 50000+10 #define MAXM 200000+10 #define INF 10000000 using namespace std; struct Edge { int from, to, next; }edge[MAXM]; int head[MAXN], edgenum; int low[MAXN], dfn[MAXN]; int dfs_clock; int sccno[MAXN], scc_cnt; stack<int> S; bool Instack[MAXN]; int in[MAXN], out[MAXN];//记录SCC出度和入度 vector<int> G[MAXN];//缩点后新图 vector<int> scc[MAXN];//存储SCC的点 int n, m; int pipei[MAXN];//二分图匹配 用到 int used[MAXN]; void init() { edgenum = 0; memset(head, -1, sizeof(head)); } void addEdge(int u, int v) { Edge E = {u, v, head[u]}; edge[edgenum] = E; head[u] = edgenum++; } void getMap() { int a, b; while(m--) { scanf("%d%d", &a, &b); addEdge(a, b); } } void tarjan(int u, int fa) { int v; low[u] = dfn[u] = ++dfs_clock; S.push(u); Instack[u] = true; for(int i = head[u]; i != -1; i = edge[i].next) { v = edge[i].to; if(!dfn[v]) { tarjan(v, u); low[u] = min(low[u], low[v]); } else if(Instack[v]) low[u] = min(low[u], dfn[v]); } if(low[u] == dfn[u]) { scc_cnt++; scc[scc_cnt].clear(); for(;;) { v = S.top(); S.pop(); scc[scc_cnt].push_back(v); Instack[v] = false; sccno[v] = scc_cnt; if(v == u) break; } } } void find_cut(int l, int r) { memset(low, 0, sizeof(low)); memset(dfn, 0, sizeof(dfn)); memset(sccno, 0, sizeof(sccno)); memset(Instack, false, sizeof(Instack)); dfs_clock = scc_cnt = 0; for(int i = l; i <= r; i++) if(!dfn[i]) tarjan(i, -1); } void suodian() { for(int i = 1; i <= scc_cnt; i++) G[i].clear(), in[i] = out[i] = 0; for(int i = 0; i < edgenum; i++) { int u = sccno[edge[i].from]; int v = sccno[edge[i].to]; if(u != v) G[u].push_back(v), in[v]++, out[u]++; } } int find(int x)//匹配 { for(int i = 0; i < G[x].size(); i++) { int y = G[x][i]; if(used[y] == 0) { used[y] = 1; if(pipei[y] == -1 || find(pipei[y])) { pipei[y] = x; return 1; } } } return 0; } void solve() { int ans = 0; memset(pipei, -1, sizeof(pipei)); for(int i = 1; i <= scc_cnt; i++) { memset(used, 0, sizeof(used)); ans += find(i); } printf("%d\n", scc_cnt - ans);//最小路径覆盖等于 节点数减去最大匹配数 } int main() { int t; scanf("%d", &t); while(t--) { scanf("%d%d", &n, &m); init(); getMap(); find_cut(1, n);//求所有SCC suodian();//缩点 solve();//求最小路径覆盖 } return 0; }
相关文章推荐
- 关于我所发表的随笔
- [转载]VFS—Kernel Space & User Space
- Cordova
- 终于决定要写博客啦
- json进阶(一)js读取解析JSON类型数据
- pat 1093. Count PAT's (25)
- dom4j utf-8
- 多校第二场 1004 hdu 5303 Delicious Apples(背包+贪心)
- json进阶(一)js读取解析JSON类型数据
- Hdu 2222(AC 自动机)
- 关于虚拟继承和直接继承
- android 自定义View drawable
- C# KTV 系统 SQL数据库连接 C# 应用窗体
- bj java 初学 2015-07-23
- iOS OC07_NSDate
- 2015 Multi-University Training Contest 2 1004 苹果树 dp+单调队列
- 关于Comparable的compareTo的正确写法
- Android环境搭建
- 关于PROFIBUS Master(H)不能正确识别并处理 DP-Slave 回复的RS帧的一些思考
- 使用zip4j加密和解密文件和目录