Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals )D. Innokenty and a Football League(2-sat)
2 seconds
256 megabytes
standard input
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:
- 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.
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.
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.
2
DINAMO BYTECITY
FOOTBALL MOSCOW
YES
DIN
FOO
2
DINAMO BYTECITY
DINAMO BITECITY
NO
3
PLAYFOOTBALL MOSCOW
PLAYVOLLEYBALL SPB
GOGO TECHNOCUP
YES
PLM
PLS
GOG
3
ABC DEF
ABC EFG
ABD OOO
YES
ABD
ABE
ABO
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+;
const int M = 1e5+;
int n,m,k=,l,ans=;
int parent[N],pre[N],color[N];
string str[];
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 = ;
memset(head,-,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^])return false;
if(vis[u])return true;
vis[u] = true;
S[top++] = u;
for(int i = head[u]; i != -; 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 = ; i < n; i += ) {
if(vis[i] || vis[i^])continue;
top = ;
if(!dfs(i)) {
while(top)vis[S[--top]] = false;
if(!dfs(i^)) return false;
}
}
return true;
}
int main() {
init();
int u,v;
scanf("%d",&n);
for(int i=;i<=n;i++){
cin>>str[];
cin>>str[];
string uu=str[].substr(,);
string vv=str[].substr(,)+str[][];
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=;i<=n;i++){
for(int j=;j<=n;j++){
if(i==j)continue;
if(a[i].fir==a[j].fir){
addedge(*i-,*j-);
addedge(*i-,*j-);
}
if(a[i].fir==a[j].sec){
addedge(*i-,*j-);
}
if(a[i].sec==a[j].fir){
addedge(*i-,*j-);
}
if(a[i].sec==a[j].sec){
addedge(*i-,*j-);
}
}
}
if(Twosat(*n)) {
puts("YES");
for(int i = ; i < *n; i++)
if(vis[i])
if((i+)&)
cout<<pm[a[(i+)/].fir]<<endl;
else
cout<<pm[a[(i+)/].sec]<<endl;
}
else puts("NO");
return ;
}
Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals )D. Innokenty and a Football League(2-sat)的更多相关文章
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)
Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) 说一点东西: 昨天晚上$9:05$开始太不好了,我在学校学校$9:40$放 ...
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D. Innokenty and a Football League
地址:http://codeforces.com/contest/782/problem/D 题目: D. Innokenty and a Football League time limit per ...
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)A模拟 B三分 C dfs D map
A. Andryusha and Socks time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 树的性质和dfs的性质 Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) E
http://codeforces.com/contest/782/problem/E 题目大意: 有n个节点,m条边,k个人,k个人中每个人都可以从任意起点开始走(2*n)/k步,且这个步数是向上取 ...
- 2-sat Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D
http://codeforces.com/contest/782/problem/D 题意: 每个队有两种队名,问有没有满足以下两个条件的命名方法: ①任意两个队的名字不相同. ②若某个队 A 选用 ...
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) E Underground Lab
地址:http://codeforces.com/contest/782/problem/E 题目: E. Underground Lab time limit per test 1 second m ...
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C Andryusha and Colored Balloons
地址:http://codeforces.com/contest/782/problem/C 题目: C. Andryusha and Colored Balloons time limit per ...
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) B. The Meeting Place Cannot Be Changed
地址:http://codeforces.com/contest/782/problem/B 题目: B. The Meeting Place Cannot Be Changed time limit ...
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) A. Andryusha and Socks
地址:http://codeforces.com/contest/782/problem/A 题目: A. Andryusha and Socks time limit per test 2 seco ...
随机推荐
- 利用npm安装/删除/发布/更新/撤销发布包
利用npm安装/删除/发布/更新/撤销发布包 什么是npm? npm是javascript的包管理工具,是前端模块化下的一个标志性产物 简单地地说,就是通过npm下载模块,复用已有的代码,提高工作效率 ...
- [Leetcode] set matrix zeroes 矩阵置零
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click ...
- POJ3623 Best Cow Line, Gold 【后缀数组】
最好的牛线,金 时间限制: 5000MS 内存限制: 65536K 提交总数: 5917 接受: 2048 描述 FJ即将把他的ñ(1≤ ñ ≤30,000)头牛竞争一年一度的"年度 ...
- 【ZJ选讲·压缩】
给一个由小写字母组成的字符串(len<=50) 我们可以用一种简单的方法来压缩其中的重复信息. 用M,R两个大写字母表示压缩信息 M标记重复串的开始, R表示后面的一段字符串重复从上一个 ...
- bzoj3343: 教主的魔法 分块 标记
修改:两边暴力重构,中间打标记.复杂度:O(n0.5) 查询:中间二分两边暴力.O(n0.5logn0.5) 总时间复杂度O(n*n0.5logn0.5) 空间复杂度是n级别的 标记不用下传因为标记不 ...
- java注解(Annotation)的简单介绍
注解你可以理解为一个特殊的类,或者接口其自定义个格式形如 public @interface 注解名(){ //注解的属性,特别提醒当注解的属性为value时,在对其赋值时,可以不写value,而直接 ...
- Optimize Prime Sieve
/// A heavily optimized sieve #include <cstdio> #include <cstring> #include <algorith ...
- C++ Review
#include "iostream" #include "iomanip" #include "cstdio" using namespa ...
- Spring - IoC(2): 属性注入 & 构造注入
依赖注入是指程序运行过程中,如果需要另外的对象协作(访问它的属性或调用它的方法)时,无须在代码中创建被调用者,而是依赖于外部容器的注入. 属性注入(Setter Injection) 属性注入是指 I ...
- 膨胀、腐蚀、开、闭(matlab实现)
膨胀.腐蚀.开.闭运算是数学形态学最基本的变换. 本文主要针对二值图像的形态学 膨胀:把二值图像各1像素连接成分的边界扩大一层(填充边缘或0像素内部的孔): B=[0 1 0 1 1 1 ...