1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| #include<bits/stdc++.h> using namespace std; #define pb push_back
const int MAXN = 50005; const int MAXM = 200005;
int n, m; int match1[MAXN], match2[MAXN]; int Q[MAXN], D1[MAXN], D2[MAXN]; vector <int> E[MAXN];
inline bool bfs() { int s = 0, t = 0, u, v; memset(D1, -1, sizeof(D1)); memset(D2, -1, sizeof(D2)); for (int i = 0; i < n; i++) if (match1[i] == -1) Q[t++] = i, D1[i] = 0; while (s != t) if ((u = Q[s++]) != n) for (int i = 0; i < (int)E[u].size(); i++) if (D2[v = E[u][i]] == -1) { D2[v] = D1[u] + 1; if (D1[match2[v]] == -1) D1[Q[t++] = match2[v]] = D2[v] + 1; } return D1[n] != -1; }
bool dfs(int u) { for (int i = 0, v; i < (int)E[u].size(); i++) if (D2[v = E[u][i]] == D1[u] + 1 && (D2[v] = -1) && (match2[v] == n || dfs(match2[v]))) { match1[u] = v; match2[v] = u; D1[u] = -1; return true; } D1[u] = -1; return false; }
inline int hopcroft_karp() { memset(match1, -1, sizeof(match1)); for (int i = 0; i < n; i++) match2[i] = n; int ret = 0; for (int i = 0; i < n; i++) for (int j = 0, u; j < E[i].size(); j++) if (match2[u = E[i][j]] == n) { match1[i] = u; match2[u] = i; ret++; break; } while (bfs()) for (int i = 0; i < n; i++) if (match1[i] == -1 && dfs(i)) ret++; return ret; } string s1,s2,t; unordered_map<string,int> ha; unordered_map<int,string> ha2; int sz; int a1[1010]; int a2[1010]; struct pp{ int a,num; } qq[1010]; bool cmp(pp &x,pp &y) { return x.a<y.a; } int main() { scanf("%d",&n); sz=n-1; for(int i=0;i<n;i++) { cin>>s1>>s2; t=""; t+=s1[0]; t+=s1[1]; t+=s1[2]; if(ha.find(t)==ha.end()) ha[t]=++sz,ha2[sz]=t; a1[i]=ha[t]; t=""; t+=s1[0]; t+=s1[1]; t+=s2[0]; if(ha.find(t)==ha.end()) ha[t]=++sz,ha2[sz]=t; a2[i]=ha[t]; qq[i].a=a1[i]; qq[i].num=i; } sort(qq,qq+n,cmp); for(int i=0;i<n;i++) { bool bj=0; if(i!=0&&qq[i].a==qq[i-1].a) bj=1; if(i!=n-1&&qq[i].a==qq[i+1].a) bj=1; int tt=qq[i].num; E[tt].pb(a2[tt]); E[a2[tt]].pb(tt); m+=2; if(bj==0) E[tt].pb(a1[tt]),E[a1[tt]].pb(tt),m+=2; } int pp=n; n=sz+1; int u=hopcroft_karp(); u/=2; if(u==pp) puts("YES"); else {puts("NO");return 0;} for(int i=0;i<pp;i++) cout<<ha2[match1[i]]<<endl; return 0; }
|