2-sat 分类讨论 UVALIVE 3713
2024-10-18 17:39:37
蓝书326
//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 1e5 + ;
struct TwoSat{
int n, c, s[maxn * ];
bool mark[maxn << ];
vector<int> G[maxn << ]; void init(int n){
this->n = n;
for (int i = ; i < * n; i++) G[i].clear();
memset(mark, false, sizeof(mark));
} void add_edge(int x, int xval, int y, int yval){
x = x * + xval, y = y * + yval;
G[x ^ ].pb(y), G[y ^ ].pb(x);
} 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;
} 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 sat2;
int n, m;
int age[maxn]; int main(){
while (scanf("%d%d", &n, &m) && (n + m) > ){
sat2.init(n);
int tot = ;
for (int i = ; i < n; i++){
scanf("%d", age + i);
tot += age[i];
}
/*
假设A和B都是1,C是0
if两者属于同一个任务,那么,两者不能分配到同一个任务,
所以两者不能同时为true和同时为false if两者属于不同的任务,那么两者不能同时为false
*/
for (int i = ; i < m; i++){
int u, v; scanf("%d%d", &u, &v);
u--, v--;
if (u == v) continue;
int tyu = age[u] * n < tot, tyv = age[v] * n < tot;
sat2.add_edge(u, , v, );
if (tyu == tyv)
sat2.add_edge(u, , v, );
}
if (!sat2.solve()) puts("No solution.");
else {
for (int i = ; i < n; i++){
if (sat2.mark[i * ]) puts("C");
if (sat2.mark[i * + ]) {
if (age[i] * n < tot) puts("B");
else puts("A");
}
}
}
}
return ;
}
最新文章
- js继承相关
- Python检测IP合法 是否为公网IP
- Magento订单打印(pdf格式)
- Python学习总结15:时间模块datetime &; time &; calendar (二)
- Zabbix监控Linux磁盘I/O
- 企业生产环境下不同业务的linux分区建议
- hadoop2.2编程:从default mapreduce program 来理解mapreduce
- The Contiki build system 编译系统
- shell中eval命令妙用——变量嵌套替换
- 【Android】自带Theme
- PuTsangTo
- pthread_cond_wait的spurious wakeup问题
- ajax请求window.open()被拦截
- Web安全之XSS Platform搭建及使用实践
- JavaFX 简介
- 《Oracle DBA工作笔记:运维、数据迁移与性能调优》 PDF 下载
- ID3-C45-CART
- python 复数类型
- pseudotime专题
- Extjs获取Form中的数据