CF 141 div2 D(2-SAT)

來源:互聯網
上載者:User
E. The Road to Berland is Paved With Good Intentionstime limit per test

2 seconds

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 41 2 12 4 04 3 13 2 0
output
43 2 1 3
input
3 31 2 02 3 03 1 0
output
Impossible
首先,每個點最多隻能選擇一次,所以在n天內完成的要求就沒什麼用了,而且,城市選擇的順序對最後結果沒有影響。然後對一條邊,如果它為1,那麼和他相鄰的城市,要麼都選擇,要麼都不選擇;如果為0,則兩個城市只能且必須選擇一個。這就是2SAT的模型了。直接套用模板,模板中的加邊是滿足條件A或B,這裡要求A且B,重新寫一個加邊函數就好了。
#include<cstdio>#include<cstring>#include<queue>#include<vector>#include<algorithm>#include<iostream>using namespace std;const int maxn = 100;struct TwoSAT {  int n;  vector<int> G[maxn*2];  bool mark[maxn*2];  int S[maxn*2], c;  bool dfs(int x) {    if (mark[x^1]) return false;    if (mark[x]) return true;    mark[x] = true;    S[c++] = x;    for (int i = 0; i < G[x].size(); i++)      if (!dfs(G[x][i])) return false;    return true;  }  void init(int n) {    this->n = n;    for (int i = 0; i < n*2; i++) G[i].clear();    memset(mark, 0, sizeof(mark));  }  // x = xval or y = yval  void add_clause(int x, int xval, int y, int yval) {    x = x * 2 + xval;    y = y * 2 + yval;    G[x^1].push_back(y);    G[y^1].push_back(x);  }  void new_add(int x,int xval,int y,int yval){    x = x*2 + xval;    y = y*2 + yval;    G[x].push_back(y);    G[y].push_back(x);  }  bool solve() {    for(int i = 0; i < n*2; i += 2)      if(!mark[i] && !mark[i+1]) {        c = 0;        if(!dfs(i)) {          while(c > 0) mark[S[--c]] = false;          if(!dfs(i+1)) return false;        }      }    return true;  }};TwoSAT solver;int main(){    int n,m;    while(cin >> n >> m){        solver.init(n);        for(int i = 0;i < m;i++){            int from,to,val;            cin >> from >> to >> val;            from--;to--;            if(val == 1){                solver.new_add(from,1,to,1);                solver.new_add(from,0,to,0);            }            else{                solver.new_add(from,1,to,0);                solver.new_add(from,0,to,1);            }        }        if(!solver.solve()){            cout << "Impossible\n";            continue;        }        int ans = 0;        for(int i = 1;i < 2*n;i+=2)            if(solver.mark[i] == true) ans++;        cout << ans << endl;        for(int i = 1;i < 2*n;i+=2)            if(solver.mark[i] == true) cout << i/2+1 << ' ';        cout << endl;    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.