Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class Solution {
- private static int[] rab;
- private static int[] l;
- private static Pair up (int a) {
- if (rab[a] == a) {
- return new Pair(a, 0);
- }
- Pair p = up (rab[a]);
- p.y += l[a];
- l[a] = p.y;
- rab[a] = p.x;
- return p;
- }
- public static void main(String[] args) throws IOException {
- BufferedReader in = new BufferedReader(new FileReader("input.txt"));
- String[] arr = in.readLine().split(" ");
- int n = Integer.parseInt(arr[0]);
- int m = Integer.parseInt(arr[1]);
- rab = new int[n];
- l = new int[n];
- for (int i = 0; i < n; i++) {
- rab[i] = i;
- }
- PrintWriter writer = new PrintWriter(new BufferedOutputStream(System.out));
- for (int i = 0; i < m; i++) {
- arr = in.readLine().split(" ");
- int z = Integer.parseInt(arr[0]);
- if (z == 1) {
- int a = Integer.parseInt(arr[1]) - 1;
- int b = Integer.parseInt(arr[2]) - 1;
- rab[a] = b;
- l[a] = 1;
- } else {
- int c = Integer.parseInt(arr[1]) - 1;
- Pair d = up(c);
- writer.println(d.y);
- }
- }
- writer.close();
- }
- private static final class Pair {
- private int x;
- private int y;
- private Pair(int x, int y) {
- this.x = x;
- this.y = y;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement