Pagini recente » Cod sursa (job #2156487) | Cod sursa (job #2181997) | Cod sursa (job #1830408) | Cod sursa (job #828416) | Cod sursa (job #376459)
Cod sursa(job #376459)
/*
* 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( int x, int y )
{list f1, f2;
for( f1=First[x], f2=First[y]; f1->next || f2->next; )
{
if( f1->next )
f1=f1->next;
if( f2->next )
f2=f2->next;
}
return f1->info == f2->info;
}
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 );
}
ofstream out("disjoint.out");
while( t-- )
{
in>>op>>x>>y;
if( 1 == op ) //join the two subsets
{list f;
for( f=First[x]; f->next; f=f->next );
f->next=First[y];
continue;
}
if( x == y || find( x, y ) )
out<<"DA\n";
else out<<"NU\n";
}
return 0;
}