Cod sursa(job #2630215)

Utilizator JafarakKarina Jafara Jafarak Data 24 iunie 2020 18:20:13
Problema Andrei Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

ifstream f("andrei.in");
ofstream g("andrei.out");

const int N=100005;

int n,m,who[N];

vector < pair < int, int > > v[N];

void find_group ( int a, int b, int c )
{
    if(who[a]==1)
    {
        if(!c)
            who[b]=2;
        else
            who[b]=1;
    }
    else
        if(who[a]==2)
        {
            if(c==1)
                who[b]=1;
            else
                who[b]=2;
        }
}

void go_group ( int a, int b, int c )
{
    if(who[a] && !who[b])
    {
        find_group(a,b,c);
        return;
    }
    if(who[b] && !who[a])
    {
        find_group(b,a,c);
        return;
    }
    if(!who[a] && !who[b])
    {
        who[a]=1;
        go_group(a,b,c);
    }
}

void Read ()
{
    int i,a,b,c;
    f>>n>>m;
    for(i=1;i<=m;i++)
    {
        f>>a>>b>>c;
        v[a].push_back(make_pair(b,c));
        go_group(a,b,c);
    }
}

void out ()
{
    int i;
    for(i=1;i<=n;i++)
        g<<who[i]-1<<" ";
}

int main()
{
    Read();
    out();
    return 0;
}