Pagini recente » Cod sursa (job #2268169) | Cod sursa (job #1263392) | Cod sursa (job #2046649) | Cod sursa (job #1480120) | Cod sursa (job #1832439)
import java.io.*;
import java.util.StringTokenizer;
class InputReader
{
private BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream)
{
reader = new BufferedReader(new InputStreamReader(stream), 65536);
tokenizer = null;
}
private String nextToken()
{
while (tokenizer == null || !tokenizer.hasMoreTokens()){
try
{
tokenizer = new StringTokenizer(reader.readLine());
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt()
{
return Integer.parseInt(nextToken());
}
public String nextString()
{
return nextToken();
}
}
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
{
InputReader is = new InputReader(new FileInputStream("disjoint.in"));
PrintWriter os = new PrintWriter(new FileOutputStream("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.println(Find(x) == Find(y) ? "DA" : "NU");
break;
}
}
os.close();
}
}