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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

keras系列︱图像多分类训练与利用bottleneck features进行微调(三)

不得不说,这深度学习框架更新太快了尤其到了Keras2.0版本,快到Keras中文版好多都是错的,快到官方文档也有旧的没更新,前路坑太多。 到发文为止,已经有theano/tensorflow...
  • sinat_26917383
  • sinat_26917383
  • 2017年06月05日 22:41
  • 10758

ElasticSearch 5.3——单台服务器多节点

1、首先将elasticsearch文件夹再复制一份 2、主要工作就是修改elasticsearch.yml配置文件。 实例二:config目录下的elasticsearch.yml内容 将n...
  • zwyjg
  • zwyjg
  • 2017年04月25日 15:07
  • 2541

【WebService学习过程记录(一)】照猫画虎---运行示例

学习网址: JAVA6开发WebService (一)  这篇博客枯燥言语少,直接一个可运行的代码,先动手一试,我习惯的学习方式是“知其然再知其所以然”,先会用再追其原理 模仿实例,遇见问题记录 ...
  • xuexin8625
  • xuexin8625
  • 2013年01月07日 13:53
  • 5838

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...
  • xiamiwage
  • xiamiwage
  • 2012年10月05日 00:48
  • 830

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
  • 1904

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...
  • hellominix
  • hellominix
  • 2013年04月03日 08:57
  • 268

codeforces 721E. Road to Home

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

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 ...
  • zsc2014030403015
  • zsc2014030403015
  • 2016年05月16日 22:21
  • 269

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

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

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
  • 274
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:CodeForces 228E The Road to Berland is Paved With Good Intentions (2-Sat)
举报原因:
原因补充:

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