Codeforces Round #141 (Div. 2)E. The Road to Berland is Paved With Good Intentions(2-SAT模板题)
来源:互联网 发布:软件著作权律师 编辑:程序博客网 时间:2024/06/05 17:09
Berland has n cities, some of them are connected by bidirectional roads. For each road we know whether it is asphalted or not.
The King of Berland Valera II wants to asphalt all roads of Berland, for that he gathered a group of workers. Every day Valera chooses exactly one city and orders the crew to asphalt all roads that come from the city. The valiant crew fulfilled the King's order in a day, then workers went home.
Unfortunately, not everything is as great as Valera II would like. The main part of the group were gastarbeiters — illegal immigrants who are enthusiastic but not exactly good at understanding orders in Berlandian. Therefore, having received orders to asphalt the roads coming from some of the city, the group asphalted all non-asphalted roads coming from the city, and vice versa, took the asphalt from the roads that had it.
Upon learning of this progress, Valera II was very upset, but since it was too late to change anything, he asked you to make a program that determines whether you can in some way asphalt Berlandian roads in at most n days. Help the king.
The first line contains two space-separated integers n, m — the number of cities and roads in Berland, correspondingly. Next m lines contain the descriptions of roads in Berland: the i-th line contains three space-separated integersai, bi, ci (1 ≤ ai, bi ≤ n; ai ≠ bi; 0 ≤ ci ≤ 1). The first two integers (ai, bi) are indexes of the cities that are connected by the i-th road, the third integer (ci) equals 1, if the road was initially asphalted, and 0 otherwise.
Consider the cities in Berland indexed from 1 to n, and the roads indexed from 1 to m. It is guaranteed that between two Berlandian cities there is not more than one road.
In the first line print a single integer x (0 ≤ x ≤ n) — the number of days needed to asphalt all roads. In the second line print x space-separated integers — the indexes of the cities to send the workers to. Print the cities in the order, in which Valera send the workers to asphalt roads. If there are multiple solutions, print any of them.
If there's no way to asphalt all roads, print "Impossible" (without the quotes).
4 41 2 12 4 04 3 13 2 0
43 2 1 3
3 31 2 02 3 03 1 0
Impossible
题目大意:有n个点m条边,现在在每个边上有权值1或0,现在每次触碰一个点,与他相连的所有边的权值发生翻转,要求算出怎么操作可以使得所有的边权值为1,输出操作步骤,否则输出不可能
解题思路:一开始以为是一个翻转问题,但是之后讲解是一个2-sat的问题
2-SAT问题主要针对的是对于一组数据来说,每两个数据之间存在着非或者并存的条件,也就是说在选择的时候可能存在着选A必须选B,或者选A不能选B的情况,求出一个满足条件不矛盾的顺序操作的问题
这类问题,1建图,将每个点当做两个点去处理,一个是A,一个是非A,然后根据要求进行建图
2.根据关系找出所有的强连通块,所谓的强连通块意味着在选择强连通块中一个的时候,必将其所在的强连通块内的点统统选中,对连通块进行着色
3.判断每个连通块内是否存在不符合条件或者彼此矛盾的情况,没有的话输出即可
#include<iostream> #include<cstdio>#include<stdio.h>#include<cstring>#include<cstdio>#include<climits> #include<cmath>#include<stack>#include<vector> #include <bitset> #include<algorithm> #include <queue> #include<map> #define inf 9999999; using namespace std;vector<int> tu[205],s;bool vis[205];int check[205];int a[205];int n,m;void pushs(int x,int y){tu[x].push_back(y);tu[y].push_back(x);}void dfs(int x){int i;vis[x]=1;for(i=0;i<tu[x].size();i++){int k=tu[x][i];if(vis[k]==0)dfs(k);}s.push_back(x);}void rdfs(int x,int ss){int i;vis[x]=1;check[x]=ss;for(i=0;i<tu[x].size();i++){int k=tu[x][i];if(vis[k]==0){rdfs(k,ss);}}}int main(){int k,i,x,y,l;cin>>n>>m;for(i=1;i<=m;i++){cin>>x>>y>>l;if(l==1){pushs(x,y);pushs(x+n,y+n);}else{pushs(x,y+n);pushs(x+n,y);}}memset(vis,0,sizeof(vis));for(i=1;i<=2*n;i++){if(!vis[i]){dfs(i);}}memset(vis,0,sizeof(vis));int cnt=0;for(i=s.size()-1;i>=0;i--){k=s[i];if(!vis[k]){rdfs(k,cnt++);}}for(i=1;i<=n;i++){if(check[i]==check[i+n]){cout<<"Impossible"<<endl;return 0;}}cnt=0;memset(a,0,sizeof(a));for(i=1;i<=n;i++){if(check[i]>check[i+n]){a[++cnt]=i;}}cout<<cnt<<endl;for(i=1;i<=cnt;i++)cout<<a[i]<<" ";cout<<endl;}
- 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 2Sat求解
- CodeForces 228E The Road to Berland is Paved With Good Intentions (2-Sat)
- Codeforces 228E The Road to Berland is Paved With Good Intentions 枚举dfs判断可行性 || 并查集
- Codeforces Round #280 (Div. 2)E good
- Codeforces Round #287 (Div. 2) 507E E. Breaking Good
- Codeforces Round #287 (Div. 2) E. Breaking Good
- Codeforces Round #287 (Div. 2) 507 E. Breaking Good
- Codeforces Round #287 (Div. 2) E. Breaking Good 最短路
- Codeforces Round #352 (Div. 2) B. Different is Good
- Codeforces Round #380 (Div. 2)C. Road to Cinema
- Codeforces Round #380 (Div. 2) C. Road to Cinema 二分
- Codeforces Round #219 (Div. 2) E. Watching Fireworks is Fun
- Codeforces Beta Round #57 (Div. 2) E. Enemy is weak
- Codeforces Round #103 (Div. 2) E题
- Codeforces Round #277 (Div. 2)E题
- Codeforces Round #277.5 (Div. 2)E题
- [2-SAT] Codeforces 668E #348 (VK Cup 2016 Round 2, Div. 1 Edition) E. Little Artem and 2-SAT
- DataExcel 隐藏列头,行头,显示标尺
- java面试题目
- python学习笔记(十五)标准库StringIO和BytesIO
- CodeFroces 478C. Table Decorations(贪心)
- Python3教程Web开发实战梳理-day1+day2(开发环境与基本骨架)
- Codeforces Round #141 (Div. 2)E. The Road to Berland is Paved With Good Intentions(2-SAT模板题)
- TCP/IP图解
- mac配置
- 【HDU
- UltraEdit中连接到FTP(远程服务器)
- 删除vSphere中DataStore(inactive)非活动存储
- hdu5248序列变换 二分查找 贪心
- python-基本数据类型之字典
- Android的基础控件