点击打开链接
题意:一个顶点要一个人放哨,则和他相连的几个定点就不需要人放哨了,问最少需要多少人放哨
思路:很明显的题意,很明显的最小顶点覆盖,最小顶点覆盖=最大匹配,建个双向边,结果除2,我的二分图匹配一直是用最大流写的
#include <queue> #include <vector> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; const int inf=0x3f3f3f3f; const int maxn=3010; struct edge{ int to,cap,rev; edge(){} edge(int a,int b,int c){to=a;cap=b;rev=c;} }; vector<edge> G[maxn]; int level[maxn],iter[maxn]; void addedge(int from,int to,int cap){ G[from].push_back(edge(to,cap,G[to].size())); G[to].push_back(edge(from,0,G[from].size()-1)); } void bfs(int s){ memset(level,-1,sizeof(level)); queue<int>que; level[s]=0;que.push(s); while(!que.empty()){ int v=que.front();que.pop(); for(unsigned int i=0;i<G[v].size();i++){ edge &e=G[v][i]; if(e.cap>0&&level[e.to]<0){ level[e.to]=level[v]+1; que.push(e.to); } } } } int dfs(int v,int t,int f){ if(v==t) return f; for(int &i=iter[v];i<G[v].size();i++){ edge &e=G[v][i]; if(e.cap>0&&level[v]<level[e.to]){ int d=dfs(e.to,t,min(f,e.cap)); if(d>0){ e.cap-=d; G[e.to][e.rev].cap+=d; return d; } } } return 0; } int max_flow(int s,int t){ int flow=0; while(1){ bfs(s); if(level[t]<0) return flow; memset(iter,0,sizeof(iter)); int f; while((f=dfs(s,t,inf))>0) flow+=f; } } int main(){ int n,a,b,c; while(scanf("%d",&n)!=-1){ for(int i=0;i<maxn;i++) G[i].clear(); for(int i=0;i<n;i++){ scanf("%d:(%d)",&a,&b); a++; for(int j=0;j<b;j++){ scanf("%d",&c);c++; addedge(a,n+c,1); addedge(c,a+n,1); } } for(int i=1;i<=n;i++) addedge(0,i,1); for(int i=n+1;i<=2*n;i++) addedge(i,2*n+1,1); int ans=max_flow(0,2*n+1); printf("%d\n",ans/2); } return 0; }