Cod sursa(job #1119915)

Utilizator pulseOvidiu Giorgi pulse Data 24 februarie 2014 20:43:05
Problema Andrei Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.88 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream fin ("andrei.in");
ofstream fout ("andrei.out");

#define NMAX 200010

bool used[NMAX], Ans[NMAX], isSol;
int n, m, np, p[NMAX];
vector <int> G[NMAX], GT[NMAX];

inline int Neg (const int x)
{
    if (x <= n)
        return n + x;
    return x - n;
}

inline void Add (const int x, const int y)
{
    G[Neg(x)].push_back(y);
    G[Neg(y)].push_back(x);

    GT[y].push_back(Neg(x));
    GT[x].push_back(Neg(y));
}

void Read_Data ()
{
    fin >> n >> m;
    for (int i = 1, x, y, color; i <= m; ++i)
    {
        fin >> x >> y >> color;
        switch(color)
        {
            case 0:
                Add(x, y);
                break;
            case 1:
                Add (Neg(x), Neg(y));
                break;
            case 2:
                Add(Neg(x), y);
                Add(x, Neg(y));
                break;
        }
    }
}

inline void DFS (const int node)
{
    used[node] = true;
    for (vector <int> :: iterator it = G[node].begin(); it != G[node].end(); ++it)
        if (!used[*it])
            DFS(*it);
    p[++np] = node;
}

inline void DFST(const int node)
{
    if (Ans[node] == true) { isSol = true; return; }
    Ans[Neg(node)] = true;
    used[node] = false;
    for (vector <int> :: iterator it = GT[node].begin(); it != GT[node].end(); ++it)
        if (used[*it])
            DFST(*it);
}

void Solve ()
{
    isSol = true;
    int N = n + n;
    for (int i = 1; i <= N; ++i)
        if (!used[i])
            DFS(i);
    for (int i = N; i; --i)
        if (used[p[i]] && used[Neg(p[i])])
            DFST(p[i]);
}

inline void Write_Data ()
{
    for (int i=1; i <= n; ++i)
        fout << Ans[i]<<" ";
}

int main()
{
    Read_Data ();
    Solve ();
    Write_Data ();
    fin.close (); fout.close ();
    return 0;
}