CodeForces 228E The Road to Berland is Paved With Good Intentions (2-Sat)
来源:互联网 发布:135端口入侵 编辑:程序博客网 时间:2024/06/05 19:48
题目地址:
http://codeforces.com/problemset/problem/228/E
题意:
一幅图,100个点,最多10000条边,边权为0或1, 每次可以选择一个点,将与之相连的边取反(0变为1,1变为0) 问最后能不能将所有边变为1,若能,输出索取的点,否则,输出Impossible
思路:每个点只能选或不选,对于u-v 边,若是1,uv只能都选或都不选,若是0,只能u选时v不选,和v选时u不选
#include <iostream>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <sstream>#include <string>#include <vector>#include <cstdio>#include <ctime>#include <bitset>#include <algorithm>#define SZ(x) ((int)(x).size())#define ALL(v) (v).begin(), (v).end()#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)#define REP(i,n) for ( int i=1; i<=int(n); i++ )using namespace std;typedef long long ll;const int N = 1200;vector<int>g[N];int n,m;bool mark[N];int sta[N],top;inline void add_edge(int u,int c1,int v,int c2){ u=2*(u-1)+c1,v=2*(v-1)+c2; g[u].push_back(v); g[v].push_back(u);}bool dfs(int u){ if(mark[u^1]) return false; if(mark[u]) return true; mark[u]=1; sta[++top]=u; int sz=g[u].size(); for(int i=0;i<sz;i++) { int v=g[u][i]; if(dfs(v)==false) return false; } return true;}bool judge(){ for(int i=0;i<n;i++) { if(!mark[i]&&!mark[i^1]) { top=0; if(dfs(i)==false) { for(int j=1;j<=top;j++) mark[sta[j]]=0; top=0; if(dfs(i^1)==false) return false; } } } return true;}void ini(){ for(int i=0;i<n;i++) g[i].clear(); memset(mark,0,sizeof(mark)); top=0;}int main(){ while(~scanf("%d%d",&n,&m)) { n*=2; ini(); for(int i=1;i<=m;i++) { int u,v,c; scanf("%d%d%d",&u,&v,&c); if(c==0) add_edge(u,1,v,0),add_edge(u,0,v,1); else add_edge(u,1,v,1),add_edge(u,0,v,0); } if(judge()) { int cnt = 0; for(int i=0;i<n;i+=2) if(mark[i]) cnt++; cout<<cnt<<endl; for(int i=0;i<n;i+=2) if(mark[i])printf("%d ",i/2+1); } else puts("Impossible"); } return 0;}
0 0
- CodeForces 228E The Road to Berland is Paved With Good Intentions 2Sat求解
- CodeForces 228E The Road to Berland is Paved With Good Intentions (2-Sat)
- Codeforces Round #141 (Div. 2)E. The Road to Berland is Paved With Good Intentions(2-SAT模板题)
- Codeforces 228E The Road to Berland is Paved With Good Intentions 枚举dfs判断可行性 || 并查集
- The Road to Performance Is Littered with Dirty Code Bombs
- The Road to Performance Is Littered with Dirty Code Bombs
- codeforces 721E. Road to Home
- Codeforces Round #280 (Div. 2)E good
- Codeforces 876E:2-SAT 或者 瞎搞
- [2-SAT] Codeforces #876E. National Property
- 【2-SAT】Codeforces 876E National Property
- Codeforces Round #287 (Div. 2) 507E E. Breaking Good
- The road to a lover’s house is never long
- CodeForces 240E E. Road Repairs 树
- Breaking Good - CodeForces 507E
- The feeling is good
- The road to xunlei
- The Road to Success
- 分割窗口(动态,静态)
- 阿里巴巴产品实习生9天
- LeetCode Number of Islands
- GCD关于队列和函数对于调用线程的影响
- 我的 Xcode 插件单
- CodeForces 228E The Road to Berland is Paved With Good Intentions (2-Sat)
- 【2015_07_09】迷失
- LeetCode228——Summary Ranges
- dedecms(织梦)在后台新建新的模型时 ,点击编辑的时候 更新时间不能弹出
- Java字符参与运算和字符串参与运算
- 初次使用cocoapods注意事项
- LeetCode Bitwise AND of Numbers Range
- CDOJ 763 树上的鸟儿(水题/模拟题)
- LeetCode Happy Number