标签:
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5444 Accepted Submission(s): 2520
/** 题意: 给出一个图,求最小顶点覆盖 做法:二分图最大匹配,最小顶点覆盖 == 最大匹配(双向图)/2;匈牙利算法,时间复杂度是O(VE), **/ #include<iostream> #include<cmath> #include<string.h> #include<algorithm> #include<stdio.h> #define maxn 1570 using namespace std; int g[maxn][maxn]; int linker[maxn]; int head[maxn]; int used[maxn]; int num = 0; int n; int tot = 0; struct Node { int to; int next; }edge[maxn*10]; void Init() { tot = 0; memset(head,-1,sizeof(head)); } void addedge(int u,int v) { edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++; } bool dfs(int u) { for(int v = head[u];~v;v = edge[v].next) { int tt = edge[v].to; if(used[tt] == 0 ) { used[tt] = 1; if(linker[tt] == -1||dfs(linker[tt])) { linker[tt] = u; return true; } } } return false; } int hungary() { memset(linker,-1,sizeof(linker)); int res = 0; for(int i=0; i<n; i++) { memset(used,0,sizeof(used)); if(dfs(i)) res++; } return res; } int main() { //#ifndef ONLINE_JUDGE // freopen("in.txt","r",stdin); //#endif // ONLINE_JUDGE while(~scanf("%d",&n)) { int u,v,Q; Init(); memset(g,0,sizeof(g)); for(int i=0; i<n; i++) { scanf("%d:(%d)",&u,&Q); while(Q--) { scanf("%d",&v); addedge(u,v); addedge(v,u); } } int res = hungary(); printf("%d\n",res/2); } }
标签:
原文地址:http://www.cnblogs.com/chenyang920/p/4392729.html