Cod sursa(job #1046865)

Utilizator PsychoAlexAlexandru Buicescu PsychoAlex Data 3 decembrie 2013 17:36:18
Problema Range minimum query Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.83 kb
#include <iostream>
#include <fstream>
#include <vector>

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

struct nod
{
    int value;
    int marime;
    nod *next;
};

int n, m;
//std::vector<nod> vertecies;

nod *vertecies[100001];

void citire()
{
    int x, y, z;
    fin>>n>>m;
    for(int i = 0; i < m; i++)
    {
        fin>>x>>y>>z;
        if(x == 1)
        {
            if(vertecies[y])
            {

            }
            else
            {
                vertecies[y] = new nod;
                vertecies[y]->value = y;
                vertecies[y]->next = NULL;
                vertecies[y]->marime = 1;
            }

            if(vertecies[z])
            {
                if(vertecies[y]->marime > vertecies[z]->marime)
                {
                    if(vertecies[y]->next)
                    {
                        vertecies[z]->next = vertecies[y];
                        if(vertecies[y]->marime == 1)
                        {
                            vertecies[y]->marime++;
                        }
                    }
                    else
                    {
                        vertecies[y]->next = vertecies[z];
                        if(vertecies[z]->marime == 1)
                        {
                            vertecies[z]->marime++;
                        }
                    }
                }
                else
                {
                    if(vertecies[z]->next)
                    {
                        vertecies[y]->next = vertecies[z];
                        if(vertecies[z]->marime == 1)
                        {
                            vertecies[z]->marime++;
                        }
                    }
                    else
                    {
                        vertecies[z]->next = vertecies[y];
                        if(vertecies[y]->marime == 1)
                        {
                            vertecies[y]->marime++;
                        }
                    }
                }
            }
            else
            {
                vertecies[z] = new nod;
                vertecies[z]->value = y;
                vertecies[z]->marime = 1;
                vertecies[z]->next = vertecies[y];
                if(vertecies[y]->marime == 1)
                {
                    vertecies[y]->marime++;
                }
            }
        }
        else
            if(x == 2)
            {
                nod *p1 = vertecies[y];
                int val1 = -1;
                while(p1)
                {
                    val1 = p1->value;
                    p1 = p1->next;
                }
                nod *p2 = vertecies[z];
                nod *lastP2 = p2;
                int val2 = -1;
                while(p2)
                {
                    val2 = p2->value;
                    lastP2 = p2;
                    p2 = p2->next;
                }
                nod *p = vertecies[z];
                while(p)
                {
                    p = p->next;
                }
                if(val1 != val2)
                {
                    fout<<"NU"<<'\n';
                }
                else
                {
                    if(val1 == -1 && y == z)
                    {
                        fout<<"DA"<<'\n';
                    }
                    else
                        if(val1 != -1)
                        {
                            fout<<"DA"<<'\n';
                        }
                        else
                        {
                            fout<<"NU"<<'\n';
                        }
                }
//                fout<<val1<<' '<<val2<<'\n';
            }
    }
}

int main()
{
    citire();
    return 0;
}