[Bzoj1143][CTSC2008]祭祀river
思路清晰的发现题意是求最小路径覆盖。
最少路径覆盖:是指在一个有向图中,找出最少的几条路径,用它们来覆盖全图。
先闭包处理一下,然后二分图跑一下。
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const unsigned int maxn = 100 + 10; 5 const double PI = acos(-1); 6 int g[maxn][maxn], L[maxn], vis[maxn]; 7 int n; 8 int dfs(int x) { 9 for (int i = 1; i <= n; i++) { 10 if (g[x][i] && vis[i] == 0) { 11 vis[i] = 1; 12 if (L[i] == -1 || dfs(L[i])) { 13 L[i] = x; 14 return 1; 15 } 16 } 17 } 18 return 0; 19 } 20 int xyl() { 21 int ans = 0; 22 memset(L, -1, sizeof(L)); 23 for (int i = 1; i <= n; i++) { 24 memset(vis, 0, sizeof(vis)); 25 if (dfs(i)) 26 ans++; 27 } 28 return ans; 29 } 30 int main() { 31 int m, x, y; 32 scanf("%d%d", &n, &m); 33 for (int i = 1; i <= m; i++) 34 scanf("%d%d", &x, &y), g[x][y] = 1; 35 for (int k = 1; k <= n; k++) 36 for (int i = 1; i <= n; i++) 37 for (int j = 1; j <= n; j++) 38 g[i][j] |= g[i][k] & g[k][j]; 39 printf("%d\n", n - xyl()); 40 }