Graph Reconstruction 可图化判定与构造方案
写了两小时发现这题没spj,绝望
1 #include<bits/stdc++.h> 2 #include<stdio.h> 3 #include<algorithm> 4 #include<queue> 5 #include<string.h> 6 #include<iostream> 7 #include<math.h> 8 using namespace std; 9 #define ll long long 10 11 const int inf=0x3f3f3f3f; 12 #define FOR(n) for(int i=1;i<=n;i++) 13 #define pb push_back 14 15 const int maxn=1e2+24; 16 int n; 17 18 struct NODE{ 19 int id; 20 int du; 21 friend bool operator <(NODE ax,NODE bx){return ax.du>bx.du;} 22 }arr[maxn]; 23 24 vector<int>U; 25 vector<int>V; 26 map<pair<int,int>,int>mp; 27 map<int,int>rmp; 28 29 int HavelHakimi(int n) //0不可图 1唯一图 -1多图 30 { 31 int flag=0; 32 for(int i = 1;i <= n; ++i){ 33 sort(arr+i,arr+n+1); 34 if(arr[i].du + i > n) return 0; 35 /* 36 前面的i个顶点的度数已经足够了,现在剩余n-i个顶点, 37 现在从这n-i个顶点里面找出一个顶点它的度数为arr[i], 38 arr[i]就代表与这个顶点相连的顶点个数,必然有arr[i]<n-i成立。 39 */ 40 if(arr[i].du+i+1<=n && arr[arr[i].du+i].du==arr[arr[i].du+i+1].du){ 41 if(arr[arr[i].du+i].du!=0){ 42 flag=1; 43 44 mp[make_pair( 45 arr[i].id,arr[arr[i].du+i].id) 46 ]=arr[arr[i].du+i+1].id; 47 48 rmp[arr[arr[i].du+i+1].id]=arr[arr[i].du+i].id; 49 } 50 } 51 for(int j = i+1;j <= arr[i].du+i;j++){ 52 arr[j].du--; 53 V.pb(arr[i].id);U.pb(arr[j].id); 54 if(arr[j].du < 0) return 0; 55 } 56 } 57 if(arr[n].du != 0) return 0; 58 else if(flag)return -1; 59 return 1; 60 } 61 62 int main(){ 63 while(~scanf("%d",&n)){ 64 65 U.clear();V.clear();mp.clear();rmp.clear(); 66 67 int ducnt=0; 68 FOR(n){ 69 scanf("%d",&arr[i].du); 70 arr[i].id=i; 71 ducnt+=arr[i].du; 72 } 73 int flag=HavelHakimi(n); 74 //printf("%d\n",flag); 75 if(flag==0)printf("IMPOSSIBLE\n"); 76 else if(flag==1){ 77 printf("UNIQUE\n"); 78 printf("%d %d\n",n,ducnt/2); 79 for(int i=0;i<V.size();i++){ 80 printf("%d ",V[i]); 81 }puts(""); 82 for(int i=0;i<U.size();i++){ 83 printf("%d ",U[i]); 84 } 85 puts(""); 86 }else{ 87 printf("MULTIPLE\n"); 88 printf("%d %d\n",n,ducnt/2); 89 for(int i=0;i<V.size();i++){ 90 printf("%d ",V[i]); 91 }puts(""); 92 for(int i=0;i<U.size();i++){ 93 printf("%d ",U[i]); 94 } 95 puts(""); 96 97 printf("%d %d\n",n,ducnt/2); 98 for(int i=0;i<V.size();i++){ 99 if(rmp.count(V[i])){ 100 printf("%d ",rmp[V[i]]); 101 rmp.erase(V[i]); 102 } 103 else printf("%d ",V[i]); 104 } 105 puts(""); 106 for(int i=0;i<U.size();i++){ 107 if(mp.count(make_pair(V[i],U[i]))){ 108 printf("%d ",mp[make_pair(V[i],U[i])]); 109 }else if(rmp.count(U[i])){ 110 printf("%d ",rmp[U[i]]); 111 rmp.erase(U[i]); 112 } 113 else printf("%d ",U[i]); 114 } 115 puts(""); 116 } 117 } 118 }