Pagini recente » Cod sursa (job #2571384) | Cod sursa (job #376426) | Cod sursa (job #2453223) | Cod sursa (job #2054306) | Cod sursa (job #376441)
Cod sursa(job #376441)
/*
* File: main.cpp
* Author: virtualdemon
*
* Created on December 21, 2009, 4:13 PM
*/
#include <fstream>
#include <stdlib.h>
/*
*
*/
using namespace std;
struct vertex
{
int info;
vertex* next;
} **First, **Last;
typedef vertex* list;
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, cm, x, y;
ifstream in("disjoint.in");
in>>n>>t;
First=(list*)malloc( n*sizeof(list*) );
Last=(list*)malloc( n*sizeof(list*) );
for( i=1; i <= n; ++i ) //initialize
{
First[i]=new vertex;
First[i]->info=i;
First[i]->next=NULL;
Last[i]=First[i];
}
ofstream out("disjoint.out");
while( t-- )
{
in>>cm>>x>>y;
if( 1 == cm ) //join the two subsets
{
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;
}