首页 > 代码库 > codeves 1222

codeves 1222

思路:先得到最大匹配,不为n,none,对于每个边,假如不可获缺,那么没有它一定无法完美匹配,对匹配的边依次删除,如果不能完美匹配就输出

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=102;
 4 
 5 int a[N][N],vis[N];
 6 int c[N],d[N];
 7 int n;
 8 bool Find(int x){
 9     for(int i=1;i<=n;i++){
10         if(!a[x][i]&&!vis[i]){
11             vis[i]=1;
12             if(c[i]==0||Find(c[i])){
13                 c[i]=x;
14                 d[x]=i;return 1;
15             }
16         }
17     }
18     return 0;
19 }
20 
21 int main(){
22 
23     int x,y;
24     scanf("%d",&n);
25     while(scanf("%d%d",&x,&y)&&x&&y){
26         a[x][y]=1;
27     }
28     int all=0;
29     for(int i=1;i<=n;i++){
30         memset(vis,0,sizeof(vis));
31          if(Find(i)) all++;
32     }
33        
34     if(all!=n) {
35         cout<<"none"<<endl;
36     }
37     else {
38         int  t=0;
39         for(int i=1;i<=n;i++){
40             memset(vis,0,sizeof(vis));
41             int z=d[i];
42             a[i][z]=1;
43             c[z]=0;d[i]=0;
44             if(!Find(i)){
45                 cout<<i<<" "<<z<<endl;t=1;
46                 c[z]=i;d[i]=z;
47             }
48             a[i][z]=0;
49         }
50         if(!t) cout<<"none"<<endl;
51     }
52 }

 

codeves 1222