Pagini recente » Cod sursa (job #1017692) | Cod sursa (job #3178098) | Cod sursa (job #2055107) | Cod sursa (job #1579117) | Cod sursa (job #879451)
Cod sursa(job #879451)
#include <fstream>
#include <vector>
#include <deque>
#define nmax 200100
#define non(x) ((x)<=N?(x+N):(x-N))
using namespace std;
vector <int> G[nmax],Gt[nmax];
deque <int> D;
int N,Answer;
bool used[nmax],mark[nmax];
void DFS(int Nod) {
if(mark[Nod]) {
Answer=0;
return;
}
used[Nod]=0;
mark[non(Nod)]=1;
for(int i=0;i<Gt[Nod].size();i++)
if(used[Gt[Nod][i]])
DFS(Gt[Nod][i]);
}
void TopologicalSort(int Nod) {
used[Nod]=1;
for(int i=0;i<G[Nod].size();i++)
if(!used[G[Nod][i]])
TopologicalSort(G[Nod][i]);
D.push_front(Nod);
}
void solve() {
int Nod;
for(Nod=1;Nod<=2*N;Nod++)
if(!used[Nod])
TopologicalSort(Nod);
Answer=1;
for(Nod=0;Answer && Nod<2*N;Nod++)
if(used[D[Nod]]&&used[non(D[Nod])])
DFS(D[Nod]);
}
void read() {
int x,y,M;
ifstream in("2sat.in");
in>>N>>M;
while(M--) {
in>>x>>y;
if(x<0) x=N-x;
if(y<0) y=N-y;
G[non(x)].push_back(y);
G[non(y)].push_back(x);
Gt[x].push_back(non(y));
Gt[y].push_back(non(x));
}
in.close();
}
void write() {
ofstream out("2sat.out");
if(!Answer)
out<<"-1";
else
for(int i=1;i<=N;i++)
out<<mark[i]<<' ';
out<<'\n';
out.close();
}
int main() {
read();
solve();
write();
return 0;
}