Pagini recente » Cod sursa (job #2388435) | Cod sursa (job #128622) | Cod sursa (job #2647290) | Cod sursa (job #275882) | Cod sursa (job #1832438)
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Scanner;
public class Main
{
private static int[] weight;
private static int[] root;
public Main(int n)
{
weight = new int[n + 1];
root = new int[n + 1];
Init(n);
}
private void Init(int n)
{
for (int index = 0; index < n; ++index)
{
weight[index] = 1;
root[index] = index;
}
}
private static void Union(int x, int y)
{
if (weight[x] > weight[y])
root[y] = x;
else
{
root[x] = y;
if (weight[x] == weight[y])
weight[y]++;
}
}
private static int Find(int node)
{
if (node != root[node])
root[node] = Find(root[node]);
return root[node];
}
public static void main(String[] args) throws IOException
{
Scanner is = new Scanner(new FileReader("disjoint.in"));
BufferedWriter os = new BufferedWriter(new FileWriter("disjoint.out"));
int n = is.nextInt(), m = is.nextInt();
new Main(n);
for (int i = 0; i < m; ++i)
{
int op = is.nextInt();
int x = is.nextInt();
int y = is.nextInt();
switch (op)
{
case 1:
Union(Find(x), Find(y));
break;
case 2:
os.write((Find(x) == Find(y) ? "DA\n" : "NU\n"));
break;
}
}
is.close();
os.close();
}
}