답안 #254794

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
254794 2020-07-30T15:30:21 Z model_code Village (BOI20_village) Java 11
100 / 100
450 ms 33804 KB
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;

public class Village {

    static int masa[] = new int[100000];
    static int masb[] = new int[100000];

    static int degree[] = new int[100001];
    static long neighboursum[] = new long[100001];
    static int rinda[] = new int[100000];
    static int subtree[] = new int[100001];
    static int place[] = new int[100001];
    static int fatchild[] = new int[100001];
    static int order[] = new int[100000];
    static int place2[] = new int[100001];
    static int group[] = new int[100002];
    static int groupSize[] = new int[100002];
    static int counters[] = new int[100002];
    static int n;

    static int min(int a, int b) {
        if (a < b) {
            return a;
        } else {
            return b;
        }
    }

    static StreamTokenizer in;
    static PrintWriter out;

    static int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }

    public static void main(String[] args) throws IOException {
        in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);

        n = nextInt();

        for (int i = 0; i < n - 1; i++) {
            masa[i] = nextInt();
            masb[i] = nextInt();

            degree[masa[i]]++;
            degree[masb[i]]++;

            neighboursum[masa[i]] += masb[i];
            neighboursum[masb[i]] += masa[i];
        }

        int prieksa = 0;
        int aste = 0;

        for (int i = 1; i <= n; i++) {
            subtree[i] = 1;
            place[i] = i;

            if (degree[i] == 1) {
                rinda[prieksa] = i;
                prieksa++;
            }
        }

        while (prieksa > aste) {

            int x = rinda[aste];
            aste++;

            int y = (int) neighboursum[x];

            if (y > 0) {
                if (fatchild[y] == 0 || subtree[fatchild[y]] < subtree[x]) {
                    fatchild[y] = x;
                }

                subtree[y] += subtree[x];

                degree[y]--;
                neighboursum[y] -= x;

                if (degree[y] == 1) {
                    rinda[prieksa] = y;
                    prieksa++;
                }
            }
        }

        long minDistance = 0;
        long maxDistance = 0;

        for (int i = 0; i < n; i++) {
            int x = rinda[i];

            if (place[x] == x) {
                int y = (int) neighboursum[x];

                if (y > 0) {
                    place[x] = place[y];
                    place[y] = x;
                    minDistance += 2;
                } else {

                    int j = i;
                    int z;

                    do {
                        j--;
                        z = rinda[j];
                    } while (j >= 0 && neighboursum[z] != x);

                    place[x] = place[z];
                    place[z] = x;

                    minDistance += 2;
                }
            }
        }

        for (int i = 1; i <= n; i++) {
            maxDistance += 2 * min(subtree[i], n - subtree[i]);
        }

        out.print(minDistance);
        out.print(" ");
        out.println(maxDistance);

        for (int i = 1; i <= n; i++) {
            out.print(place[i]);

            if (i < n) {
                out.print(" ");
            } else {
                out.println();
            }
        }

        // find centroid
        int c = 1;
        int cc = 1;

        do {
            c = cc;

            if (subtree[c] < n / 2 + n % 2) {
                cc = (int) neighboursum[c];
            } else if (subtree[c] > n / 2) {
                if (fatchild[c] != 0 && subtree[fatchild[c]] > n / 2) {
                    cc = fatchild[c];
                }
            }
        } while (cc != c);

        int nextGroup = 2;

        for (int i = n - 1; i >= 0; i--) {
            int x = rinda[i];

            if ((x == c) || (neighboursum[x] == c)) {
                group[x] = nextGroup;
                nextGroup++;
            } else if (neighboursum[x] > 0) {
                group[x] = group[(int) neighboursum[x]];
            } else {
                group[x] = 1;
            }

            groupSize[group[x]]++;
        }

        for (int i = 1; i < nextGroup; i++) {
            groupSize[i] += groupSize[i - 1];
        }

        for (int i = 1; i <= n; i++) {
            order[groupSize[group[i] - 1] + counters[group[i]]] = i;
            counters[group[i]]++;
        }

        for (int i = 0; i < n; i++) {
            place2[order[i]] = order[(i + n / 2) % n];
        }

        for (int i = 1; i <= n; i++) {
            out.print(place2[i]);

            if (i < n) {
                out.print(" ");
            } else {
                out.println();
            }
        }

        out.flush();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 15756 KB Output is correct
