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

题意:

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

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

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

思路:

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

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

  1. //看看会不会爆int!数组会不会少了一维!
  2. //取物问题一定要小心先手胜利的条件
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. #pragma comment(linker,"/STACK:102400000,102400000")
  6. #define LL long long
  7. #define ALL(a) a.begin(), a.end()
  8. #define pb push_back
  9. #define mk make_pair
  10. #define fi first
  11. #define se second
  12. #define haha printf("haha\n")
  13. /*
  14. 另第一个名字为a,第二个名字为b
  15. 一:
  16. 如果ai = aj,那么只能选择第二个
  17. ①如果bi = bj,那么无解
  18. ②如果bi != bj,那么两者之间连接一条边
  19. 上面是分类讨论
  20.  
  21. 二:
  22. 如果ai != aj
  23.  
  24. */
  25. const int maxn = + ;
  26. struct TwoSAT{
  27. int n;
  28. vector<int> G[maxn * ];
  29. bool mark[maxn * ];
  30. int c, s[maxn * ];///c是表示目前dfs到的个数和已经被标记的集合s
  31. bool dfs(int x){
  32. if (mark[x ^ ]) return false;
  33. if (mark[x]) return true;
  34. mark[x] = true;
  35. s[c++] = x;
  36. for (int i = ; i < G[x].size(); i++)
  37. if (!dfs(G[x][i])) return false;
  38. return true;
  39. }
  40.  
  41. void init(int n){
  42. this->n = n;
  43. for (int i = ; i < * n; i++) G[i].clear();
  44. memset(mark, , sizeof(mark));
  45. }
  46. ///利用题目条件找到x和y,即假设x1[0] | x2[1] = false;即:x1[0]|!x2[1]=false
  47. ///那么我们反转该条件:即x1[1]|!x2[0] = true,即两者任意一个成立即为true
  48. ///那么我们只要交叉建图即可
  49. void addedge(int x, int y){
  50. G[x].pb(y);
  51. }
  52.  
  53. bool solve(){
  54. for (int i = ; i < * n; i += ){
  55. if (!mark[i] && !mark[i + ]){
  56. c = ;
  57. if (!dfs(i)){
  58. while (c) mark[s[--c]] = false;
  59. if (!dfs(i + )) return false;
  60. }
  61. }
  62. }
  63. return true;
  64. }
  65. };
  66. TwoSAT tar;
  67. int n;
  68. pair<string, string> p[maxn];
  69. char a[], b[];
  70.  
  71. bool solve(){
  72. for (int i = ; i < n; i++){
  73. for (int j = i + ; j < n; j++){
  74. if (p[i].fi == p[j].fi){
  75. if (p[i].se == p[j].se) return false;
  76. else
  77. tar.addedge(i*, i*+), tar.addedge(j*, j*+);
  78. }
  79. else {
  80. if (p[i].fi == p[j].se){
  81. tar.addedge(i*, j*), tar.addedge(j*+, i*+);
  82. }
  83. if (p[i].se == p[j].fi){
  84. tar.addedge(i*+, j*+), tar.addedge(j*, i*);
  85. }
  86. if (p[i].se == p[j].se){
  87. tar.addedge(i*+, j*), tar.addedge(j*+, i*);
  88. }
  89. }
  90. }
  91. }
  92. if (!tar.solve()) return false;
  93. puts("YES");
  94. for (int i = ; i < * n; i += ){
  95. if (tar.mark[i]){
  96. cout << p[i/].fi << endl;
  97. }
  98. else {
  99. cout << p[i/].se << endl;
  100. }
  101. }
  102. return true;
  103. }
  104.  
  105. int main(){
  106. cin >> n;
  107. tar.init(n);
  108. for (int i = ; i < n; i++){
  109. scanf("%s%s", a, b);
  110. p[i].fi = p[i].fi + a[] + a[] + a[];
  111. p[i].se = p[i].se + a[] + a[] + b[];
  112. }
  113. if (!solve()) puts("NO");
  114. return ;
  115. }

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. OA_1界面

    <%@ page language="java" contentType="text/html;charset=GB18030" pageEncoding ...

  2. RAID卡服务器安装2003教程

     这里先讲讲安装系统的几个思路: 1.U盘安装法(U盘只做可启动PE,常用的大白菜,IT天空,老毛桃.....拷贝系统ISO镜像到U盘,进入PE之后找到ISO,用虚拟光驱加载,运行WIN系统安装器 ...

  3. Debugger DataSet 调试时查看DataSet

    delphi  跟踪调试的时候查看DataSet数据记录 Ctrl+F7调试 增强工具DataSethttp://edn.embarcadero.com/article/40268 http://do ...

  4. HDU——1788 Chinese remainder theorem again

    再来一发水体,是为了照应上一发水题. 再次也特别说明一下,白书上的中国剩余定理的模板不靠谱. 老子刚刚用柏树上的模板交上去,简直wa出翔啊. 下面隆重推荐安叔版同余方程组的求解方法. 反正这个版本十分 ...

  5. elsarticle模板 去掉Preprint submitted to

    参考:http://latex.org/forum/viewtopic.php?t=11123 修改elsarticle.cls文件. 我的CTeX装在c盘中,elsarticle.cls文件路径为: ...

  6. P2891 [USACO07OPEN]吃饭Dining(最大流+拆点)

    题目描述 Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she w ...

  7. String Problem HDU - 3374(最大最小表示法+循环节)

    题意: 给出一个字符串,问这个字符串经过移动后的字典序最小的字符串的首字符位置和字典序最大的字符串的首字符的位置,和能出现多少次最小字典序的字符串和最大字典序的字符串 解析: 能出现多少次就是求整个字 ...

  8. [BZOJ4653][NOI2016]区间 贪心+线段树

    4653: [Noi2016]区间 Time Limit: 60 Sec  Memory Limit: 256 MB Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],. ...

  9. 【刷题】BZOJ 2599 [IOI2011]Race

    Description 给一棵树,每条边有权.求一条简单路径,权值和等于K,且边的数量最小.N <= 200000, K <= 1000000 Input 第一行 两个整数 n, k 第二 ...

  10. 谈谈 Java 类加载机制

    概述 类加载器主要分为两类,一类是 JDK 默认提供的,一类是用户自定义的. JDK 默认提供三种类加载器: Bootstrap ClassLoader 启动类加载器:每次执行 java 命令时都会使 ...