Connected Component, 2-SAT
Jump to navigation
Jump to search
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
typedef long long LL;
typedef pair<int, int> P;
typedef double ld;
const double EPS = 1e-9;
const double PI = acos(-1.0);
const int N = 1e5 + 10;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
/********** head **********/
int n, m;
vector<int> G[N], rG[N], vs;
int used[N], cmp[N];
void add_edge(int from, int to) {
G[from].push_back(to);
rG[to].push_back(from);
}
void dfs(int v) {
used[v] = true;
for (int u: G[v]) {
if (!used[u])
dfs(u);
}
vs.push_back(v);
}
void rdfs(int v, int k) {
used[v] = true;
cmp[v] = k;
for (int u: rG[v])
if (!used[u])
rdfs(u, k);
}
int scc() {
memset(used, 0, sizeof(used));
vs.clear();
for (int v = 0; v < n; ++v)
if (!used[v]) dfs(v);
memset(used, 0, sizeof(used));
int k = 0;
for (int i = (int) vs.size() - 1; i >= 0; --i)
if (!used[vs[i]]) rdfs(vs[i], k++);
return k;
}
int main() {
cin >> n >> m;
n *= 2;
for (int i = 0; i < m; ++i) {
int a, b; cin >> a >> b;
add_edge(a - 1, (b - 1) ^ 1);
add_edge(b - 1, (a - 1) ^ 1);
}
scc();
for (int i = 0; i < n; i += 2) {
if (cmp[i] == cmp[i + 1]) {
puts("NIE");
return 0;
}
}
for (int i = 0; i < n; i += 2) {
if (cmp[i] > cmp[i + 1]) printf("%d\n", i + 1);
else printf("%d\n", i + 2);
}
}