Cod sursa(job #975270)

Utilizator Daniel3717Aleca Daniel Adrian Daniel3717 Data 19 iulie 2013 17:13:17
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.61 kb
#include <iostream>
#include <fstream>
using namespace std;
struct nod{
nod *ant;
} *p,*q,*pp,*qq,*ax;
struct vnod{
nod *z;} v[100001];
int n,m,i,t,x,y,kx,ky;
int main(void)
{
    FILE * f;
    f=fopen("disjoint.in","r");
    ofstream g("disjoint.out");
    fscanf(f,"%d%d",&n,&m);
    for (i=1;i<=n;i++)
    {
        p=new(nod);
        p->ant=NULL;
        v[i].z=p;
    }
    for (i=1;i<=m;i++)
    {
        fscanf(f,"%d%d%d",&t,&x,&y);
        if (t==1)
        {
            p=v[x].z;
            q=v[y].z;
            kx=0;
            ky=0;
            while (p->ant!=NULL)
            {
                kx++;
                p=p->ant;
            }
            while (q->ant!=NULL)
            {
                ky++;
                q=q->ant;
            }
            if (kx>ky)
                q->ant=p;
            else
                p->ant=q;
        }
        if (t==2)
        {
            p=v[x].z;
            q=v[y].z;
            while (p->ant!=NULL)
                p=p->ant;
            while (q->ant!=NULL)
                q=q->ant;
            pp=v[x].z;
            if (pp!=p)
            while (pp->ant!=p)
            {
                ax=pp;
                pp=pp->ant;
                ax->ant=p;
            }
            qq=v[y].z;
            if (qq!=q)
            while (qq->ant!=q)
            {
                ax=qq;
                qq=qq->ant;
                ax->ant=q;
            }
            if (p==q)
                g<<"DA\n";
            else
                g<<"NU\n";
        }
    }
    g.close();
    return 0;
}