标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3444 Accepted Submission(s): 1669
#include <cstdio> #include <cmath> #include <cstring> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <queue> #include <map> #include <stack> using namespace std; #define test freopen("in.txt","r",stdin) #define pt(u,p) cout<<u<<"="<<p<<endl; #define pb(u) push_back(u) #define ptln cout<<endl; #define ptarr(i,a,s,f) for(int i=s;i<=f;i++) {if(i!=f) cout<<a[i]<<" ";else cout<<a[i]<<endl;} #define REP(i,s,f) for(int i=s;i<=f;i++) #define RREP(i,s,f) for(int i=s;i>=f;i--) #define COVER(a,v) memset(a,v,sizeof(a)) #define IOS ios_base::sync_with_stdio() typedef long long ll; const int inf=0x3f3f3f3f; const ll infll = 0x3f3f3f3f3f3f3f3fLL; const int maxn=400; const int maxm=4000; int n,m,k,kase; struct Edge{ int to,next; }edge[maxm]; int head[maxn],tot; 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++; } int linker[maxn]; bool used[maxn]; bool dfs(int u) { for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(!used[v]) { used[v]=true; if(linker[v]==-1||dfs(linker[v])) { linker[v]=u; return true; } } } return false; } int hungary() { int res=0; memset(linker,-1,sizeof(linker)); for(int u=0;u<n;u++) { memset(used,false,sizeof(used)); int ret=dfs(u); if(ret) res++; } return res; } int main() { while(1) { cin>>n; if(!n)break; cin>>m>>k; init(); for(int i=0;i<k;i++) { int u,v1,v2; cin>>u>>v1>>v2; if(v1>0&&v2>0) addedge(v1,v2); } cout<<hungary()<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/diang/p/4840346.html