codeforces 782D 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 yif the first options of x and y are different.

每队队名各有两种选择,
第一种选择是:选team's name的前三个字母,记作第一字符串
第二种选择是:选team's name的前两个字母加hometown's name的第一个字母,
              记作第二字符串
*:如果某队的第一字符串在所有第一字符串中出现了两次及以上,则只能选第二字符串。
要求使每队队名互不相同的方案。


由于每队只有两种选择,而且选择之间相互影响,
所以其实可以看作2-SAT问题去做,就很裸了,
设队i为变量xi,选第一字符串xi为0,选第二字符串xi为1,
队名相同即xi与xj冲突,则可连相应的有向边。


由于条件*,所以需要注意一些xi已经有初值了。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define FOP freopen("data.txt","r",stdin)
#define FOP2 freopen("data1.txt","w",stdout)
#define inf_LL 4223372036854775807
#define inf 0x3f3f3f3f
#define maxn 1010
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;

int n;
char s1[30], s2[30];
char a[maxn][2][5];

int cot1[150][150][150]; // cot1[i][j][k]记录第一字符串中字符串"ijk"的出现次数
int cot2[150][150][150]; // cot2[i][j][k]记录必须选择的第二字符串中字符串"ijk"的出现次数

// two SAT
vector<int> G[maxn*2];
bool mark[maxn*2];
int S[maxn*2], c;

bool in[maxn*2]; // 初始赋值

void add_clause(int x, int xval, int y, int yval)
{
    x = x*2 + xval;
    y = y*2 + yval;
    G[x].push_back(y^1);
    G[y].push_back(x^1);
}

bool dfs(int x)
{
    if(mark[x^1] || in[x^1]) return false;
    if(mark[x]) return true;
    mark[x] = true;
    S[c++] = x;
    for(int i = 0; i < G[x].size(); i++)
        if(!dfs(G[x][i])) return false;
    return true;
}

bool solve()
{
    for(int i = 2; i <= n*2; i += 2)
    {
        if(!mark[i] && !mark[i+1])
        {
            c = 0;
            if(!dfs(i))
            {
                while(c > 0) mark[S[--c]] = false; // 恢复
                if(!dfs(i+1)) return false;
            }
        }
    }
    return true;
}

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    {
        scanf("%s", s1);
        scanf("%s", s2);
        for(int j = 0; j < 3; j++) a[i][0][j] = s1[j];
        for(int j = 0; j < 2; j++) a[i][1][j] = s1[j];
        a[i][1][2] = s2[0];
    }

    for(int i = 1; i <= n; i++)
    {
        int a1 = a[i][0][0], a2 = a[i][0][1], a3 = a[i][0][2];
        cot1[a1][a2][a3]++;
    }

    bool f = 0;
    for(int i = 1; i <= n; i++)
    {
        int a1 = a[i][0][0], a2 = a[i][0][1], a3 = a[i][0][2];
        if(cot1[a1][a2][a3] > 1)
        {
            a1 = a[i][1][0], a2 = a[i][1][1], a3 = a[i][1][2];
            if(++cot2[a1][a2][a3] > 1) { f = 1; break; }
            in[i*2+1] = true;
        }
    }
    if(f) { printf("NO\n"); return 0; }

     for(int i = 1; i <= n; i++)
        for(int ti = 0; ti < 2; ti++)
            for(int j = i+1; j <= n; j++)
                for(int tj = 0; tj < 2; tj++)
                    if(strcmp(a[i][ti], a[j][tj]) == 0)
                        add_clause(i, ti, j, tj);

    if(!solve()) printf("NO\n");
    else
    {
        printf("YES\n");
        for(int i = 1; i <= n; i++)
        {
            if(mark[i*2]) puts(a[i][0]);
            else puts(a[i][1]);
        }
    }
    return 0;
}


源码来自:https://pan.quark.cn/s/7a757c0c80ca 《在Neovim中运用Lua的详尽教程》在当代文本编辑器领域,Neovim凭借其卓越的性能、可扩展性以及高度可定制的特点,赢得了程序开发者的广泛青睐。 其中,Lua语言的融入更是为Neovim注入了强大的活力。 本指南将深入剖析如何在Neovim中高效地运用Lua进行配置和插件开发,助你充分发挥这一先进功能的潜力。 一、Lua为何成为Neovim的优选方案经典的Vim脚本语言(Vimscript)虽然功能完备,但其语法结构与现代化编程语言相比显得较为复杂。 与此形成对比的是,Lua是一种精简、轻量且性能卓越的脚本语言,具备易于掌握、易于集成的特点。 因此,Neovim选择Lua作为其核心扩展语言,使得配置和插件开发过程变得更加直观和便捷。 二、安装与设置在Neovim中启用Lua支持通常十分简便,因为Lua是Neovim的固有组件。 然而,为了获得最佳体验,我们建议升级至Neovim的最新版本。 可以通过`vim-plug`或`dein.vim`等包管理工具来安装和管理Lua插件。 三、Lua基础在着手编写Neovim的Lua配置之前,需要对Lua语言的基础语法有所掌握。 Lua支持变量、函数、控制流、表(类似于数组和键值对映射)等核心概念。 它的语法设计简洁明了,便于理解和应用。 例如,定义一个变量并赋值:```lualocal myVariable = "Hello, Neovim!"```四、Lua在Neovim中的实际应用1. 配置文件:Neovim的初始化文件`.vimrc`能够完全采用Lua语言编写,只需在文件首部声明`set runtimepath^=~/.config/nvim ini...
基于STM32 F4的永磁同步电机无位置传感器控制策略研究内容概要:本文围绕基于STM32 F4的永磁同步电机(PMSM)无位置传感器控制策略展开研究,重点探讨在不使用机械式位置传感器的情况下,如何通过算法实现对电机转子位置和速度的精确估算与控制。文中结合STM32 F4高性能微控制器平台,采用如滑模观测器(SMO)、扩展卡尔曼滤波(EKF)或高频注入法等先进观测技术,实现对电机反电动势或磁链的实时估算,进而完成磁场定向控制(FOC)。研究涵盖了控制算法设计、系统建模、仿真验证(可能使用Simulink)以及在嵌入式平台上的代码实现与实验测试,旨在提高电机驱动系统的可靠性、降低成本并增强环境适应性。; 适合人群:具备一定电机控制理论基础和嵌入式开发经验的电气工程、自动化及相关专业的研究生、科研人员及从事电机驱动开发的工程师;熟悉C语言和MATLAB/Simulink工具者更佳。; 使用场景及目标:①为永磁同步电机驱动系统在高端制造、新能源汽车、家用电器等领域提供无位置传感器解决方案的设计参考;②指导开发者在STM32平台上实现高性能FOC控制算法,掌握位置观测器的设计与调试方法;③推动电机控制技术向低成本、高可靠方向发展。; 其他说明:该研究强调理论与实践结合,不仅包含算法仿真,还涉及实际硬件平台的部署与测试,建议读者在学习过程中配合使用STM32开发板和PMSM电机进行实操验证,以深入理解控制策略的动态响应与鲁棒性问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值