Cod sursa(job #3279771)

Utilizator Dia3141Costea Diana Stefania Dia3141 Data 24 februarie 2025 13:53:33
Problema Andrei Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <fstream>
#include <stack>
#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,minlv[dim],niv[dim],sol[dim];
vector<int>v[dim];
stack<int>s;
bool viz[dim];
void dfs(int nod,int t){
    s.push(nod);
    viz[nod]=1;
    niv[nod]=minlv[nod]=niv[t]+1;
    for(auto i:v[nod])
        if(!viz[i]){
            dfs(i,nod);
            minlv[nod]=min(minlv[nod],minlv[i]);
        }else
            minlv[nod]=min(minlv[nod],niv[i]);
    if(minlv[nod]==niv[nod]){
        while(!s.empty()&&s.top()!=nod){
            sol[s.top()]=nr;
            s.pop();
        }
        s.pop();
        sol[nod]=nr;
        nr++;
    }
}
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);
        }else if(tip==1){
            v[n+y].push_back(x);
            v[n+x].push_back(y);
        }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);
        }
    }
    for(int i=1;i<=2*n;i++)
        if(!viz[i])
            dfs(i,0);
    for(int i=1;i<=n;i++)
        if(sol[i]<sol[i+n])
            cout<<0<<" ";
        else
            cout<<1<<" ";
    return 0;
}