Submission #254792

# Submission time Handle Problem Language Result Execution time Memory
254792 2020-07-30T15:29:19 Z model_code Village (BOI20_village) C++17
100 / 100
72 ms 6648 KB
#include <iostream>
using namespace std;

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]);
    }
    
    cout << minDistance << " " << maxDistance << endl;
    
    for (int i = 1; i <= n; i++)
    {
        printf("%d", place[i]);
        
        if (i < n)
        {
            printf(" ");
        }
        else
        {
            printf("\n");
        }
    }
    
    // find centroid
    
    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.cpp: In function 'int main()':
Village.cpp:37:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
Village.cpp:41:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &mas[i].a, &mas[i].b);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 0 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 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory 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 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 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
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 0 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 0 ms 384 KB Output is correct
17 Correct 0 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
31 Correct 1 ms 384 KB Output is correct
32 Correct 1 ms 384 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 61 ms 5768 KB Output is correct
49 Correct 62 ms 6264 KB Output is correct
50 Correct 60 ms 6264 KB Output is correct
51 Correct 46 ms 4984 KB Output is correct
52 Correct 63 ms 6136 KB Output is correct
53 Correct 57 ms 5624 KB Output is correct
54 Correct 34 ms 3320 KB Output is correct
55 Correct 64 ms 6264 KB Output is correct
56 Correct 64 ms 6264 KB Output is correct
57 Correct 66 ms 6136 KB Output is correct
58 Correct 64 ms 6264 KB Output is correct
59 Correct 66 ms 6264 KB Output is correct
60 Correct 63 ms 6648 KB Output is correct
61 Correct 63 ms 6008 KB Output is correct
62 Correct 60 ms 6012 KB Output is correct
63 Correct 57 ms 5752 KB Output is correct
64 Correct 60 ms 6264 KB Output is correct
65 Correct 61 ms 6392 KB Output is correct
66 Correct 67 ms 5752 KB Output is correct
67 Correct 42 ms 4600 KB Output is correct
68 Correct 50 ms 5368 KB Output is correct
69 Correct 58 ms 6136 KB Output is correct
70 Correct 59 ms 5880 KB Output is correct
71 Correct 53 ms 4472 KB Output is correct
72 Correct 49 ms 4984 KB Output is correct
73 Correct 72 ms 6392 KB Output is correct
74 Correct 54 ms 5752 KB Output is correct
75 Correct 60 ms 6264 KB Output is correct
76 Correct 63 ms 6264 KB Output is correct
77 Correct 55 ms 5880 KB Output is correct
78 Correct 40 ms 4216 KB Output is correct
79 Correct 45 ms 4984 KB Output is correct
80 Correct 60 ms 6264 KB Output is correct
81 Correct 60 ms 5900 KB Output is correct
82 Correct 58 ms 6136 KB Output is correct