2-sat Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D
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的更多相关文章
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)
Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) 说一点东西: 昨天晚上$9:05$开始太不好了,我在学校学校$9:40$放 ...
- 树的性质和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步,且这个步数是向上取 ...
- 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) 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) 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 ...
- 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 ...
- 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 ...
随机推荐
- Scrum立会报告+燃尽图 03
此作业要求:[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2190] 一.小组介绍 组长:王一可 组员:范靖旋,王硕,赵佳璐,范洪达,祁 ...
- 第十一次作业 - Alpha 事后诸葛亮(团队)
软工 · 第十一次作业 - Alpha 事后诸葛亮(团队) 组长本次作业链接 现代软件工程 项目Postmortem 设想和目标 1.我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场 ...
- 404 Note Found 现场编程
目录 组员职责分工 github 的提交日志截图 程序运行截图 程序运行环境 GUI界面 基础功能实现 运行视频 LCG算法 过滤(降权)算法 算法思路 红黑树 附加功能一 背景 实现 附加功能二(迭 ...
- Java中按值传递与按引用传递的区别
值传递:(形式参数类型是基本数据类型):方法调用时,实际参数把它的值传递给对应的形式参数,形式参数只是用实际参数的值初始化自己的存储单元内容,是两个不同的存储单元,所以方法执行中形式参数值的改变不影响 ...
- Struts2(三)
以下内容是基于导入struts2-2.3.32.jar包来讲的 1.全局视图配置 xml标签:<global-results> <result name="error&qu ...
- (十三)Jmeter之Bean Shell 的使用(二)
该文章来自:http://www.cnblogs.com/puresoul/p/5092628.html 上一篇Jmeter之Bean shell使用(一)简单介绍了下Jmeter中的Bean she ...
- delphi 事务处理SQL语句
方法一(利用adoconnection.exe(sqlstate)): adoconnection1.begintrans;//开始事务try adoconnection1.execute(sqlst ...
- java学习 猜数字
package study; import java.util.Scanner; /** * 猜数字小游戏 * * @author carry * */ public class GuessNumbe ...
- 【.Net】net 反射15分钟速成
概述 什么是反射 Reflection,中文翻译为反射. 这是.Net中获取运行时类型信息的方式,.Net的应用程序由几个部分:‘程序集(Assembly)’.‘模块(Module)’. ...
- Wedding UVA - 11294(2-SAT男女分点)
题意: 有N-1对夫妻参加一个婚宴,所有人都坐在一个长长的餐桌左侧或者右侧,新郎和新娘面做面坐在桌子的两侧.由于新娘的头饰很复杂,她无法看到和她坐在同一侧餐桌的人,只能看到对面餐桌的人.任意一对夫妻不 ...