Cod sursa(job #376449)

Utilizator alexandru92alexandru alexandru92 Data 21 decembrie 2009 17:06:30
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
/* 
 * File:   main.cpp
 * Author: virtualdemon
 *
 * Created on December 21, 2009, 4:13 PM
 */
#include <fstream>
#include <cstdlib>
#define Nmax 111111

/*
 * 
 */
using namespace std;
struct vertex
{
    int info;
    vertex* next;
} **First, **Last;
typedef vertex* list;
inline void push( list& f, int x )
{
    list q=new vertex;
    q->info=x;
    q->next=f;
    f=q;
}
bool find( list f, int x, int y )
{bool ok1=false, ok2=false;
    for(; f; f=f->next )
    {
        if( x == f->info )
            ok1=true;
        if( y == f->info )
            ok2=true;
        if( ok1 && ok2 )
            break;
    }
    return ok1 && ok2;
}
int main()
{int n, t, i, op, x, y;
    ifstream in("disjoint.in");
    in>>n>>t;
    First=(list*)calloc( n, sizeof(vertex) );
    Last=(list*)calloc( n, sizeof(vertex) );
    for( i=1; i <= n; ++i ) //init
    {
        push( First[i], i );
        Last[i]=First[i];
    }
    ofstream out("disjoint.out");
    while( t-- )
    {
        in>>op>>x>>y;
        if( 1 == op ) //join the two usbsets
        {
            Last[x]->next=First[y];
            First[y]=First[x];
            Last[x]=Last[y];
            continue;
        }
        if( find( First[x], x, y ) )
            out<<"DA\n";
        else out<<"NU\n";
    }
    return 0;
}