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.

Input

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 ≤ nai ≠ 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.

Output

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).

题目大意:给一幅无向图,每条边有一个权值0或1,每选择一个点,这个点周围的边权就异或1,问能不能选择一些点,能把所有边权变成1,能则输出方案,不能则输出Impossible。

思路:2-SAT,边权为1边的两个点,只能同时选或同时不选,选和不选冲突,连边。边权为0的边的两个点,要选且只能选一个,选和选冲突,不选和不选冲突,连边。

代码(30MS):

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std; const int MAXN = ;
const int MAXV = ;
const int MAXE = MAXV * MAXV; struct Topological {
int stk[MAXV], top;
int n, ecnt, cnt;
int head[MAXV], order[MAXV], indeg[MAXV];
int to[MAXE], next[MAXE]; void init(int nn) {
memset(head, -, sizeof(head));
memset(indeg, , sizeof(indeg));
n = nn; ecnt = ;
} void add_edge(int u, int v) {
to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
++indeg[v];
} void build() {
top = cnt = ;
for(int i = ; i <= n; ++i)
if(indeg[i] == ) stk[++top] = i;
while(top) {
int u = stk[top--]; order[cnt++] = u;
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(--indeg[v] == ) stk[++top] = v;
}
}
}
} T; struct TwoSAT {//从0开始编号
int stk[MAXV], top;
int n, ecnt, dfs_clock, scc_cnt;
int head[MAXV], sccno[MAXV], pre[MAXV], lowlink[MAXV];
int to[MAXE], next[MAXE];
int select[MAXV], sccnox[MAXV]; void init(int nn) {
memset(head, -, sizeof(head));
memset(pre, , sizeof(pre));
memset(sccno, , sizeof(sccno));
n = nn, ecnt = dfs_clock = scc_cnt = ;
} void add_edge(int x, int y) {//x, y clash
to[ecnt] = y ^ ; next[ecnt] = head[x]; head[x] = ecnt++;
to[ecnt] = x ^ ; next[ecnt] = head[y]; head[y] = ecnt++;
} void dfs(int u) {
lowlink[u] = pre[u] = ++dfs_clock;
stk[++top] = u;
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(!pre[v]) {
dfs(v);
if(lowlink[v] < lowlink[u]) lowlink[u] = lowlink[v];
} else if(!sccno[v]) {
if(pre[v] < lowlink[u]) lowlink[u] = pre[v];
}
}
if(lowlink[u] == pre[u]) {
sccnox[++scc_cnt] = u;
while(true) {
int x = stk[top--];
sccno[x] = scc_cnt;
if(x == u) break;
}
}
} bool solve() {
for(int i = ; i < n; ++i) if(!pre[i]) dfs(i);
for(int i = ; i < n; i += )
if(sccno[i] == sccno[i ^ ]) return false;
return true;
} void build_select() {
T.init(scc_cnt);
for(int u = ; u < n; ++u) {
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(sccno[u] == sccno[v]) continue;
T.add_edge(sccno[u], sccno[v]);
}
}
T.build();
memset(select, -, sizeof(select));
for(int i = T.n - ; i >= ; --i) {
int &x = T.order[i];
if(select[x] == -) {
select[x] = ;
select[sccno[sccnox[x] ^ ]] = ;
}
}
}
} G; int n, m; int main() {
scanf("%d%d", &n, &m);
G.init(n << );
for(int i = ; i < m; ++i) {
int u, v, p;
scanf("%d%d%d", &u, &v, &p);
--u, --v;
if(p) {
G.add_edge( * u, * v ^ );
G.add_edge( * u ^ , * v);
} else {
G.add_edge( * u, * v);
G.add_edge( * u ^ , * v ^ );
}
}
if(!G.solve()) {
printf("Impossible");
} else {
G.build_select();
int cnt = ;
for(int i = ; i < n; ++i) cnt += G.select[G.sccno[ * i]];
printf("%d\n", cnt);
for(int i = ; i < n; ++i)
if(G.select[G.sccno[ * i]]) printf("%d ", i + );
}
}

