Pagini recente » Cod sursa (job #6598) | Cod sursa (job #6743) | Cod sursa (job #1188914) | preONI 2008 - Runda 3 | Cod sursa (job #1229049)
#include<fstream>
using namespace std;
ofstream g("disjoint.out");
int n,m,a[100010],b[100010];
void weld(int x,int y);
int position(int x);
void read();
void write(bool x);
void read(){
ifstream f("disjoint.in");
f>>n>>m;
for(int i=1;i<=n;i++)
{
a[i]=i;
b[i]=1;
}
for(int i=1;i<=m;i++)
{
int x,y,z;
f>>x>>y>>z;
if(x==1) weld(position(y),position(z));
else if(position(y)==position(z)) write(1);
else write(0);
}
f.close();
}
void weld(int x,int y){
if(b[x]>=b[y]) a[y]=x;
else a[x]=y;
if(b[x]==b[y]) b[x]++;
}
int position(int x){
int i,j;
for(i=x;i!=a[i];i=a[i]);
for(j=x;i!=a[j];j=a[j]) a[j]=i;
return i;
}
void write(bool x){
if(x==1) g<<"DA\n";
else g<<"NU\n";
}
int main(){
read();
return 0;
}