codeforces 781B. Innokenty and a Football League(貪心)

codeforces 781B. Innokenty and a Football League(貪心)

題目:
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:

the short name is the same as three first letters of the team’s name, for example, for the mentioned club it is “DIN”,
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個球隊,每個球隊有兩種明明方式 ,隊伍名字前三個字母或者隊伍名字前兩個字母加城市首字母,要求一旦這個隊伍使用第二種方式的時候不能有使用第一種命名方式的隊伍和這個這個隊伍使用第一種命名方式的名字相同,問能否爲所有隊伍命名

首先把他們的兩種命名結果hash一下,然後遍歷,發現相同的則用第二種明明方式同時對先前的處理,,具體處理方式,,看代碼吧2333,說不明白了…..

AC代碼:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct xx
{
    int a,b;
};
int a[1005],b[1005];
int now[1005];
int vis[75000];
int dfs(int i)
{
    int x=now[i],flag=0;
    if(vis[x]){
        int j=vis[x];
        if(j==-1){
            if(now[i]==b[i]) {vis[x]=i; return 0;}
            else{
                now[i]=b[i];
                return dfs(i);
            }
        }
        if(now[j]==b[j]&&now[i]==b[i]) return 1;
        if(now[j]==b[j]&&now[i]==a[i]){
            now[i]=b[i];
            return dfs(i);
        }
        if(now[j]==a[j]&&now[i]==b[i]){
            now[j]=b[j];
            vis[x]=i;
            return dfs(j);
        }
        now[i]=b[i];
        now[j]=b[j];
        vis[x]=-1;
        return dfs(i)+dfs(j);
    }
    else
        vis[x]=i;
    return flag;
}
int main()
{
    char s1[105],s2[105];
    memset(vis,0,sizeof(vis));
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%s %s",s1,s2);
        int x=(s1[0]-'A')*50*50+(s1[1]-'A')*50+1;
        a[i]=x+s1[2]-'A';
        b[i]=x+s2[0]-'A';
        now[i]=a[i];
    }
    int flag=0;
    for(int i=1;i<=n;i++){
        if(dfs(i)){
            flag=1;
            break;
        }
    }
    if(flag){
        printf("NO\n");
    }
    else {
        printf("YES\n");
        for(int i=1;i<=n;i++){
            now[i]--;
            int x=now[i]/2500;
            now[i]-=x*2500;
            int y=now[i]/50;
            int z=now[i]%50;
            printf("%c%c%c\n",'A'+x,'A'+y,'A'+z);
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章