Pagini recente » Cod sursa (job #967452) | Cod sursa (job #2335999) | Cod sursa (job #833977) | Cod sursa (job #2197623) | Cod sursa (job #1705426)
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
public class Main {
static int[] rank;
static int[] set;
static int findSet (int x) {
int y = x;
while (set[y] != y)
y = set[y];
int res = y;
y = x;
while (set[y] != y){
set[y] = res;
y = set[y];
}
return res;
}
static void union (int n1, int n2) {
int u = findSet(n1);
int v = findSet(n2);
if (rank[u] > rank[v]){
set[v] = u;
return;
}
if (rank[u] < rank[v]){
set[u] = v;
return;
}
set[u] = v;
rank[v]++;
}
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new FileReader("disjoint.in"));
//PrintStream out = new PrintStream();
OutputStream out = new BufferedOutputStream ( new FileOutputStream("disjoint.out") );
String tmp[] = bf.readLine().split(" ");
int n = Integer.parseInt(tmp[0]);
int m = Integer.parseInt(tmp[1]);
set = new int[n+1];
rank = new int[n+1];
for(int i = 1; i <= n; i++) {
set[i] = i;
rank[i] = 1;
}
int x,y ;
for (int i = 0; i < m; i++) {
tmp = bf.readLine().split(" ");
int type = Integer.parseInt(tmp[0]);
int n1 = Integer.parseInt(tmp[1]);
int n2 = Integer.parseInt(tmp[2]);
if (type == 1)
union(n1, n2);
if (type == 2){
x = findSet(n1);
y = findSet(n2);
if (x == y)
out.write(("DA\n").getBytes());
else
out.write(("NU\n").getBytes());
}
}
out.flush();
out.close();
bf.close();
}
}