Pagini recente » Cod sursa (job #2623154) | Cod sursa (job #348934) | Cod sursa (job #258921) | Cod sursa (job #1879330) | Cod sursa (job #1265327)
#include <iostream>
#include <vector>
#include <queue>
#define MAXM 250000
using namespace std;
vector<int> G[2 * MAXM + 5];
vector<int> G2[2 * MAXM + 5];
bool viz[2 * MAXM + 5];
int timp[2 * MAXM + 5];
int vtc[2 * MAXM + 5];
int deg_in[2 * MAXM + 5];
int deg_out[2 * MAXM + 5];
vector<int> value(2 * MAXM + 5, -1);
vector<int> p;
vector<int> sol[2 * MAXM + 5];
int t, N, M, nr;
void DFS(int x) {
viz[x] = 1;
for(int i = 0; i < G[x].size(); ++i) {
if(!viz[G[x][i]]) {
DFS(G[x][i]);
}
}
timp[++t] = x;
}
void DFS_inv(int x) {
viz[x] = 1;
p.push_back(x);
for(int i = 0; i < G2[x].size(); ++i) {
if(!viz[G2[x][i]]) {
DFS_inv(G2[x][i]);
}
}
}
int main()
{
int s1, s2, st1, st2;
cin >> N >> M;
for(int i = 1; i <= N; ++i) {
cin >> s1 >> st1 >> s2 >> st2;
if(st1) {
if(st2) {
G[s1].push_back(s2 + M);
G[s2].push_back(s1 + M);
G2[s2 + M].push_back(s1);
G2[s1 + M].push_back(s2);
}
else {
G[s1].push_back(s2);
G[s2 + M].push_back(s1 + M);
G[s2].push_back(s1);
G[s1 + M].push_back(s2 + M);
}
}
else {
if(st2) {
G[s1 + M].push_back(s2 + M);
G[s2].push_back(s1);
G[s2 + M].push_back(s1 + M);
G[s1].push_back(s2);
}
else {
G[s1 + M].push_back(s2);
G[s2 + M].push_back(s1);
G[s2].push_back(s1 + M);
G[s1].push_back(s2 + M);
}
}
}
for(int i = 1; i <= 2 * M; ++i)
if(!viz[i]) {
DFS(i);
}
for(int i = 1; i <= 2 * M; ++i)
viz[i] = 0;
for(int i = t; i >= 1; --i)
if(!viz[timp[i]]) {
DFS_inv(timp[i]);
int sizep = p.size();
if(sizep)
++nr;
for(int i = 0; i < sizep; ++i) {
vtc[p[i]] = nr;
sol[nr].push_back(p[i]);
cout << p[i] << ' ';
}
cout << endl;
p.clear();
}
for(int i = 1; i <= M; ++i) {
if(vtc[i] == vtc[i + M]) {
cout << "IMPOSSIBLE";
return 0;
}
}
for(int x = 1; x <= M * 2; ++x) {
vector <int>::iterator it;
for(it = G[x].begin(); it != G[x].end(); ++ it)
if(vtc[x] != vtc[*it])
++deg_in[vtc[*it]];
}
queue <int> Q;
for (int idx = 1; idx <= nr; ++idx) {
if (deg_in[idx] == 0)
Q.push(idx);
}
while (!Q.empty()) {
int idx = Q.front();
Q.pop();
vector <int>::iterator it, i, j;
for (it = sol[idx].begin(); it != sol[idx].end(); ++it) {
int x = (*it > M) ? *it - M : *it;
if (value[x] == -1)
value[x] = (*it <= M) ? 0 : 1;
}
for (i = sol[idx].begin(); i != sol[idx].end(); ++i) {
for (j = G[*i].begin(); j != G[*i].end(); ++j)
if (vtc[*i] != vtc[*j]) {
if ((--deg_in[vtc[*j]]) == 0)
Q.push(vtc[*j]);
}
}
}
for (int i = 1; i <= M; ++ i)
cout << value[i] << " ";
return 0;
}