题目大意:有三个任务A、B、C,n个已知年龄的人。A任务只能被年龄不小于平均年龄的人做,B任务只能被平均年龄以下的人做,C任务不限,相互讨厌的两个人不能做同一件任务,现在已知厌恶关系,求一种任务分配方案。

题目分析:每个人都有两个选择,对于年龄大的人,设选A为真,选C为假,对年龄小的人,设选B为真,选C为假。相互讨厌的两个人X、Y若同类,X、Y只能一真一假,也就是X、Y不能同真也不能同假,连边:X1->Y0、Y1->X0、X0->Y1、Y0->X1;若X、Y不是同类,则X、Y不能同时为假,连边:X0->Y1、Y0->X1。

代码如下:

# include<iostream>
# include<cstdio>
# include<vector>
# include<cstring>
# include<algorithm>
using namespace std; const int maxn=100005;
vector<int>G[2*maxn];
int mark[2*maxn],Age[maxn],S[maxn],cnt;
double x; bool same(int a,int b)
{
if(Age[a]>=x&&Age[b]>=x)
return true;
if(Age[a]<x&&Age[b]<x)
return true;
return false;
} bool dfs(int u)
{
if(mark[u^1]) return false;
if(mark[u]) return true;
mark[u]=1;
S[cnt++]=u;
for(int i=0;i<G[u].size();++i)
if(!dfs(G[u][i]))
return false;
return true;
} bool judge(int n)
{
memset(mark,0,sizeof(mark));
for(int i=0;i<2*n;i+=2){
cnt=0;
if(!dfs(i)){
while(cnt>0) mark[S[--cnt]]=0;
if(!dfs(i+1)) return false;
}
}
return true;
} int main()
{
int n,m,a,b;
while(scanf("%d%d",&n,&m)&&(n+m))
{
for(int i=0;i<2*n;++i) G[i].clear();
x=0.0;
for(int i=0;i<n;++i){
scanf("%d",Age+i);
x+=Age[i];
}
x/=n;
while(m--)
{
scanf("%d%d",&a,&b);
--a,--b;
G[2*a+1].push_back(2*b);
G[2*b+1].push_back(2*a);
if(same(a,b)){
G[2*a].push_back(2*b+1);
G[2*b].push_back(2*a+1);
}
}
if(!judge(n))
printf("No solution.\n");
else for(int i=0;i<2*n;i+=2){
if(!mark[i])
printf("C\n");
else
printf("%c\n",(Age[i/2]>=x)?'A':'B');
}
}
return 0;
}

  

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

  1. UVALive - 3713 - Astronauts(图论——2-SAT)

    Problem   UVALive - 3713 - Astronauts Time Limit: 3000 mSec Problem Description Input The input cont ...

  2. UVALive - 3713 Astronauts

    给定n个宇航员的年龄,平均年龄为 ave,根据下列要求分配任务: B任务只能分配给年龄<ave的宇航员: A任务只能分配给年龄>=ave的宇航员: C任务可以任意分配. 给定m组互相憎恨的 ...

  3. UVALive 3713 Astronauts (2-SAT,变形)

    题意: 有A,B,C三种任务,每个人必获得1个任务,大于等于平均年龄的可以选择A和C,小于平均年龄的可以选择B和C.这些人有一些是互相讨厌的,必须不能执行同任务,问能否安排他们工作?若行,输出任意一组 ...

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

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

  5. 【UVALive - 3713】Astronauts (2-SAT)

    题意: 有n个宇航员,按照年龄划分,年龄低于平均年龄的是年轻宇航员,而年龄大于等于平均年龄的是老练的宇航员. 现在要分配他们去A,B,C三个空间站,其中A站只有老练的宇航员才能去,而B站是只有年轻的才 ...

  6. Astronauts UVALive - 3713(2-SAT)

    大白书例题 #include <iostream> #include <cstdio> #include <sstream> #include <cstrin ...

  7. UVA 3713 Astronauts

    The Bandulu Space Agency (BSA) has plans for the following three space missions: • Mission A: Landin ...

  8. 2-sat 分类讨论 UVALIVE 3713

    蓝书326 //看看会不会爆int!数组会不会少了一维! //取物问题一定要小心先手胜利的条件 #include <bits/stdc++.h> using namespace std; ...

  9. LA 3713 Astronauts

    给个题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=sh ...

  10. UVA Live 3713 Astronauts (2-SAT)

    用布尔变量表示状态,把限制条件转化为XνY的形式以后跑2SAT,根据变量取值输出方案. #include<bits/stdc++.h> using namespace std; ; #de ...

随机推荐

  1. 189. Rotate Array(两次反转)

    DescriptionHintsSubmissionsDiscussSolution   Pick One Rotate an array of n elements to the right by  ...

  2. hdu2609 How many

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=2609 题目: How many Time Limit: 2000/1000 MS (Java/Others ...

  3. javascript 理解对象--- 定义多个属性和读取属性的特性

    一 定义多个属性 ECMAScript5 定义了一个Object.defineProperties()方法,用于定义多个属性.此方法接受两个对象参数: 第一个对象:要添加或修改其属性的对象 第二个对象 ...

  4. 如何高效的遍历HashMap 以及对key 进行排序

    Map<Integer ,Object> map = new HashMap<Integer,Object>(); for(int i = 0; i<=100;i++){ ...

  5. hTML5 多图上传预览

    <p> <label>请选择一个文件:</label> <input type="file" id="file" mu ...

  6. 20145314郑凯杰《信息安全系统设计基础》第9周学习总结 PART B

    20145314郑凯杰<信息安全系统设计基础>第9周学习总结 PART B 明确教材学习目标 注意每个系统调用的参数.返回值,会查帮助文档 阅读教材,完成课后练习(书中有参考答案),考核: ...

  7. markdown工作随笔总结

    1. 锚点 (使用方法和链接很像) ## 目录 1. [命名](#命名) ....... **[返回顶部](#目录)** ## 命名 ###命名原则 可以从返回顶部回到目录,也可以点击目录的命名跳到命 ...

  8. nuget sources

    https://docs.microsoft.com/en-us/nuget/tools/cli-ref-sources https://gemfury.com/help/nuget-server/ ...

  9. 第七篇:Spark SQL 源码分析之Physical Plan 到 RDD的具体实现

    /** Spark SQL源码分析系列文章*/ 接上一篇文章Spark SQL Catalyst源码分析之Physical Plan,本文将介绍Physical Plan的toRDD的具体实现细节: ...

  10. 【cs231n】神经网络学习笔记3

    + mu) * v # 位置更新变了形式 对于NAG(Nesterov's Accelerated Momentum)的来源和数学公式推导,我们推荐以下的拓展阅读: Yoshua Bengio的Adv ...