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. Spring Boot(八)集成Spring Cache 和 Redis

    在Spring Boot中添加spring-boot-starter-data-redis依赖: <dependency> <groupId>org.springframewo ...

  2. 爬虫学习之-操作mysql

    在操作数据库的时候,python2中一般使用mysqldb,但在python3中已经不在支持mysqldb了,我们可以用pymysql和mysql.connector.本文的所有操作都是在python ...

  3. 使用python操作Memcache、Redis、RabbitMQ、

    Memcache 简述: Memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的.需要 ...

  4. [C/C++] 虚函数机制

    转自:c++ 虚函数的实现机制:笔记 1.c++实现多态的方法 其实很多人都知道,虚函数在c++中的实现机制就是用虚表和虚指针,但是具体是怎样的呢?从more effecive c++其中一篇文章里面 ...

  5. IE 之 userData 模拟 localStorage

    引 chrome,  safari, firefox, ie 9都支持 localStorage.  但可恶的是,中国 ie 6 占有最大的比例. 使用 cookie 不但容量有限,而且给我们增加了不 ...

  6. bzoj1093[ZJOI2007]最大半连通子图(tarjan+拓扑排序+dp)

    Description 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u ...

  7. 常用yum源

    #epel源 [myepel] name=zheda baseurl=http://mirrors.zju.edu.cn/epel/6/x86_64/ gpgcheck= enabled= #mysq ...

  8. P2812 校园网络【[USACO]Network of Schools加强版】

    题目背景 浙江省的几所OI强校的神犇发明了一种人工智能,可以AC任何题目,所以他们决定建立一个网络来共享这个软件.但是由于他们脑力劳动过多导致全身无力身体被♂掏♂空,他们来找你帮助他们. 题目描述 共 ...

  9. 【bzoj3295】[Cqoi2011]动态逆序对 树套树 线段树套替罪羊树

    这个东西,关于某个数的逆序对数为在他之前比他大的在他之后比他小的数的数目的和,所以删除之前先把这个减去就好了 人傻自带超大常数,中间由于内存池开小了所以运行错误. #include<cstdio ...

  10. Debugging QML Applications

    Debugging QML Applications Console API Log console.log, console.debug, console.info, console.warn an ...