Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals )D. Innokenty and a Football League(2-sat)

D. Innokenty and a Football League
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Innokenty is a president of a new football league in Byteland. The first task he should do is to assign short names to all clubs to be shown on TV next to the score. Of course, the short names should be distinct, and Innokenty wants that all short names consist of three letters.

Each club's full name consist of two words: the team's name and the hometown's name, for example, "DINAMO BYTECITY". Innokenty doesn't want to assign strange short names, so he wants to choose such short names for each club that:

  1. the short name is the same as three first letters of the team's name, for example, for the mentioned club it is "DIN",
  2. or, the first two letters of the short name should be the same as the first two letters of the team's name, while the third letter is the same as the first letter in the hometown's name. For the mentioned club it is "DIB".

Apart from this, there is a rule that if for some club x the second option of short name is chosen, then there should be no club, for which the first option is chosen which is the same as the first option for the club x. For example, if the above mentioned club has short name "DIB", then no club for which the first option is chosen can have short name equal to "DIN". However, it is possible that some club have short name "DIN", where "DI" are the first two letters of the team's name, and "N" is the first letter of hometown's name. Of course, no two teams can have the same short name.

Help Innokenty to choose a short name for each of the teams. If this is impossible, report that. If there are multiple answer, any of them will suit Innokenty. If for some team the two options of short name are equal, then Innokenty will formally think that only one of these options is chosen.

Input

The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of clubs in the league.

Each of the next n lines contains two words — the team's name and the hometown's name for some club. Both team's name and hometown's name consist of uppercase English letters and have length at least 3 and at most 20.

Output

It it is not possible to choose short names and satisfy all constraints, print a single line "NO".

Otherwise, in the first line print "YES". Then print n lines, in each line print the chosen short name for the corresponding club. Print the clubs in the same order as they appeared in input.

If there are multiple answers, print any of them.

Examples
Input
2
DINAMO BYTECITY
FOOTBALL MOSCOW
Output
YES
DIN
FOO
Input
2
DINAMO BYTECITY
DINAMO BITECITY
Output
NO
Input
3
PLAYFOOTBALL MOSCOW
PLAYVOLLEYBALL SPB
GOGO TECHNOCUP
Output
YES
PLM
PLS
GOG
Input
3
ABC DEF
ABC EFG
ABD OOO
Output
YES
ABD
ABE
ABO
Note

In the first sample Innokenty can choose first option for both clubs.

In the second example it is not possible to choose short names, because it is not possible that one club has first option, and the other has second option if the first options are equal for both clubs.

In the third example Innokenty can choose the second options for the first two clubs, and the first option for the third club.

In the fourth example note that it is possible that the chosen short name for some club x is the same as the first option of another club y if the first options of x and y are different.

 【分析】题意有一点难理解,就是有n支球队,每支球队的名字由两个字符串组成。给一支球队取简称有两种方式。一:取第一个字符串的前三个字母;二:取第一个字符串前两个字符和第二个字符串的第一个字符组合。所有队伍的简称都必须是不同的。而且,如果一个队用第二种方式去了名称,那么其他队伍不但不能与它取一样的,并且如果他俩的第一种方式取的名是一样的,那么也不能取与他第一种方式的名称。

有点绕口啊,我的做法是2-sat(不会的可以去hihocoder上学学,就在前几周)。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 2e5+10;
const int M = 1e5+10;
int  n,m,k=0,l,ans=0;
int parent[N],pre[N],color[N];
string str[3];
map<string,int>mp;
map<int,string>pm;
struct man{
    int fir,sec;
}a[N];
struct Edge {
    int to,next;
} edge[M];
int head[M],tot;
void init() {
    tot = 0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v) {
    edge[tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot++;
}
bool vis[N];
int S[N],top;
bool dfs(int u) {
    if(vis[u^1])return false;
    if(vis[u])return true;
    vis[u] = true;
    S[top++] = u;
    for(int i = head[u]; i != -1; i = edge[i].next)
        if(!dfs(edge[i].to))
            return false;
    return true;
}
bool Twosat(int n) {
    memset(vis,false,sizeof(vis));
    for(int i = 0; i < n; i += 2) {
        if(vis[i] || vis[i^1])continue;
        top = 0;
        if(!dfs(i)) {
            while(top)vis[S[--top]] = false;
            if(!dfs(i^1)) return false;
        }
    }
    return true;
}
int main() {
    init();
    int u,v;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        cin>>str[1];
        cin>>str[2];
        string uu=str[1].substr(0,3);
        string vv=str[1].substr(0,2)+str[2][0];
        if(!mp[uu])mp[uu]=++k,pm[k]=uu;
        if(!mp[vv])mp[vv]=++k,pm[k]=vv;
        a[i].fir=mp[uu];
        a[i].sec=mp[vv];

    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i==j)continue;
            if(a[i].fir==a[j].fir){
                addedge(2*i-2,2*j-1);
                addedge(2*i-1,2*j-1);
            }
            if(a[i].fir==a[j].sec){
                addedge(2*i-2,2*j-2);
            }
            if(a[i].sec==a[j].fir){
                addedge(2*i-1,2*j-1);
            }
            if(a[i].sec==a[j].sec){
                addedge(2*i-1,2*j-2);
            }
        }
    }
    if(Twosat(2*n)) {
        puts("YES");
        for(int i = 0; i < 2*n; i++)
            if(vis[i])
                if((i+1)&1)
                    cout<<pm[a[(i+2)/2].fir]<<endl;
                else
                    cout<<pm[a[(i+1)/2].sec]<<endl;
    } 
    else puts("NO");
    return 0;
}
原文地址:https://www.cnblogs.com/jianrenfang/p/6511476.html