CodeForces 228E The Road to Berland is Paved With Good Intentions (2-Sat)





题目地址: 

http://codeforces.com/problemset/problem/228/E 


E. The Road to Berland is Paved With Good Intentions
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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

Sample test(s)
input
4 4
1 2 1
2 4 0
4 3 1
3 2 0
output
4
3 2 1 3
input
3 3
1 2 0
2 3 0
3 1 0
output
Impossible




题意:

一幅图,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;
}


你可能感兴趣的:(CodeForces 228E The Road to Berland is Paved With Good Intentions (2-Sat))