Cod sursa(job #2112260)

Utilizator omegasFilip Ion omegas Data 23 ianuarie 2018 11:46:32
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <iostream>
#include <fstream>

using namespace std;

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

struct nod{
    int val;
    nod* next;
};

nod* v[100001];
int u1[100001];
int u2[100001];

int k(int a){
    nod *p;
    for(p=v[a];p->next!=NULL;p=p->next);
    return p->val;
}

void update(int a){
    nod *p,*q;
    for(p=v[a];p->next!=NULL;p=p->next);

    q = p;

    for(p=v[a];p->next!=NULL;p=p->next)
    v[p->val] = q;

    v[a] = q;
}


void connect(int a,int b){
    nod *p,*q,*t;
    int l2 = 0;
    int l1 = 0;

    for(p=v[a];p->next!=NULL;p=p->next)
        ++l1;
    for(q=v[b];q->next!=NULL;q=q->next)
        ++l2;

    if(l1 > l2)
        t = q->next = p;
    else
        t = p->next = q;

    update(a);
    update(b);

}

int main()
{
    int i,n,m;
    int x,y,z;

    fin >> n >> m;
    for(i=1;i<=n;++i){
        v[i] = new nod;
        v[i]->val = i;
        v[i]->next = NULL;
    }

    for(i=1;i<=m;++i){
        fin >> x >> y >> z;
        if(x == 1)
            connect(y,z);
        else
            if( k(y) == k(z) )
                fout << "DA\n";
            else
                fout << "NU\n";
    }

    return 0;
}