Astronauts UVALive - 3713(2-SAT)
大白书例题
- #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)的更多相关文章
- 训练指南 UVALive - 3713 (2-SAT)
layout: post title: 训练指南 UVALive - 3713 (2-SAT) author: "luowentaoaa" catalog: true mathja ...
- UVALive - 3211 (2-SAT + 二分)
layout: post title: 训练指南 UVALive - 3211 (2-SAT + 二分) author: "luowentaoaa" catalog: true m ...
- 训练指南 UVALive - 3415(最大点独立集)
layout: post title: 训练指南 UVALive - 3415(最大点独立集) author: "luowentaoaa" catalog: true mathja ...
- 训练指南 UVALive - 5135 (双连通分量)
layout: post title: 训练指南 UVALive - 5135 (双连通分量) author: "luowentaoaa" catalog: true mathja ...
- UVALive - 6436 —(DFS+思维)
题意:n个点连成的生成树(n个点,n-1条边,点与点之间都连通),如果某个点在两点之间的路径上,那这个点的繁荣度就+1,问你在所有点中,最大繁荣度是多少?就比如上面的图中的C点,在A-B,A-D,A- ...
- Remember the Word UVALive - 3942(dp+trie)
题意: 给S个不同的单词和一个长字符串 问将其分解为若干个单词有多少种方法(单词可重复使用) 解析: dp[i]表示在这个字符串中以某个位置i为起点的 的一段子字符串 则这个子字符串若存在某个前缀恰好 ...
- UVALive 5873 (几何+思维)
唉 被秀了... 还是太弱,说好的数形结合呢,列个式子出来后就被吓到了,然后就懵逼了. 题意: 有一条狗,从原点出发,沿n个向量走,每个向量只走一次,沿着一个向量(x,y)走时,既可以往(x,y)方向 ...
- 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 ...
- 学习笔记(two sat)
关于two sat算法 两篇很好的论文由对称性解2-SAT问题(伍昱), 赵爽 2-sat解法浅析(pdf). 一些题目的题解 poj 3207 poj 3678 poj 3683 poj 3648 ...
随机推荐
- day2 HTML - body
<body>内常用标签 1.基本标签 所有标签分为: # 块级标签: div(白板),H系列(加大加粗),p标签(段落和段落之间有间距) # 行内标签: span(白板) 1. 图标, ...
- ElasticSearch 聚合查询百分比
这里用的是es5.6.9 bucket_script :它执行一个脚本,该脚本可以对多桶聚合中的指定度量执行每桶计算,指定的度量标准必须为数字,并且脚本必须返回数值. 官方语法 https://www ...
- Intellij IDEA破解激活
本文使用破解补丁激活Intellij IDEA 第一步:下载破解补丁(http://idea.lanyus.com/) 第二步:将下载好的补丁放置磁盘的任意位置,下面是我放置的位置 第三步:打开Int ...
- PHP双向队列
假定队列的左边为头部,右边为尾部 <?php class myDeque { private $deque=array(); /** *头部进队列 */ public function lPus ...
- 设计模式C++实现(1)——策略(Strategy)模式
目录 策略模式 应用案例 实现的关键 Talk is cheap,let's See The Code 设计思想 参考 策略模式 策略模式定义了一系列算法和行为(也就是策略),他们可以在运行时相互替换 ...
- Python基础入门(迭代器和生成器)
1 Python迭代器 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束. 迭代器只能往前不会后退. 迭代器有两个基本的方法:iter() 和 ...
- Dsniff简介
原文发表于:2010-09-25 转载至cu于:2012-07-21 前两天因为看局域网安全的视频中介绍dsniff,也想自己安装下来看看效果.简单的使用没什么难的(高级使用就需要研究文档了),但是安 ...
- Python20-Day02
1.数据 数据为什么要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同类型的数据表示: 数据类型 数字(整形,长整形,浮点型,复数),字符串,列表,元组,字典,集合 2.字符串 1.按索引取 ...
- linux-ubuntu配置通过22端口远程连接
当安装好ubuntu后获取到对应主机的ip地址,要想通过类似xshell这样的远程连接工具连接到ubuntu主机,需要在你刚刚安装好的ubuntu主机上安装openssh这个软件,才能通过远程来连接u ...
- Scrum立会报告+燃尽图(Beta阶段第五次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2387 项目地址:https://coding.net/u/wuyy694 ...