답안 #300438

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
300438 2020-09-17T07:16:00 Z bacegen4o Village (BOI20_village) C
100 / 100
71 ms 7672 KB
#pragma GCC optimize "-O3"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<assert.h>
#include<stdbool.h>
#include<limits.h>

struct paris
{
    int a;
    int b;
} mas[100000];
int degree[100001];
long long neighboursum[100001];
int rinda[100000];
int subtree[100001];
int place[100001];
int fatchild[100001];
int order[100000];
int place2[100001];
int group[100002];
int groupSize[100002];
int counters[100002];
int n;
int min(int a, int b){
    if (a < b)
    {
        return a;
    }
    else
    {
        return b;
    }
}
int main(){
    scanf("%d", &n);
    for (int i = 0; i < n - 1; i++)
    {
        scanf("%d %d", &mas[i].a, &mas[i].b);
        degree[mas[i].a]++;
        degree[mas[i].b]++;
        neighboursum[mas[i].a] += mas[i].b;
        neighboursum[mas[i].b] += mas[i].a;
    }
    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 = 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 long minDistance = 0;
    long long maxDistance = 0;
    for (int i = 0; i < n; i++)
    {
        int x = rinda[i];
        if (place[x] == x)
        {
            int y = 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]);
    }
    printf("%lld %lld\n", minDistance, maxDistance);
    for (int i = 1; i <= n; i++)
    {
        printf("%d", place[i]);
        if (i < n)
        {
            printf(" ");
        }
        else
        {
            printf("\n");
        }
    }
    int c = 1;
    int cc = 1;
    do
    {
        c = cc;
        if (subtree[c] < n / 2 + n % 2)
        {
            cc = 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[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++)
    {
        printf("%d", place2[i]);
        if (i < n)
        {
            printf(" ");
        }
        else
        {
            printf("\n");
        }
    }
    return 0;
}

Compilation message

Village.c: In function 'main':
Village.c:38:5: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
   38 |     scanf("%d", &n);
      |     ^~~~~~~~~~~~~~~
Village.c:41:9: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
   41 |         scanf("%d %d", &mas[i].a, &mas[i].b);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 416 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 512 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 544 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 416 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 512 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 1 ms 544 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 1 ms 384 KB Output is correct
37 Correct 1 ms 384 KB Output is correct
38 Correct 1 ms 384 KB Output is correct
39 Correct 1 ms 384 KB Output is correct
40 Correct 1 ms 384 KB Output is correct
41 Correct 1 ms 384 KB Output is correct
42 Correct 1 ms 384 KB Output is correct
43 Correct 1 ms 384 KB Output is correct
44 Correct 1 ms 384 KB Output is correct
45 Correct 1 ms 384 KB Output is correct
46 Correct 1 ms 384 KB Output is correct
47 Correct 1 ms 384 KB Output is correct
48 Correct 58 ms 6776 KB Output is correct
49 Correct 63 ms 7288 KB Output is correct
50 Correct 64 ms 7288 KB Output is correct
51 Correct 51 ms 5752 KB Output is correct
52 Correct 64 ms 7288 KB Output is correct
53 Correct 57 ms 6524 KB Output is correct
54 Correct 32 ms 3832 KB Output is correct
55 Correct 65 ms 7416 KB Output is correct
56 Correct 64 ms 7288 KB Output is correct
57 Correct 63 ms 7288 KB Output is correct
58 Correct 64 ms 7288 KB Output is correct
59 Correct 63 ms 7288 KB Output is correct
60 Correct 58 ms 7672 KB Output is correct
61 Correct 62 ms 7160 KB Output is correct
62 Correct 61 ms 7160 KB Output is correct
63 Correct 56 ms 6776 KB Output is correct
64 Correct 61 ms 7292 KB Output is correct
65 Correct 60 ms 7288 KB Output is correct
66 Correct 56 ms 6780 KB Output is correct
67 Correct 56 ms 5376 KB Output is correct
68 Correct 71 ms 6264 KB Output is correct
69 Correct 61 ms 7288 KB Output is correct
70 Correct 57 ms 6904 KB Output is correct
71 Correct 46 ms 5240 KB Output is correct
72 Correct 46 ms 5880 KB Output is correct
73 Correct 59 ms 7288 KB Output is correct
74 Correct 57 ms 6776 KB Output is correct
75 Correct 68 ms 7288 KB Output is correct
76 Correct 65 ms 7416 KB Output is correct
77 Correct 58 ms 6904 KB Output is correct
78 Correct 41 ms 4984 KB Output is correct
79 Correct 48 ms 5752 KB Output is correct
80 Correct 66 ms 7288 KB Output is correct
81 Correct 57 ms 6904 KB Output is correct
82 Correct 60 ms 7288 KB Output is correct