codeforces 228E The Road to Berland is Paved With Good Intentions(2-SAT)的更多相关文章

  1. Educational Codeforces Round 48 (Rated for Div. 2) B 1016B Segment Occurrences (前缀和)

    B. Segment Occurrences time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  2. codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(启发式合并)

    codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...

  3. Educational Codeforces Round 47 (Rated for Div. 2) :C. Annoying Present(等差求和)

    题目链接:http://codeforces.com/contest/1009/problem/C 解题心得: 题意就是一个初始全为0长度为n的数列,m此操作,每次给你两个数x.d,你需要在数列中选一 ...

  4. Codeforces Round #286 (Div. 1) B. Mr. Kitayuta&#39;s Technology (强连通分量)

    题目地址:http://codeforces.com/contest/506/problem/B 先用强连通判环.然后转化成无向图,找无向图连通块.若一个有n个点的块内有强连通环,那么须要n条边.即正 ...

  5. Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords(贪心/数学)

    题目链接:https://codeforces.com/contest/1366/problem/A 题意 有两个数 $a$ 和 $b$,每次可以选择从一个数中取 $2$,另一个数中取 $1$,问最多 ...

  6. Codeforces Round #259 (Div. 2) C - Little Pony and Expected Maximum (数学期望)

    题目链接 题意 : 一个m面的骰子,掷n次,问得到最大值的期望. 思路 : 数学期望,离散时的公式是E(X) = X1*p(X1) + X2*p(X2) + …… + Xn*p(Xn) p(xi)的是 ...

  7. codeforces 700C Break Up 暴力枚举边+边双缩点(有重边)

    题意:n个点,m条无向边,每个边有权值,给你 s 和 t,问你至多删除两条边,让s,t不连通,问方案的权值和最小为多少,并且输出删的边 分析:n<=1000,m是30000  s,t有4种情况( ...

  8. Codeforces Round #496 (Div. 3 ) E1. Median on Segments (Permutations Edition)(中位数计数)

    E1. Median on Segments (Permutations Edition) time limit per test 3 seconds memory limit per test 25 ...

  9. Codeforces Round #294 (Div. 2) A and B and Lecture Rooms(LCA 倍增)

    A and B and Lecture Rooms time limit per test 2 seconds memory limit per test 256 megabytes input st ...

随机推荐

  1. Javascript混淆与解混淆的那些事儿

    像软件加密与解密一样,javascript的混淆与解混淆同属于同一个范畴.道高一尺,魔高一丈.没有永恒的黑,也没有永恒的白.一切都是资本市场驱动行为,现在都流行你能为人解决什么问题,这个概念.那么市场 ...

  2. vue-router笔记

    1.vue-router 安装 在安装webpack模块时就安装了 eg: vue  init  webpack  demo    (安装webpack模块并取名为demo) 在安装模块时没有安装的话 ...

  3. Magazine Ad CodeForces - 803D(二分 + 贪心,第一次写博客)

    Magazine Ad The main city magazine offers its readers an opportunity to publish their ads. The forma ...

  4. sql 优化的几种方法

    .对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. .应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而 ...

  5. TinyMCE插件:Filemanager [4.x-6.x] 图片自动添加水印

    上传图片程序(filemanager/upload.php) 在if (!empty($_FILES) && $upload_files)有一个move_uploaded_file() ...

  6. checked和unchecked转换

    static unsafe void Main(string[] args) { unchecked //checked 运行时候引发异常 { int n = int.MaxValue; n++; C ...

  7. ubuntu下的python网页解析库的安装——lxml, Beautiful Soup, pyquery, tesserocr

    lxml 的安装(xpath) pip3 install lxml 可能会缺少以下依赖: sudo apt-get install -y python3-dev build-e ssential li ...

  8. gem install ruby-odbc失败

    解决: brew install unixodbc gem install ruby-odbc -v '0.99998'

  9. centos7 杂记

    yum 源 https://www.cnblogs.com/renpingsheng/p/7845096.html 安装nginx php mysql https://www.cnblogs.com/ ...

  10. WPF Prism MVVM 中 弹出新窗体. 放入用户控件

    原文:WPF Prism MVVM 中 弹出新窗体. 放入用户控件 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_37214567/artic ...