Cod sursa(job #1044809)

Utilizator scipianusFMI Ciprian Olariu scipianus Data 30 noiembrie 2013 14:28:51
Problema Andrei Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.95 kb
#include <fstream>
#include <vector>
using namespace std;
int n, m, nrsol;
vector <int> G[200100], GT[200100];
bool viz[200100], sol[200100];
int postordine[200100], nrord;
 
inline int Not(int x)
{
    if(x <= n)
        return x + n;
    return x - n;
}
 
inline void Add(int x, int y) // x sau y
{
	G[Not(x)].push_back(y);
	G[Not(y)].push_back(x);
	GT[x].push_back(Not(y));
	GT[y].push_back(Not(x));
}

inline void Citire()
{
    int i, x, y, tip;
    ifstream fin("andrei.in");
    fin >> n >> m;
    for(i = 1; i <= m; ++i)
    {
        fin >> x >> y >> tip;
        if(tip == 0) // nu pot fi ambele in stanga
			Add(Not(x), Not(y));
		if(tip == 1) // nu pot fi ambele in dreapta
			Add(x, y); 
		if(tip == 2) // ambele in stanga sau ambele in dreapta
		{
			G[x].push_back(y);
			G[y].push_back(x);
			G[Not(x)].push_back(Not(y));
			G[Not(y)].push_back(Not(x));
			GT[y].push_back(x);
			GT[x].push_back(y);
			GT[Not(y)].push_back(Not(x));
			GT[Not(x)].push_back(Not(y));
		}
    }
    fin.close();
}
 
inline void DFS(int x)
{
    viz[x] = true;
    vector <int>::iterator it;
    for(it = G[x].begin(); it != G[x].end(); ++it)
        if(!viz[*it])
            DFS(*it);
    postordine[++nrord] = x;
}
 
inline void DFST(int x)
{
    viz[x] = false;
    sol[Not(x)] = true;
    vector <int>::iterator it;
    for(it = GT[x].begin(); it != GT[x].end(); ++it)
        if(viz[*it])
            DFST(*it);
}
 
inline void Rezolvare()
{
    int i;
    for(i = 1; i <= 2 * n; ++i)
        if(!viz[i])
            DFS(i);
    for(i = 2 * n; i > 0; --i)
        if(viz[postordine[i]] && viz[Not(postordine[i])])
            DFST(postordine[i]);
}
 
inline void Afisare()
{
	int i;
    ofstream fout("andrei.out");
    for(i = 1; i <= n; ++i)
        fout << (1 - sol[i]) << ' ';
	fout << "\n";
    fout.close();
}
 
int main()
{
    Citire();
    Rezolvare();
    Afisare();
    return 0;
}