2 Correct 74 ms 15612 KB Output is correct
3 Correct 78 ms 15640 KB Output is correct
4 Correct 78 ms 15844 KB Output is correct
5 Correct 79 ms 15612 KB Output is correct
6 Correct 75 ms 15736 KB Output is correct
7 Correct 77 ms 15728 KB Output is correct
8 Correct 82 ms 15592 KB Output is correct
9 Correct 75 ms 15600 KB Output is correct
10 Correct 73 ms 15720 KB Output is correct
11 Correct 77 ms 15604 KB Output is correct
12 Correct 78 ms 15724 KB Output is correct
13 Correct 76 ms 15736 KB Output is correct
14 Correct 75 ms 15608 KB Output is correct
15 Correct 77 ms 15608 KB Output is correct
16 Correct 74 ms 15608 KB Output is correct
17 Correct 75 ms 15736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 86 ms 16860 KB Output is correct
2 Correct 86 ms 16872 KB Output is correct
3 Correct 89 ms 16996 KB Output is correct
4 Correct 95 ms 17268 KB Output is correct
5 Correct 103 ms 16888 KB Output is correct
6 Correct 103 ms 17116 KB Output is correct
7 Correct 96 ms 17000 KB Output is correct
8 Correct 98 ms 16760 KB Output is correct
9 Correct 98 ms 17020 KB Output is correct
10 Correct 94 ms 17240 KB Output is correct
11 Correct 105 ms 17000 KB Output is correct
12 Correct 100 ms 17528 KB Output is correct
13 Correct 96 ms 16888 KB Output is correct
14 Correct 107 ms 17128 KB Output is correct
15 Correct 102 ms 17192 KB Output is correct
16 Correct 107 ms 16980 KB Output is correct
17 Correct 104 ms 16920 KB Output is correct
18 Correct 100 ms 17768 KB Output is correct
19 Correct 97 ms 17016 KB Output is correct
20 Correct 98 ms 17016 KB Output is correct
21 Correct 100 ms 17016 KB Output is correct
22 Correct 101 ms 17176 KB Output is correct
23 Correct 101 ms 17016 KB Output is correct
24 Correct 108 ms 16880 KB Output is correct
25 Correct 94 ms 16968 KB Output is correct
26 Correct 100 ms 17148 KB Output is correct
27 Correct 98 ms 17000 KB Output is correct
28 Correct 103 ms 17524 KB Output is correct
29 Correct 109 ms 16888 KB Output is correct
30 Correct 102 ms 17012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 15756 KB Output is correct
2 Correct 74 ms 15612 KB Output is correct
3 Correct 78 ms 15640 KB Output is correct
4 Correct 78 ms 15844 KB Output is correct
5 Correct 79 ms 15612 KB Output is correct
6 Correct 75 ms 15736 KB Output is correct
7 Correct 77 ms 15728 KB Output is correct
8 Correct 82 ms 15592 KB Output is correct
9 Correct 75 ms 15600 KB Output is correct
10 Correct 73 ms 15720 KB Output is correct
11 Correct 77 ms 15604 KB Output is correct
12 Correct 78 ms 15724 KB Output is correct
13 Correct 76 ms 15736 KB Output is correct
14 Correct 75 ms 15608 KB Output is correct
15 Correct 77 ms 15608 KB Output is correct
16 Correct 74 ms 15608 KB Output is correct
17 Correct 75 ms 15736 KB Output is correct
18 Correct 86 ms 16860 KB Output is correct
19 Correct 86 ms 16872 KB Output is correct
20 Correct 89 ms 16996 KB Output is correct
21 Correct 95 ms 17268 KB Output is correct
22 Correct 103 ms 16888 KB Output is correct
23 Correct 103 ms 17116 KB Output is correct
24 Correct 96 ms 17000 KB Output is correct
25 Correct 98 ms 16760 KB Output is correct
26 Correct 98 ms 17020 KB Output is correct
27 Correct 94 ms 17240 KB Output is correct
28 Correct 105 ms 17000 KB Output is correct
29 Correct 100 ms 17528 KB Output is correct
30 Correct 96 ms 16888 KB Output is correct
31 Correct 107 ms 17128 KB Output is correct
32 Correct 102 ms 17192 KB Output is correct
33 Correct 107 ms 16980 KB Output is correct
34 Correct 104 ms 16920 KB Output is correct
35 Correct 100 ms 17768 KB Output is correct
36 Correct 97 ms 17016 KB Output is correct
37 Correct 98 ms 17016 KB Output is correct
38 Correct 100 ms 17016 KB Output is correct
39 Correct 101 ms 17176 KB Output is correct
40 Correct 101 ms 17016 KB Output is correct
41 Correct 108 ms 16880 KB Output is correct
42 Correct 94 ms 16968 KB Output is correct
43 Correct 100 ms 17148 KB Output is correct
44 Correct 98 ms 17000 KB Output is correct
45 Correct 103 ms 17524 KB Output is correct
46 Correct 109 ms 16888 KB Output is correct
47 Correct 102 ms 17012 KB Output is correct
48 Correct 419 ms 32208 KB Output is correct
49 Correct 373 ms 33588 KB Output is correct
50 Correct 376 ms 31656 KB Output is correct
51 Correct 334 ms 30508 KB Output is correct
52 Correct 388 ms 32764 KB Output is correct
53 Correct 373 ms 32164 KB Output is correct
54 Correct 343 ms 25924 KB Output is correct
55 Correct 403 ms 32952 KB Output is correct
56 Correct 432 ms 33500 KB Output is correct
57 Correct 375 ms 32536 KB Output is correct
58 Correct 417 ms 32044 KB Output is correct
59 Correct 381 ms 32092 KB Output is correct
60 Correct 423 ms 32460 KB Output is correct
61 Correct 417 ms 33060 KB Output is correct
62 Correct 404 ms 31824 KB Output is correct
63 Correct 392 ms 31360 KB Output is correct
64 Correct 398 ms 33608 KB Output is correct
65 Correct 377 ms 31324 KB Output is correct
66 Correct 392 ms 32964 KB Output is correct
67 Correct 312 ms 30264 KB Output is correct
68 Correct 383 ms 31860 KB Output is correct
69 Correct 405 ms 32156 KB Output is correct
70 Correct 397 ms 31808 KB Output is correct
71 Correct 380 ms 30256 KB Output is correct
72 Correct 342 ms 30780 KB Output is correct
73 Correct 374 ms 33176 KB Output is correct
74 Correct 386 ms 30772 KB Output is correct
75 Correct 407 ms 33472 KB Output is correct
76 Correct 399 ms 32008 KB Output is correct
77 Correct 425 ms 33340 KB Output is correct
78 Correct 343 ms 29852 KB Output is correct
79 Correct 390 ms 30904 KB Output is correct
80 Correct 395 ms 33804 KB Output is correct
81 Correct 442 ms 32292 KB Output is correct
82 Correct 450 ms 33720 KB Output is correct