Codeforces 1041 E

构造

#include<bits/stdc++.h>
#define N 1007
using namespace std;
int usd[N],last,a[N],b[N],n,t[N],p,ega[N],egb[N],tot;
int get() {
    static int tog=1;
    while (usd[tog]) tog++;
    return usd[tog]=1,tog;
}
signed main () {
    scanf("%d",&n);
    for (int i=1;i<n;i++) {
     scanf("%d%d",a+i,b+i); 
     if (a[i]>b[i]) swap(a[i],b[i]);
     if (b[i]!=n) {
         puts("NO"); return 0 ;
     } 
     t[a[i]]++; usd[a[i]]=1;
    }
    for (int i=1;i<n;i++) {
       if (!t[i]) continue;
       last=n; usd[i]=1; 
       for (int j=1;j<t[i];j++) {
              p=get();
           if (p>i) {
                puts("NO"); return 0;
           } 
              ega[++tot]=last; egb[tot]=p;
              last=p;
       }
       ega[++tot]=last; egb[tot]=i;
//       printf("%d %d\n",last,i);
    }
    puts("YES");
    for (int i=1;i<n;i++) {
        printf("%d %d\n",ega[i],egb[i]);
    }
    
}

 

posted @ 2018-10-28 21:11  泪寒之雪  阅读(202)  评论(0编辑  收藏  举报