最小不可相交路径覆盖。
点数-最大匹配数
代码如下:
#include<iostream> #include<algorithm> #include<queue> using namespace std; const int max_n = 2000; const int inf = 2e9; struct edge{ int to,next; }E[max_n<<1]; int head[max_n]; int cnt=1; void add(int from,int to){ E[cnt].to=to;E[cnt].next=head[from]; head[from]=cnt++; } int leftTo[max_n], rightTo[max_n]; int distleft[max_n], distright[max_n]; int dist; bool seacherpath(int left_tot, int right_tot) { fill(distleft, distleft + left_tot + 1, -1); fill(distright, distright + right_tot + 1, -1); queue<int> que;dist = inf; for (int i = 1;i <= left_tot;++i) if (leftTo[i] == -1) que.push(i); while (!que.empty()) { int u = que.front();que.pop(); if (distleft[u] > dist)break; for (int i = head[u];i;i = E[i].next) { int v = E[i].to; if (distright[v] != -1)continue; distright[v] = distleft[u] + 1; if (rightTo[v] == -1)dist = distright[v]; else { distleft[rightTo[v]] = distright[v] + 1; que.push(rightTo[v]); } } }return dist != inf; } bool matchpath(int u) { for (int i = head[u];i;i = E[i].next) { int v = E[i].to; if (distright[v] != distleft[u] + 1)continue; if (distright[v] == dist && rightTo[v] != -1)continue; distright[v] = -1; if (rightTo[v] == -1 || matchpath(rightTo[v])) { leftTo[u] = v; rightTo[v] = u; return true; } }return false; } int HK(int left_tot, int right_tot) { fill(leftTo, leftTo + left_tot + 1, -1); fill(rightTo, rightTo + right_tot + 1, -1); int ans = 0; while (seacherpath(left_tot, right_tot)) { for (int i = 1;i <= left_tot;++i) if (leftTo[i] == -1 && matchpath(i)) ++ans; }return ans; } int n; int main(){ while(scanf("%d",&n)!=EOF){ fill(head,head+n+10,0); cnt=1; for (int i=1;i<=n;++i){ int u,nu; scanf("%d:(%d)",&u,&nu); ++u; while (nu--){ int v;scanf("%d",&v); add(u,++v);add(v,u); } } printf("%d\n",HK(n,n)/2); } }