http://codeforces.com/contest/782/problem/D

题意:

每个队有两种队名,问有没有满足以下两个条件的命名方法:

①任意两个队的名字不相同。

②若某个队 A 选用了第二种队名,那么如果队 B 的第一种队名和队 A 的相同,那么同样不能选择。当然,队B的第二个队名无所谓

思路:

学习了2-sat发现这题这么简单= =。

如果那天A了这题就前200了

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
/*
另第一个名字为a,第二个名字为b
一:
如果ai = aj,那么只能选择第二个
①如果bi = bj,那么无解
②如果bi != bj,那么两者之间连接一条边
上面是分类讨论 二:
如果ai != aj */
const int maxn = + ;
struct TwoSAT{
int n;
vector<int> G[maxn * ];
bool mark[maxn * ];
int c, s[maxn * ];///c是表示目前dfs到的个数和已经被标记的集合s
bool dfs(int x){
if (mark[x ^ ]) return false;
if (mark[x]) return true;
mark[x] = true;
s[c++] = x;
for (int i = ; i < G[x].size(); i++)
if (!dfs(G[x][i])) return false;
return true;
} void init(int n){
this->n = n;
for (int i = ; i < * n; i++) G[i].clear();
memset(mark, , sizeof(mark));
}
///利用题目条件找到x和y,即假设x1[0] | x2[1] = false;即:x1[0]|!x2[1]=false
///那么我们反转该条件:即x1[1]|!x2[0] = true,即两者任意一个成立即为true
///那么我们只要交叉建图即可
void addedge(int x, int y){
G[x].pb(y);
} bool solve(){
for (int i = ; i < * n; i += ){
if (!mark[i] && !mark[i + ]){
c = ;
if (!dfs(i)){
while (c) mark[s[--c]] = false;
if (!dfs(i + )) return false;
}
}
}
return true;
}
};
TwoSAT tar;
int n;
pair<string, string> p[maxn];
char a[], b[]; bool solve(){
for (int i = ; i < n; i++){
for (int j = i + ; j < n; j++){
if (p[i].fi == p[j].fi){
if (p[i].se == p[j].se) return false;
else
tar.addedge(i*, i*+), tar.addedge(j*, j*+);
}
else {
if (p[i].fi == p[j].se){
tar.addedge(i*, j*), tar.addedge(j*+, i*+);
}
if (p[i].se == p[j].fi){
tar.addedge(i*+, j*+), tar.addedge(j*, i*);
}
if (p[i].se == p[j].se){
tar.addedge(i*+, j*), tar.addedge(j*+, i*);
}
}
}
}
if (!tar.solve()) return false;
puts("YES");
for (int i = ; i < * n; i += ){
if (tar.mark[i]){
cout << p[i/].fi << endl;
}
else {
cout << p[i/].se << endl;
}
}
return true;
} int main(){
cin >> n;
tar.init(n);
for (int i = ; i < n; i++){
scanf("%s%s", a, b);
p[i].fi = p[i].fi + a[] + a[] + a[];
p[i].se = p[i].se + a[] + a[] + b[];
}
if (!solve()) puts("NO");
return ;
}

2-sat Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D的更多相关文章

  1. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)

    Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) 说一点东西: 昨天晚上$9:05$开始太不好了,我在学校学校$9:40$放 ...

  2. 树的性质和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步,且这个步数是向上取 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. Codeforces Round #403 (Div. 1, based on Technocup 2017 Finals)

    Div1单场我从来就没上过分,这场又剧毒,半天才打出B,C挂了好几次最后还FST了,回紫了. AC:AB Rank:340 Rating:2204-71->2133 Div2.B.The Mee ...

  9. 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 ...

随机推荐

  1. Java课程实验报告 实验四 Java网络编程及安全

    北京电子科技学院(BESTI) 实     验    报     告 课程:Java程序设计     班级:1352     姓名:吕松鸿  学号:20135229 成绩:               ...

  2. 按照Right-BICEP要求设计的测试用例

    测试用例: 测试方法:Right-BICEP 测试要求: Right-结果是否正确? B-是否所有的边界条件都是正确的? P-是否满足性能要求? 题目是否有重复? 数量是否可定制? 数值范围是否可定制 ...

  3. 团队Alpha冲刺(二)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示组内 ...

  4. IDEA + SSH OA 第一天(Hibernate : Mapping (RESOURCE) not found)

    切入主题,看看今天的错误是如何发生的: 首先这是我的项目路径,java 是 Sources Root , resources 是 Resources Root ,放了所需要的配置文件,其中 Hiber ...

  5. jQuery之offset,position

    获取/设置标签的位置数据 * offset(): 相对页面左上角的坐标 * position(): 相对于父元素左上角的坐标. 需求: 1. 点击 btn1 打印 div1 相对于页面左上角的位置 打 ...

  6. 1029对c语言文法的理解

    <程序>→<外部声明>|<程序><外部声明> <外部声明>→<函数定义>|<声明> <函数定义>→< ...

  7. 【转】mysql force Index 强制索引

    其他强制操作,优先操作如下: mysql常用的hint 对于经常使用oracle的朋友可能知道,oracle的hint功能种类很多,对于优化sql语句提供了很多方法.同样,在mysql里,也有类似的h ...

  8. Java 线程池详解

    Executors创建线程池 Java中创建线程池很简单,只需要调用Executors中相应的便捷方法即可,比如Executors.newFixedThreadPool(int nThreads),但 ...

  9. js控制iframe高度自动撑开

    <iframe src="index.html" width="100%" name="" id="myiframe&quo ...

  10. ZOJ2725_Digital Deletions

    题意是这样的,一开始给你一串数字,两个人轮流操作,操作可以分为两种. 1.每次修改一个数字,使其变为一个小于当前的非负数. 2.移除中间的某一个0以及0右边的所有数字. 使得所有数字消失的游戏者获胜. ...