[HDU1814] Peaceful Commission (2-sat + violence staining)

Portal

\ (2-sat \) template title, first obtained the title of binary relations: for contradictory \ (x_i, x_j \) , up to select one, then even the edge \ (x_i \ rightarrow x_j ', x_j \ rightarrow x_i '\) .
Here is a clear relationship between this relationship, we can not even use only when you can not even clear relationship side. Assuming no election \ (x_i \) , we do not know \ (x_j \) option is not selected, the optional is optional, so do the \ (x_i '\) departure even edge.

Since then subject the lexicographically smallest claim, said to \ (Trajan \) condensing point can be problematic, as do, because the data range is relatively small, so the direct \ (O (nm) \) violent QAQ staining on the line
code is as follows:

/*
 * Author:  heyuhhh
 * Created Time:  2019/11/29 14:52:58
 */
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <cstring>
#include <iomanip>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << '\n'; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
  #define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 8005;

int n, m;
vector <int> g[N << 1];

int get(int x) {
    if(x % 2) return x + 1;
    return x - 1;
}

int cnt;
int col[N << 1], tmp[N];

bool paint(int x) {
    if(col[x]) {
        if(col[x] % 2 == 0) return false;
        return true;   
    }
    tmp[++cnt] = x;
    col[x] = 1; col[get(x)] = 2;
    for(auto y : g[x]) {
        if(!paint(y)) return false;   
    }
    return true;
}

bool work() {
    memset(col, 0, sizeof(col));
    for(int i = 1; i <= 2 * n; i++) {
        if(col[i]) continue;
        cnt = 0;
        if(!paint(i)) {
            for(int j = 1; j <= cnt; j++) col[tmp[j]] = col[get(tmp[j])] = 0;
            cnt = 0;
            if(!paint(get(i))) return false; 
        }
    }
    return true;
}

void run(){
    for(int i = 1; i <= 2 * n; i++) g[i].clear();
    for(int i = 1; i <= m; i++) {
        int u, v; cin >> u >> v;
        g[u].push_back(get(v));
        g[v].push_back(get(u));
        g[get(u)].push_back(v);
        g[get(v)].push_back(u);
    }
    if(work()) {
        for(int i = 1; i <= 2 * n; i++) 
            if(col[i] == 1) cout << i << '\n';
    } else cout << "NIE" << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    while(cin >> n >> m) run();
    return 0;
}

Guess you like

Origin www.cnblogs.com/heyuhhh/p/11965770.html