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

原创 2015年07月09日 00:25:37




题目地址: 

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 2Sat求解

//CodeForces 228E The Road to Berland is Paved With Good Intentions 2Sat求解 /* 题目地址: http://codeforce...

Codeforces Beta Round div.2 #228 A B C D E

Codeforces Beta Round #228 A B C D E
  • hcbbt
  • hcbbt
  • 2014年02月04日 23:10
  • 1867

The Road to Performance Is Littered with Dirty Code Bombs

More often than not, performance tuning a system requires you to alter code. When we need to alter c...

codeforces 721E. Road to Home

题目链接:http://codeforces.com/contest/721/problem/E 题目大意:在一条坐标范围为[0,L]的路上有n盏路灯,每盏路灯有一个照亮范围[li,ri]且没有重叠...
  • aufeas
  • aufeas
  • 2016年11月04日 21:11
  • 304

Codeforces Round #352 (Div. 2)-B. Different is Good(模拟)

B. Different is Good time limit per test 2 seconds memory limit per test 256 megabytes ...

Codeforces Round #287 (Div. 2) E. Breaking Good(最短路、dp)

题意: N≤105个城市,M≤105条边,保证无向图连通且无自环和重边N\le10^5个城市, M\le10^5条边, 保证无向图连通且无自环和重边 边分2种,0代表毁坏的,1代表正常的边分...
  • lwt36
  • lwt36
  • 2016年02月13日 17:24
  • 255

Windows下升级SVN后,Ubuntu下svn命令报错—svn: E155021: This client is too old to work with the working copy at

Windows下升级SVN后,在Ubuntu下执行svn命令报错,如下:      svn: E155021: This client is too old to work with the w...

SVN 报错:svn: E155021: This client is too old to work with the working copy at..... (format '31').

产生该错误的原因:电脑安装的SVN客户端版本如下图: eclipse的svn插件是: 因为运行的下面操作,所以产生此错误! 对本地Checkout的版本号运行Upgrade: ...

Codeforces Round #352 (Div. 2) B. Different is Good __ substrings water problem

the smallest Substring of the given string can be lowercase English letters, so if it is possible to...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:CodeForces 228E The Road to Berland is Paved With Good Intentions (2-Sat)
举报原因:
原因补充:

(最多只允许输入30个字)