大白书例题

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(a, n) for(int i=a; i<=n; i++)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = ; struct TwoSAT {
int n;
vector<int> G[maxn*];
bool mark[maxn*];
int S[maxn*], c; 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));
} void add_clause(int x, int xval, int y, int yval) {
x = x * + xval;
y = y * + yval;
G[x^].push_back(y);
G[y^].push_back(x);
} 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 solver; int n, m, total_age, age[maxn]; int is_young(int x) {
return age[x] * n < total_age;
} int main() {
while(scanf("%d%d", &n, &m) == && n) {
total_age = ;
for(int i = ; i < n; i++) {
scanf("%d", &age[i]);
total_age += age[i];
} solver.init(n);
for(int i = ; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
a--;
b--;
if(a == b) continue;
solver.add_clause(a, , b, ); //不能同时为真
if(is_young(a) == is_young(b))
solver.add_clause(a, , b, ); //不能同时为假
} if(!solver.solve()) printf("No solution.\n");
else {
for(int i = ; i < n; i++)
if(solver.mark[i*]) printf("C\n"); //设的是选c为真 因为年龄小和年龄大的c是相同的 a和b不好直接判断 所以只要c被标记了 那么就是选了c
else if(is_young(i)) printf("B\n"); //如果没选c 那就判断是不是年龄小的
else printf("A\n");
}
}
return ;
}

Astronauts UVALive - 3713(2-SAT)的更多相关文章

  1. 训练指南 UVALive - 3713 (2-SAT)

    layout: post title: 训练指南 UVALive - 3713 (2-SAT) author: "luowentaoaa" catalog: true mathja ...

  2. UVALive - 3211 (2-SAT + 二分)

    layout: post title: 训练指南 UVALive - 3211 (2-SAT + 二分) author: "luowentaoaa" catalog: true m ...

  3. 训练指南 UVALive - 3415(最大点独立集)

    layout: post title: 训练指南 UVALive - 3415(最大点独立集) author: "luowentaoaa" catalog: true mathja ...

  4. 训练指南 UVALive - 5135 (双连通分量)

    layout: post title: 训练指南 UVALive - 5135 (双连通分量) author: "luowentaoaa" catalog: true mathja ...

  5. UVALive - 6436 —(DFS+思维)

    题意:n个点连成的生成树(n个点,n-1条边,点与点之间都连通),如果某个点在两点之间的路径上,那这个点的繁荣度就+1,问你在所有点中,最大繁荣度是多少?就比如上面的图中的C点,在A-B,A-D,A- ...

  6. Remember the Word UVALive - 3942(dp+trie)

    题意: 给S个不同的单词和一个长字符串 问将其分解为若干个单词有多少种方法(单词可重复使用) 解析: dp[i]表示在这个字符串中以某个位置i为起点的 的一段子字符串 则这个子字符串若存在某个前缀恰好 ...

  7. UVALive 5873 (几何+思维)

    唉 被秀了... 还是太弱,说好的数形结合呢,列个式子出来后就被吓到了,然后就懵逼了. 题意: 有一条狗,从原点出发,沿n个向量走,每个向量只走一次,沿着一个向量(x,y)走时,既可以往(x,y)方向 ...

  8. POJ 3678 Katu Puzzle(2 - SAT) - from lanshui_Yang

    Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...

  9. 学习笔记(two sat)

    关于two sat算法 两篇很好的论文由对称性解2-SAT问题(伍昱), 赵爽 2-sat解法浅析(pdf). 一些题目的题解 poj 3207 poj 3678 poj 3683 poj 3648 ...

随机推荐

  1. L018-crond的生产场景经验小节

    L018-crond的生产场景经验小节 怎么说呢,其实L018这节课还是巩固crond的知识,前半堂课主要是解决上堂课老师留的作业(在L017已经更新,拉到最后),然后剩下的半堂客主要是讲解了一些生产 ...

  2. 用MYSQLworkbench导出数据excel

    步骤: 1.先从数据库中将表导出,右键需要导出的表格——>Table Data Export Wizard 2.点击Next,选择你需要把数据存放的文件路径.导出的数据格式(表格的话就默认选择C ...

  3. Jmeter如何做接口测试

    最近在学习Jmeter,记录下来,与大家分享. 1. 打开Jmeter,我用的是5.0版本,打开后如下图所示: 2. 右键测试计划,添加 - 线程(用户) - 线程组,如下如图所示: 3. 右键线程组 ...

  4. Vue学习计划基础笔记(一) - vue实例

    最近又重新看vue的文档了,计划是别人写的,之前看过一次,没有考虑太多,只考虑看懂能用就好.看完之后写过写demo,现在是零实际项目经验的,所以这一次打算细看,算是官方文档的二次产物吧,但是不是全部直 ...

  5. C/C++语言基础

    1. 一个子类中含有其他类对象,则构造函数的顺序是? 先执行基类的(如果基类当中有虚基类,要先执行虚基类的,其他基类则按照声明派生类是的顺序依次执行),在执行成员对象的,最后执行自己的. 2.spri ...

  6. linux中匹配正确的ip地址

    1.假设IP地址是规范的,没有出错误的 sed -n "/[0-9]\{1,3\}.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/p" test ...

  7. python-redis哈希模式

    命令: hset   info name hgetall info hkeys info hvlls  info m系列批量处理: ---------------------------------- ...

  8. LearnPython - Zip格式文件的解压缩

    import zipfile import os def unzip(zip_name, target_dir): files = zipfile.ZipFile(zip_name) for zip_ ...

  9. 阿里IPO法律咨询费达1580万美元 为Facebook六倍

    据路透社报道,阿里巴巴集团周五在 IPO (首次公开招股)更新文件中披露,将向美国盛信律师事务所(Simpson Thacher)以及其他为 IPO 提供咨询服务的律师事务所支付 1580 万美元的法 ...

  10. 点滴拾遗 - 自定义 Format 控制 String.Format 行为

    点击下载示例代码 String.Format 一重载方法的签名如下 public static string Format( IFormatProvider provider, string form ...