Cod sursa(job #3279753)

Utilizator Dia3141Costea Diana Stefania Dia3141 Data 24 februarie 2025 13:23:10
Problema Andrei Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.54 kb
#include <fstream>
#include <cstring>
#include <vector>
#define dim (int)(2e5+1)
using namespace std;
ifstream cin("andrei.in");
ofstream cout("andrei.out");
int n,m,x,y,k,tip,nr,tops[dim],sol[dim];
vector<int>v[dim],vt[dim];
bool viz[dim];
void dfs(int nod){
    viz[nod]=1;
    for(auto i:v[nod])
        if(!viz[i])
            dfs(i);
    tops[++k]=nod;
}
void dfst(int nod){
    viz[nod]=1;
    sol[nod]=nr;
    for(auto i:vt[nod])
        if(!viz[i])
            dfst(i);
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        cin>>x>>y>>tip;
        if(tip==0){
            v[x].push_back(n+y);
            v[y].push_back(n+x);
            vt[n+y].push_back(x);
            vt[n+x].push_back(y);
        }else if(tip==1){
            v[n+y].push_back(x);
            v[n+x].push_back(y);
            vt[x].push_back(n+y);
            vt[y].push_back(n+x);
        }else{
            v[x].push_back(y);
            v[y].push_back(x);
            v[n+y].push_back(n+x);
            v[n+x].push_back(n+y);
            vt[y].push_back(x);
            vt[x].push_back(y);
            vt[n+y].push_back(n+x);
            vt[n+x].push_back(n+y);
        }
    }
    for(int i=1;i<=2*n;i++)
        if(!viz[i])
            dfs(i);
    memset(viz,0,sizeof(viz));
    for(int i=n*2;i>0;i--)
        if(!viz[tops[i]]){
            dfst(i);
            nr++;
        }
    for(int i=1;i<=n;i++)
        if(sol[i]<sol[i+n])
            cout<<0<<" ";
        else
            cout<<1<<" ";
    return 0;
}