codeforces 228E The Road to Berland is Paved With Good Intentions(2-SAT)

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

  1 #include <iostream>

  2 #include <cstring>

  3 #include <cstdio>

  4 #include <vector>

  5 using namespace std;

  6 

  7 const int MAXN = 110;

  8 const int MAXV = 210;

  9 const int MAXE = MAXV * MAXV;

 10 

 11 struct Topological {

 12     int stk[MAXV], top;

 13     int n, ecnt, cnt;

 14     int head[MAXV], order[MAXV], indeg[MAXV];

 15     int to[MAXE], next[MAXE];

 16 

 17     void init(int nn) {

 18         memset(head, -1, sizeof(head));

 19         memset(indeg, 0, sizeof(indeg));

 20         n = nn; ecnt = 0;

 21     }

 22 

 23     void add_edge(int u, int v) {

 24         to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;

 25         ++indeg[v];

 26     }

 27 

 28     void build() {

 29         top = cnt = 0;

 30         for(int i = 1; i <= n; ++i)

 31             if(indeg[i] == 0) stk[++top] = i;

 32         while(top) {

 33             int u = stk[top--]; order[cnt++] = u;

 34             for(int p = head[u]; ~p; p = next[p]) {

 35                 int &v = to[p];

 36                 if(--indeg[v] == 0) stk[++top] = v;

 37             }

 38         }

 39     }

 40 } T;

 41 

 42 struct TwoSAT {//从0开始编号

 43     int stk[MAXV], top;

 44     int n, ecnt, dfs_clock, scc_cnt;

 45     int head[MAXV], sccno[MAXV], pre[MAXV], lowlink[MAXV];

 46     int to[MAXE], next[MAXE];

 47     int select[MAXV], sccnox[MAXV];

 48 

 49     void init(int nn) {

 50         memset(head, -1, sizeof(head));

 51         memset(pre, 0, sizeof(pre));

 52         memset(sccno, 0, sizeof(sccno));

 53         n = nn, ecnt = dfs_clock = scc_cnt = 0;

 54     }

 55 

 56     void add_edge(int x, int y) {//x, y clash

 57         to[ecnt] = y ^ 1; next[ecnt] = head[x]; head[x] = ecnt++;

 58         to[ecnt] = x ^ 1; next[ecnt] = head[y]; head[y] = ecnt++;

 59     }

 60 

 61     void dfs(int u) {

 62         lowlink[u] = pre[u] = ++dfs_clock;

 63         stk[++top] = u;

 64         for(int p = head[u]; ~p; p = next[p]) {

 65             int &v = to[p];

 66             if(!pre[v]) {

 67                 dfs(v);

 68                 if(lowlink[v] < lowlink[u]) lowlink[u] = lowlink[v];

 69             } else if(!sccno[v]) {

 70                 if(pre[v] < lowlink[u]) lowlink[u] = pre[v];

 71             }

 72         }

 73         if(lowlink[u] == pre[u]) {

 74             sccnox[++scc_cnt] = u;

 75             while(true) {

 76                 int x = stk[top--];

 77                 sccno[x] = scc_cnt;

 78                 if(x == u) break;

 79             }

 80         }

 81     }

 82 

 83     bool solve() {

 84         for(int i = 0; i < n; ++i) if(!pre[i]) dfs(i);

 85         for(int i = 0; i < n; i += 2)

 86             if(sccno[i] == sccno[i ^ 1]) return false;

 87         return true;

 88     }

 89 

 90     void build_select() {

 91         T.init(scc_cnt);

 92         for(int u = 0; u < n; ++u) {

 93             for(int p = head[u]; ~p; p = next[p]) {

 94                 int &v = to[p];

 95                 if(sccno[u] == sccno[v]) continue;

 96                 T.add_edge(sccno[u], sccno[v]);

 97             }

 98         }

 99         T.build();

100         memset(select, -1, sizeof(select));

101         for(int i = T.n - 1; i >= 0; --i) {

102             int &x = T.order[i];

103             if(select[x] == -1) {

104                 select[x] = 1;

105                 select[sccno[sccnox[x] ^ 1]] = 0;

106             }

107         }

108     }

109 } G;

110 

111 int n, m;

112 

113 int main() {

114     scanf("%d%d", &n, &m);

115     G.init(n << 1);

116     for(int i = 0; i < m; ++i) {

117         int u, v, p;

118         scanf("%d%d%d", &u, &v, &p);

119         --u, --v;

120         if(p) {

121             G.add_edge(2 * u, 2 * v ^ 1);

122             G.add_edge(2 * u ^ 1, 2 * v);

123         } else {

124             G.add_edge(2 * u, 2 * v);

125             G.add_edge(2 * u ^ 1, 2 * v ^ 1);

126         }

127     }

128     if(!G.solve()) {

129         printf("Impossible");

130     } else {

131         G.build_select();

132         int cnt = 0;

133         for(int i = 0; i < n; ++i) cnt += G.select[G.sccno[2 * i]];

134         printf("%d\n", cnt);

135         for(int i = 0; i < n; ++i)

136             if(G.select[G.sccno[2 * i]]) printf("%d ", i + 1);

137     }

138 }
View Code

 

你可能感兴趣的:(codeforces)