题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861
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条有向通道。如果从某个城市一条道路一直走到没有路可走,那么在这条首路上的所有城市划分成一个国家,每个城市只能属于一个国家,一个环形上的城市当然也是属于同一国家。问这些城市最少分为多少个国家。
解题:很明显是最小路径覆盖,但在这有向图中存在环,而二分图是在有向无环图时才正确,所以先用强连通缩点重新构成有向无环图。再二分匹配输出联通块个数-二分匹配数(最小边覆盖)
#pragma GCC optimize(2) #include<stdio.h> #include<algorithm> #include<string.h> #include<set> #include<vector> #include<queue> using namespace std; const int maxn = 1e5 + 100; const int inf = 0x3f3f3f3f; typedef long long ll; struct node { int v, next; }edge[maxn]; int dfn[maxn], low[maxn], Stack[maxn], instack[maxn], belong[maxn]; int head[maxn], linker[maxn], vis[maxn]; vector<int>vec[maxn]; int tot, n, m, index, cnt, stop; void init() { memset(head, -1, sizeof(head)); memset(dfn, 0, sizeof(dfn)); memset(low, 0, sizeof(low)); memset(Stack, 0, sizeof(Stack)); memset(instack, 0, sizeof(instack)); memset(belong, 0, sizeof(belong)); index = tot = 0; stop = cnt = 0; return; } void addedge(int u, int v) { edge[tot].v = v; edge[tot].next = head[u]; head[u] = tot++; return; } void tarjan(int u) { dfn[u] = low[u] = ++index; instack[u] = 1; Stack[stop++] = u; for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].v; if (!dfn[v]) { tarjan(v); low[u] = min(low[u], low[v]); } else if (instack[v]) { low[u] = min(low[u], dfn[v]); } } if (low[u] == dfn[u]) { cnt++; int v; do { v = Stack[--stop]; instack[v] = 0; belong[v] = cnt; } while (u != v); } } bool dfs(int x) { for (int i = 0; i < vec[x].size(); i++) { int v = vec[x][i]; if (!vis[v]) { vis[v] = true; if (linker[v] == -1 || dfs(linker[v])) { linker[v] = x; return true; } } } return false; } int match() { int ans = 0; memset(linker, -1, sizeof(linker)); for (int i = 1; i <= n; i++) { memset(vis, 0, sizeof(vis)); ans += dfs(i); } return ans; } int main() { //freopen("C://input.txt", "r", stdin); int t; scanf("%d", &t); while (t--) { init(); scanf("%d%d", &n, &m); for (int i = 0; i <= n; i++) { vec[i].clear(); } for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); addedge(u, v); } for (int i = 1; i <= n; i++) { if (!dfn[i]) { tarjan(i); } } for (int i = 1; i <= n; i++) { for (int j = head[i]; j != -1; j = edge[j].next) { int v = edge[j].v; if (belong[i] != belong[v]) { vec[belong[i]].push_back(belong[v]); } } } printf("%d\n", cnt - match()); } return 0; }
hdu 3861 The King’s Problem (强联通+二分匹配) 好题
最新推荐文章于 2019-08-17 23:11:30 发布