UVALive 3713 Astronauts (2-SAT,变形)
题意:
有A,B,C三种任务,每个人必获得1个任务,大于等于平均年龄的可以选择A和C,小于平均年龄的可以选择B和C。这些人有一些是互相讨厌的,必须不能执行同任务,问能否安排他们工作?若行,输出任意一组解。
思路:
依然是 2-SAT,只不过换了个样子,建图时不同而已。这里每个人依然有2人选择,也有冲突的出现,问题在如何找出冲突。
首先,无论是哪两人,只要互相讨厌,去抢C,必定冲突。其次,如果是同龄人(同大于等于,或同小于),那么抢他们那个年龄段的任务也会冲突。所以共计2种,每种2条边,即我选的时候,你不能选;你选的时候,我不能选。
在建好图后,进行DFS进行尝试一组可行解,所有尝试都不行时,就No Solution。有解时,如果2*i为true,那么输出时要判断这个人的年龄,给他分配对应年龄段的任务;如果i*2+1为true,那么这个人是选C的,不关年龄事直接输出。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <stack>
#include <algorithm> #include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=*+;
int age[N], n, m, col[N],s[N],c;
double even;
vector<int> vect[N]; bool isolder(int x)
{
return x>=even? : ;
}
void add_edge(int i,int j) //建图
{
//x*2+1的表示选择c,x*2表示选择非c
vect[i*+].push_back(j*); //无论哪两人,抢c必有冲突
vect[j*+].push_back(i*); if(isolder(age[i])==isolder(age[j])) //抢A或B有冲突
{
vect[i*].push_back(j*+);
vect[j*].push_back(i*+);
}
} bool color(int x)
{
if(col[x^]) return false;
if(col[x]) return true;
col[x]=;
s[c++]=x;
for(int i=; i<vect[x].size(); i++)
{
int t=vect[x][i];
if(!color(t)) return false;
}
return true;
} bool cal(int n)
{
memset(col,,sizeof(col));
memset(s,,sizeof(s));
for(int i=; i<n; i+=)
{
if(!col[i]&&!col[i+])
{
c=;
if(!color(i))
{
while(c) col[s[--c]]=;
if(!color(i+)) return false;
}
}
}
return true;
} int main()
{
freopen("input.txt", "r", stdin);
int a,b;
while(scanf("%d%d",&n,&m),n+m)
{
for(int i=n*; i>=; i--) vect[i].clear();
even=0.0;
for(int i=; i<n; i++)
{
scanf("%d",&age[i]);
even+=age[i];
}
even/=n; for(int i=; i<m; i++)
{
scanf("%d%d",&a,&b);
add_edge(--a,--b);
} if(!cal(n<<)) puts("No solution.");
else
{
for(int i=; i<n; i++) //随便输出一组合法解。
{
if(col[i*])
{
if(isolder(age[i]) ) puts("A");
else puts("B");
}
else puts("C");
}
}
}
return ;
}
AC代码
UVALive 3713 Astronauts (2-SAT,变形)的更多相关文章
- UVALive - 3713 - Astronauts(图论——2-SAT)
Problem UVALive - 3713 - Astronauts Time Limit: 3000 mSec Problem Description Input The input cont ...
- UVALive - 3713 Astronauts
给定n个宇航员的年龄,平均年龄为 ave,根据下列要求分配任务: B任务只能分配给年龄<ave的宇航员: A任务只能分配给年龄>=ave的宇航员: C任务可以任意分配. 给定m组互相憎恨的 ...
- 训练指南 UVALive - 3713 (2-SAT)
layout: post title: 训练指南 UVALive - 3713 (2-SAT) author: "luowentaoaa" catalog: true mathja ...
- 【UVALive - 3713】Astronauts (2-SAT)
题意: 有n个宇航员,按照年龄划分,年龄低于平均年龄的是年轻宇航员,而年龄大于等于平均年龄的是老练的宇航员. 现在要分配他们去A,B,C三个空间站,其中A站只有老练的宇航员才能去,而B站是只有年轻的才 ...
- Astronauts UVALive - 3713(2-SAT)
大白书例题 #include <iostream> #include <cstdio> #include <sstream> #include <cstrin ...
- UVA 3713 Astronauts
The Bandulu Space Agency (BSA) has plans for the following three space missions: • Mission A: Landin ...
- 2-sat 分类讨论 UVALIVE 3713
蓝书326 //看看会不会爆int!数组会不会少了一维! //取物问题一定要小心先手胜利的条件 #include <bits/stdc++.h> using namespace std; ...
- LA 3713 Astronauts
给个题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=sh ...
- UVA Live 3713 Astronauts (2-SAT)
用布尔变量表示状态,把限制条件转化为XνY的形式以后跑2SAT,根据变量取值输出方案. #include<bits/stdc++.h> using namespace std; ; #de ...
随机推荐
- PhantomJS实现最简单的模拟登录方案
以前写爬虫,遇到需要登录的页面,一般都是通过chrome的检查元素,查看登录需要的参数和加密方法,如果网站的加密非常复杂,例如登录qq的,就会很蛋疼 在后面,有了Pyv8,就可以把加密的js文件扔给它 ...
- 【BZOJ 2301】[HAOI2011]Problem b
Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. Input 第一行一个整数 ...
- uxpin books
http://uxpin.com/knowledge.html/?utm_source=Email+Marketing+Automation&utm_campaign=80d94e146a-l ...
- Asp.Net MVC结合ExtJs gridPanel 分页和高度自适应
Ext.onReady(function () { gridPanel(); var panel = Ext.getCmp('gridPanel'); window.onresize = functi ...
- 强连通分量Tarjan模板
#include<iostream> #include<stdio.h> #include<string.h> #include<stack> #inc ...
- hibernate映射文件基础
一.利用hibernate的eclipse插件快速生成实体类与配置文件的方法 1.首先下载安装和自己的eclipse版本配套的hibernate tools,如果是Myeclipse,在/readme ...
- 实用程序Commer的开发——U盘内容可选同步至FTP服务器
需求分析:需要在软件运行后将插入的U盘里面的文件Copy至本机上,然后可选的上传一部分至FTP服务器上. 系统设计:基于MFC的基本对话框程序:主要模块有检测U盘插入并复制文件以及上传到网络.通过对U ...
- js页面刷新一次
// var str = document.location.hash, // index = str.indexOf("#"); // if(index == 0){ // wi ...
- [转载]html5直接在网页上播放视频音频兼容所有浏览器
文章给大家分享一个html5直接在网页上播放视频兼容所有浏览器,有需要的同学可参考. HTML5可以用video标签来播放视频 当前,video 元素支持三种视频格式: 格式 IE Firefox O ...
- The 6th Zhejiang Provincial Collegiate Programming Contest->ProblemB:Light Bulb
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3203 题意:求影子的最长长度L; 当灯,人头和墙角成一条直线时(假设此时人 ...