poj 3905 Perfect Election 【2-sat 简单建图】
来源:互联网 发布:领导力的文书 知乎 编辑:程序博客网 时间:2024/05/17 08:10
Description
Input
Accepted answers to the poll questionEncodingI would be happy if at least one from i and j is elected.+i +jI would be happy if at least one from i and j is not elected.-i -jI would be happy if i is elected or j is not elected or both events happen.+i -jI would be happy if i is not elected or j is elected or both events happen.-i +j
The input data are separated by white spaces, terminate with an end of file, and are correct.
Output
Sample Input
3 3 +1 +2 -1 +2 -1 -3 2 3 -1 +2 -1 -2 +1 -2 2 4 -1 +2 -1 -2 +1 -2 +1 +2 2 8 +1 +2 +2 +1 +1 -2 +1 -2 -2 +1 -1 +1 -2 -2 +1 -1
Sample Output
1101
Hint
For the first data set the result of the problem is 1; there are several perfect election outcomes, e.g. 1 is not elected, 2 is elected, 3 is not elected. The result for the second data set is justified by the perfect election outcome: 1 is not elected, 2 is not elected. The result for the third data set is 0. According to the answers -1 +2 and -1 -2 the candidate 1 must not be elected, whereas the answers +1 -2 and +1 +2 say that candidate 1 must be elected. There is no perfect election outcome. For the fourth data set notice that there are similar or identical poll answers and that some answers mention a single candidate. The result is 1.
题意:有N个候选人,给出M个限制条件。这些条件可以分成4类
1,+i +j 表示 i 和 j 至少选一个;
2,-i -j 表示 i 和 j 最多选一个;
3,+i -j 表示 选i 和 不选j 最少成立一个 ;
4,-i +j 表示 不选i 和 选j 最少成立一个;
问你有没有一种方案满足M个条件。
建图: 我用Ai表示i 被选上,!Ai表示i没有被选上。
对于1则有 !Ai -> Aj 和 !Ai -> Aj
对于2则有 Ai -> !Aj 和 Aj -> !Ai
对于3则有 Aj -> Ai 和 !Ai -> !Aj
对于4则有 !Aj -> !Ai 和 Ai -> Aj
建好图后 就是tarjan求SCC,然后判断是否矛盾即可。
AC代码:
#include <cstdio>#include <cstring>#include <queue>#include <stack>#include <vector>#include <algorithm>#define MAXN 2000+10#define INF 1000000#define eps 1e-5using namespace std;vector<int> G[MAXN];int low[MAXN], dfn[MAXN];int dfs_clock;int sccno[MAXN], scc_cnt;stack<int> S;bool Instack[MAXN];int N, M;void init(){for(int i = 1; i <= 2*N; i++) G[i].clear();}void getMap(){int i, j;char op1, op2;while(M--){ scanf(" %c%d %c%d", &op1, &i, &op2, &j);if(op1 == '+' && op2 == '+'){G[i + N].push_back(j);G[j + N].push_back(i);} else if(op1 == '-' && op2 == '-'){G[i].push_back(j + N);G[j].push_back(i + N);}else if(op1 == '+' && op2 == '-'){G[i + N].push_back(j + N);//i若没有被选上 j一定没有被选上 G[j].push_back(i);//j被选上 i一定被选上 }else{G[i].push_back(j);//i被选上 j一定被选上 G[j + N].push_back(i + N);//j没有被选上 i一定没有被选上}}}void tarjan(int u, int fa){int v;low[u] = dfn[u] = ++dfs_clock;S.push(u);Instack[u] = true;for(int i = 0; i < G[u].size(); i++){v = G[u][i];if(!dfn[v]){tarjan(v, u);low[u] = min(low[u], low[v]);}else if(Instack[v])low[u] = min(low[u], dfn[v]);}if(low[u] == dfn[u]){scc_cnt++;for(;;){v = S.top(); S.pop();sccno[v] = scc_cnt;Instack[v] = false;if(v == u) break;}}}void find_cut(int l, int r){memset(low, 0, sizeof(low));memset(dfn, 0, sizeof(dfn));memset(sccno, 0, sizeof(sccno));memset(Instack, false, sizeof(Instack));dfs_clock = scc_cnt = 0;for(int i = l; i <= r; i++)if(!dfn[i]) tarjan(i, -1);}void solve(){for(int i = 1; i <= N; i++){if(sccno[i] == sccno[i+N]){printf("0\n");return ;}}printf("1\n");}int main(){while(scanf("%d%d", &N, &M) != EOF){init();getMap();find_cut(1, 2*N);solve();}return 0;}
- poj 3905 Perfect Election 【2-sat 简单建图】
- POJ 3905 - Perfect Election 简单2-sat
- POJ 3905 Perfect Election(简单2-SAT)
- poj 3905 Perfect Election(2-SAT判断简单)
- 【POJ】3905 Perfect Election 2-sat
- POJ 3905 Perfect Election(2-sat)
- POJ 3905 Perfect Election(2-sat)
- poj 3905 Perfect Election 2-sat
- POJ - 3905 Perfect Election(2-SAT)
- POJ 3905 Perfect Election(2-SAT)
- poj--3905--Perfect Election(2-sat)
- Poj 3905 Perfect Election(2-sat 可行性判定)
- POJ 3905 Perfect Election (初学2-Sat)
- 2-SAT:以 POJ 3905 - Perfect Election 为例
- Poj 3905 Perfect Election【2-SAT-------Tarjan强连通】
- POJ 3905Perfect Election 2-sat判断可行解
- poj3905 Perfect Election (2-SAT)
- POJ 3905 Perfect Election
- 关于RTP时间戳及多媒体通信同步的问题/H264关于RTP协议的实现
- 暑假夯实安卓基础系列二
- [cocos2dx]抽象元素——CCCamera摄像机类
- web实现验证码和简单的登录注册
- 文章标题
- poj 3905 Perfect Election 【2-sat 简单建图】
- 没有任何关闭socket的日志,客户端和服务端进程都在, 网络连接完好, 为什么进行某操作后好好的tcp连接莫名其妙地断了呢?
- hdu 5305 Friends(2015多校第二场第6题)记忆化搜索
- 动态代理
- Jetson TK1
- 红枣银耳莲子汤
- 第二章(变量,数据类型和变量名,数据输入)
- BZOJ 1090 [SCOI2003]字符串折叠 区间DP
- 七月二十二日 Linux操作系统c语言学习笔记