标签:
二分图的匹配多采用匈牙利算法,而一般图的匹配则是带花树。
4 4 1 3 2 3 2 4 3 1 6 6 1 2 3 2 3 4 5 2 5 4 5 6
1 2 3 2 4 5
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<set> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) typedef long long LL; typedef pair<int,int>pil; pil temp[250]; int ans[50]; deque<int>Q; const int MAXN=110; bool mp[MAXN][MAXN],inque[MAXN],inblossom[MAXN],G[MAXN][MAXN]; int match[MAXN],pre[MAXN],base[MAXN]; int findancestor(int u,int v) { bool inpath[MAXN]={false}; while(1) { u=base[u]; inpath[u]=true; if(match[u]==-1)break; u=pre[match[u]]; } while(1) { v=base[v]; if(inpath[v])return v; v=pre[match[v]]; } } void reset(int u,int anc){ while(u!=anc) { int v=match[u]; inblossom[base[u]]=1; inblossom[base[v]]=1; v=pre[v]; if(base[v]!=anc)pre[v]=match[u]; u=v; } } void contract(int u,int v,int n){ int anc=findancestor(u,v); memset(inblossom,0,sizeof(inblossom)); reset(u,anc);reset(v,anc); if(base[u]!=anc)pre[u]=v; if(base[v]!=anc)pre[v]=u; for(int i=1;i<=n;i++) if(inblossom[base[i]]) { base[i]=anc; if(!inque[i]) { Q.push_back(i); inque[i]=1; } } } bool dfs(int S,int n){ for(int i=0;i<=n;i++)pre[i]=-1,inque[i]=0,base[i]=i; Q.clear();Q.push_back(S);inque[S]=1; while(!Q.empty()) { int u=Q.front();Q.pop_front(); for(int v=1;v<=n;v++) { if(mp[u][v]&&base[v]!=base[u]&&match[u]!=v) { if(v==S||(match[v]!=-1&&pre[match[v]]!=-1))contract(u,v,n); else if(pre[v]==-1) { pre[v]=u; if(match[v]!=-1)Q.push_back(match[v]),inque[match[v]]=1; else { u=v; while(u!=-1) { v=pre[u]; int w=match[v]; match[u]=v; match[v]=u; u=w; } return true; } } } } } return false; } int solve(int n) { CLEAR(match,-1); int sum=0; for(int i=1;i<=n;i++) if(match[i]==-1&&dfs(i,n)) sum++; return sum; } int main() { int n,m; int x,y; while(~scanf("%d%d",&n,&m)) { CLEAR(mp,0); REPF(i,1,m) { scanf("%d%d",&x,&y); temp[i].first=x; temp[i].second=y; mp[x][y]=mp[y][x]=1; } memcpy(G,mp,sizeof(mp)); int s=solve(n); int k=0; REPF(l,1,m) { int u=temp[l].first; int v=temp[l].second; memcpy(mp,G,sizeof(G)); REPF(i,1,n) { if(mp[u][i]) mp[u][i]=mp[i][u]=0; if(mp[v][i]) mp[v][i]=mp[i][v]=0; } if(solve(n)+1<s) ans[k++]=l; } printf("%d\n",k); if(k==0) puts(""); sort(ans,ans+k); for(int i=0;i<k;i++) { if(i!=k-1) printf("%d ",ans[i]); else printf("%d\n",ans[k-1]); } } return 0; }
1099. Work Scheduling
Time limit: 0.5 second
Memory limit: 64 MB There is certain amount of night guards that are available to protect the local junkyard from possible junk robberies. These guards need to scheduled in pairs, so that each pair guards at different
night. The junkyard CEO ordered you to write a program which given the guards characteristics determines the maximum amount of scheduled guards (the rest will be fired). Please note that each guard can be scheduled with only one of his colleagues and no guard
can work alone.
InputThe first line of the input contains one number N ≤ 222 which is the amount of night guards. Unlimited number of lines consisting of unordered pairs (i, j) follow, each such
pair means that guard #i and guard #j can work together, because it is possible to find uniforms that suit both of them (The junkyard uses different parts of uniforms for different guards i.e. helmets, pants, jackets. It is impossible to
put small helmet on a guard with a big head or big shoes on guard with small feet). The input ends with Eof.
OutputYou should output one possible optimal assignment. On the first line of the output write the even number C, the amount of scheduled guards. Then output C/2 lines, each containing 2
integers (i, j) that denote that i and j will work together.
Sample
|
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<set> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) typedef long long LL; typedef pair<int,int>pil; deque<int>Q; const int MAXN=250; bool mp[MAXN][MAXN],inque[MAXN],inblossom[MAXN],G[MAXN][MAXN]; int match[MAXN],pre[MAXN],base[MAXN]; int findancestor(int u,int v) { bool inpath[MAXN]={false}; while(1) { u=base[u]; inpath[u]=true; if(match[u]==-1)break; u=pre[match[u]]; } while(1) { v=base[v]; if(inpath[v])return v; v=pre[match[v]]; } } void reset(int u,int anc){ while(u!=anc) { int v=match[u]; inblossom[base[u]]=1; inblossom[base[v]]=1; v=pre[v]; if(base[v]!=anc)pre[v]=match[u]; u=v; } } void contract(int u,int v,int n){ int anc=findancestor(u,v); memset(inblossom,0,sizeof(inblossom)); reset(u,anc);reset(v,anc); if(base[u]!=anc)pre[u]=v; if(base[v]!=anc)pre[v]=u; for(int i=1;i<=n;i++) if(inblossom[base[i]]) { base[i]=anc; if(!inque[i]) { Q.push_back(i); inque[i]=1; } } } bool dfs(int S,int n){ for(int i=0;i<=n;i++)pre[i]=-1,inque[i]=0,base[i]=i; Q.clear();Q.push_back(S);inque[S]=1; while(!Q.empty()) { int u=Q.front();Q.pop_front(); for(int v=1;v<=n;v++) { if(mp[u][v]&&base[v]!=base[u]&&match[u]!=v) { if(v==S||(match[v]!=-1&&pre[match[v]]!=-1))contract(u,v,n); else if(pre[v]==-1) { pre[v]=u; if(match[v]!=-1)Q.push_back(match[v]),inque[match[v]]=1; else { u=v; while(u!=-1) { v=pre[u]; int w=match[v]; match[u]=v; match[v]=u; u=w; } return true; } } } } } return false; } int solve(int n) { CLEAR(match,-1); int sum=0; for(int i=1;i<=n;i++) if(match[i]==-1&&dfs(i,n)) sum++; return sum; } int main() { int n; int x,y; while(~scanf("%d",&n)) { CLEAR(mp,0); while(~scanf("%d%d",&x,&y)) mp[x][y]=mp[y][x]=1; int s=solve(n); printf("%d\n",s*2); REPF(i,1,n) { for(int j=i+1;j<=n;j++) if(match[j]==i) printf("%d %d\n",i,j); } } return 0; }
标签:
原文地址:http://blog.csdn.net/u013582254/article/details/43488975