Cod sursa(job #1290642)

Utilizator cojocarugabiReality cojocarugabi Data 11 decembrie 2014 17:11:22
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
# include <bits/stdc++.h>
using namespace std;
const int nmax = 1e5 + 5;
int s[nmax],v[nmax];
int poz(int x)
{
    int r=x,aux;
    while (s[r] != r) r=s[r];
    while (s[x] != x) aux=s[x],s[x]=r,x=aux;
    return x;
}
void comb(int x,int y)
{
    if (v[x] > v[y]) s[y]=x;else s[x]=y;
    if (v[x] == v[y]) ++v[y];
}
int main(void)
{
    int n,m,t,x,y;
    ifstream fi("disjoint.in");
    ofstream fo("disjoint.out");
    fi>>n>>m;
    for (int i=1;i<=n;++i) s[i]=i,v[i]=1;
    while (m --)
    {
        fi>>t>>x>>y;
        if (t == 1) comb(poz(x),poz(y));else fo << (poz(x) == poz(y) ? "DA\n":"NU\n");
    }
    return 0;
}