HDU 4115 Eliminate the Conflict

pid=4115">题目链接

题意:Alice和Bob这对狗男女在玩剪刀石头布。已知Bob每轮要出什么,然后Bob给Alice一些限制,1表示i轮和j轮Alice必须出不一样的,0表示必须出一样的。假设Alice有一局输了就算输了,否则就是赢,问Alice能否赢

思路:2-sat问题,已经Bob出什么,Alice要么就出赢的要么就出平的,然后加上m个约束就是2-sat问题了

代码:

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std; const int N = 10005; int t, n, m, x[N], sn, S[N * 2];
vector<int> g[N * 2];
bool mark[N * 2]; void init() {
for (int i = 0; i < 2 * n; i++) g[i].clear();
memset(mark, false, sizeof(mark));
} void add_edge(int u, int x, int v, int y) {
u = u * 2 + x;
v = v * 2 + y;
g[u^1].push_back(v);
g[v^1].push_back(u);
} bool dfs(int u) {
if (mark[u^1]) return false;
if (mark[u]) return true;
mark[u] = true;
S[sn++] = u;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (!dfs(v)) return false;
}
return true;
} bool solve() {
for (int i = 0; i < 2 * n; i += 2) {
if (!mark[i] && !mark[i + 1]) {
sn = 0;
if (!dfs(i)) {
for (int j = 0; j < sn; j++) mark[S[j]] = false;
sn = 0;
if (!dfs(i + 1)) return false;
}
}
}
return true;
} int main() {
int cas = 0;
scanf("%d", &t);
while (t--) {
init();
scanf("%d%d", &n, &m);
int tmp;
for (int i = 0; i < n; i++)
scanf("%d", &x[i]);
int u, v, w;
while (m--) {
scanf("%d%d%d", &u, &v, &w);
u--; v--;
int tmp = 6 - x[u] - x[v];
int a = 6 - x[u] - tmp;
int b = 6 - x[v] - tmp;
int u1, u2, v1, v2;
u2 = a > tmp; u1 = !u2;
v2 = b > tmp; v1 = !v2;
if (w == 1) {
if (x[u] == x[v]) {
add_edge(u, 1, v, 1);
add_edge(u, 0, v, 0);
} else add_edge(u, !u1, v, !v1);
} else {
if (x[u] == x[v]) {
add_edge(u, 0, v, 1);
add_edge(u, 1, u, 0);
} else {
add_edge(u, u1, u, u1);
add_edge(v, v1, v, v1);
}
}
}
printf("Case #%d: %s\n", ++cas, solve() ? "yes" : "no");
}
return 0;
}

HDU 4115 Eliminate the Conflict(2-sat)的更多相关文章

  1. hdu 4115 Eliminate the Conflict ( 2-sat )

    Eliminate the Conflict Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  2. HDU 4115 Eliminate the Conflict(2-SAT)(2011 Asia ChengDu Regional Contest)

    Problem Description Conflicts are everywhere in the world, from the young to the elderly, from famil ...

  3. HDU 4115 Eliminate the Conflict

    2-SAT,拆成六个点. #include<cstdio> #include<cstring> #include<cmath> #include<stack& ...

  4. 图论--2-SAT--HDU/HDOJ 4115 Eliminate the Conflict

    Problem Description Conflicts are everywhere in the world, from the young to the elderly, from famil ...

  5. hdu 4115 石头剪子布(2-sat问题)

    /* 意甲冠军:石头剪子布,目前已知n周围bob会有什么,对alice限制.供u,v,w:设w=0说明a,b回合必须出的一样 否则,必须不一样.alice假设输一回合就输了,否则就赢了 解: 2-sa ...

  6. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  7. HDU 5980 Find Small A(寻找小A)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  8. hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. Java中类,对象,方法的内存分配

    Java中类,对象,方法的内存分配 以下针对引用数据类型: 在内存中,类是静态的概念,它存在于内存中的CodeSegment中. 当我们使用new关键字生成对象时,JVM根据类的代码,去堆内存中开辟一 ...

  2. sh脚本写法

    1.shell注释符号: 1. 单行注释: “#” 2. 多行注释: : << ! 语句1 语句2 语句3 语句4 ! http://blog.csdn.net/lansesl2008/a ...

  3. 抓取oracle数据库耗费资源的sql语句

    oracle数据库连接业务系统,而有些sql语句的执行严重影响了oracle的性能,就如同mysql的慢查询一样,mysql可以开启慢查询日志定位这些造成数据库性能下降的语句,而oracle同样可以做 ...

  4. JS数组专题1️⃣ ➖ 数组扁平化

    一.什么是数组扁平化 扁平化,顾名思义就是减少复杂性装饰,使其事物本身更简洁.简单,突出主题. 数组扁平化,对着上面意思套也知道了,就是将一个复杂的嵌套多层的数组,一层一层的转化为层级较少或者只有一层 ...

  5. Java多线程的同步方式和锁机制

    Object.wait(miliSec)/notify()/notifyAll() 线程调用wait()之后可以由notify()唤醒,如果指定了miliSec的话也可超时后自动唤醒.wait方法的调 ...

  6. 关于JVM内存模型,GC策略以及类加载器的思考

    JVM内存模型 Sun在2006年将Oracle JDK开源最终形成了Open JDK项目,两者在绝大部分的代码上都保持一致.JVM的内存模型是围绕着原子性(操作有且仅有一个结果).可见性(racin ...

  7. 条款17:以独立语句将newed对象置入智能指针(Store newed objects in smart pointers in standalone statements)

    NOTE: 1.以独立语句将newed对象存储于智能指针内.如果不这样做,一旦异常被抛出,有可能导致难以察觉的资源泄漏.

  8. 第二天,学习if,变量,注释

    zz_age = 31guss_age=int(input("input your answer:"))if guss_age == zz_age: print ("Yo ...

  9. Spider-天眼查字体反爬

    字体反爬也就是自定义字体反爬,通过调用自定义的woff文件来渲染网页中的文字,而网页中的文字不再是文字,而是相应的字体编码,通过复制或者简单的采集是无法采集到编码后的文字内容! 1.思路 近期在爬取天 ...

  10. Java中的事务——全局事务与本地事务

    转载,原文来源 http://www.hollischuang.com Java事务的类型有三种:JDBC事务.JTA(Java Transaction API)事务.容器事务.这是从事务的实现角度区 ...