Submission #343068

# Submission time Handle Problem Language Result Execution time Memory
343068 2021-01-03T11:48:10 Z leinad2 Village (BOI20_village) C++17
100 / 100
179 ms 21612 KB
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, i, j, k, a, b, ans[100010], A[100010], res, cent, sz[100010], dep[100010], in[100010], revin[100010], cnt, res2, ans2[100010];
vector<int>adj[100010];
void dfs(int v, int par, int depth)
{
    in[v]=++cnt;
    dep[v]=depth;
    sz[v]=1;
    for(int i=0;i<adj[v].size();i++)
    {
        int p=adj[v][i];
        if(p==par)continue;
        dfs(p, v, depth+1);
        sz[v]+=sz[p];
    }
}
int centroid(int v, int par)
{
    for(int i=0;i<adj[v].size();i++)
    {
        int p=adj[v][i];
        if(p==par)continue;
        if(sz[p]*2>n)
        {
            return centroid(p, v);
        }
    }
    return v;
}
int f(int x)
{
    return ((x+(n/2))%n==0?n:(x+(n/2))%n);
}
queue<int>q;
void dfs2(int v, int par)
{
    if(adj[v].size()==1&&par==adj[v][0])
    {
        A[v]=1;
        swap(ans2[adj[v][0]], ans2[v]);
        res2+=2;
        return;
    }
    vector<int>V;
    for(int i=0;i<adj[v].size();i++)
    {
        int p=adj[v][i];
        if(p==par)continue;
        dfs2(p, v);
        if(!A[p])
        {
            V.push_back(p);
        }
    }
    for(int i=0;i<V.size();i++)
    {
        swap(ans2[v], ans2[V[i]]);
        res2+=2;
    }
    if(ans2[v]!=v)A[v]=1;
}
main()
{
    for(scanf("%lld", &n);++i<n;)
    {
        scanf("%lld %lld", &a, &b);
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    dfs(1, 0, 0);
    cent=centroid(1, 0);
    cnt=0;
    dfs(cent, 0, 0);
    for(i=0;i++<n;)ans2[i]=i;
    dfs2(1, 0);
    if(ans2[1]==1)
    {
        swap(ans2[1], ans2[adj[1][0]]);
        res2+=2;
    }
    for(i=0;i++<n;)res+=dep[i]*2;
    for(i=0;i++<n;)
    {
        revin[in[i]]=i;
    }
    for(i=0;i++<n;)
    {
        ans[revin[i]]=revin[f(i)];
    }
    printf("%lld %lld\n", res2, res);
    for(i=0;i++<n;)printf("%lld ", ans2[i]);
    puts("");
    for(i=0;i++<n;)
    {
        printf("%lld ", ans[i]);
    }
}

Compilation message

Village.cpp: In function 'void dfs(long long int, long long int, long long int)':
Village.cpp:11:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 |     for(int i=0;i<adj[v].size();i++)
      |                 ~^~~~~~~~~~~~~~
Village.cpp: In function 'long long int centroid(long long int, long long int)':
Village.cpp:21:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |     for(int i=0;i<adj[v].size();i++)
      |                 ~^~~~~~~~~~~~~~
Village.cpp: In function 'void dfs2(long long int, long long int)':
Village.cpp:47:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |     for(int i=0;i<adj[v].size();i++)
      |                 ~^~~~~~~~~~~~~~
Village.cpp:57:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |     for(int i=0;i<V.size();i++)
      |                 ~^~~~~~~~~
Village.cpp: At global scope:
Village.cpp:64:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   64 | main()
      |      ^
Village.cpp: In function 'int main()':
Village.cpp:66:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   66 |     for(scanf("%lld", &n);++i<n;)
      |         ~~~~~^~~~~~~~~~~~
Village.cpp:68:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   68 |         scanf("%lld %lld", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2816 KB Output is correct
7 Correct 2 ms 2668 KB Output is correct
8 Correct 2 ms 2668 KB Output is correct
9 Correct 2 ms 2668 KB Output is correct
10 Correct 2 ms 2668 KB Output is correct
11 Correct 2 ms 2668 KB Output is correct
12 Correct 2 ms 2668 KB Output is correct
13 Correct 2 ms 2668 KB Output is correct
14 Correct 2 ms 2668 KB Output is correct
15 Correct 2 ms 2668 KB Output is correct
16 Correct 2 ms 2668 KB Output is correct
17 Correct 2 ms 2668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
3 Correct 2 ms 2796 KB Output is correct
4 Correct 3 ms 2796 KB Output is correct
5 Correct 3 ms 2796 KB Output is correct
6 Correct 3 ms 2796 KB Output is correct
7 Correct 3 ms 2796 KB Output is correct
8 Correct 3 ms 2816 KB Output is correct
9 Correct 3 ms 2796 KB Output is correct
10 Correct 3 ms 2796 KB Output is correct
11 Correct 3 ms 2884 KB Output is correct
12 Correct 2 ms 2796 KB Output is correct
13 Correct 3 ms 2796 KB Output is correct
14 Correct 3 ms 2796 KB Output is correct
15 Correct 3 ms 2796 KB Output is correct
16 Correct 3 ms 2796 KB Output is correct
17 Correct 3 ms 2796 KB Output is correct
18 Correct 4 ms 2796 KB Output is correct
19 Correct 3 ms 2796 KB Output is correct
20 Correct 3 ms 2796 KB Output is correct
21 Correct 3 ms 2796 KB Output is correct
22 Correct 3 ms 2796 KB Output is correct
23 Correct 3 ms 2796 KB Output is correct
24 Correct 3 ms 2796 KB Output is correct
25 Correct 2 ms 2796 KB Output is correct
26 Correct 3 ms 2796 KB Output is correct
27 Correct 2 ms 2796 KB Output is correct
28 Correct 3 ms 2796 KB Output is correct
29 Correct 3 ms 2796 KB Output is correct
30 Correct 3 ms 2796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2816 KB Output is correct
7 Correct 2 ms 2668 KB Output is correct
8 Correct 2 ms 2668 KB Output is correct
9 Correct 2 ms 2668 KB Output is correct
10 Correct 2 ms 2668 KB Output is correct
11 Correct 2 ms 2668 KB Output is correct
12 Correct 2 ms 2668 KB Output is correct
13 Correct 2 ms 2668 KB Output is correct
14 Correct 2 ms 2668 KB Output is correct
15 Correct 2 ms 2668 KB Output is correct
16 Correct 2 ms 2668 KB Output is correct
17 Correct 2 ms 2668 KB Output is correct
18 Correct 2 ms 2668 KB Output is correct
19 Correct 2 ms 2796 KB Output is correct
20 Correct 2 ms 2796 KB Output is correct
21 Correct 3 ms 2796 KB Output is correct
22 Correct 3 ms 2796 KB Output is correct
23 Correct 3 ms 2796 KB Output is correct
24 Correct 3 ms 2796 KB Output is correct
25 Correct 3 ms 2816 KB Output is correct
26 Correct 3 ms 2796 KB Output is correct
27 Correct 3 ms 2796 KB Output is correct
28 Correct 3 ms 2884 KB Output is correct
29 Correct 2 ms 2796 KB Output is correct
30 Correct 3 ms 2796 KB Output is correct
31 Correct 3 ms 2796 KB Output is correct
32 Correct 3 ms 2796 KB Output is correct
33 Correct 3 ms 2796 KB Output is correct
34 Correct 3 ms 2796 KB Output is correct
35 Correct 4 ms 2796 KB Output is correct
36 Correct 3 ms 2796 KB Output is correct
37 Correct 3 ms 2796 KB Output is correct
38 Correct 3 ms 2796 KB Output is correct
39 Correct 3 ms 2796 KB Output is correct
40 Correct 3 ms 2796 KB Output is correct
41 Correct 3 ms 2796 KB Output is correct
42 Correct 2 ms 2796 KB Output is correct
43 Correct 3 ms 2796 KB Output is correct
44 Correct 2 ms 2796 KB Output is correct
45 Correct 3 ms 2796 KB Output is correct
46 Correct 3 ms 2796 KB Output is correct
47 Correct 3 ms 2796 KB Output is correct
48 Correct 120 ms 12192 KB Output is correct
49 Correct 138 ms 13164 KB Output is correct
50 Correct 136 ms 13164 KB Output is correct
51 Correct 111 ms 10792 KB Output is correct
52 Correct 139 ms 13036 KB Output is correct
53 Correct 116 ms 12036 KB Output is correct
54 Correct 70 ms 11884 KB Output is correct
55 Correct 179 ms 21612 KB Output is correct
56 Correct 148 ms 17260 KB Output is correct
57 Correct 144 ms 15852 KB Output is correct
58 Correct 133 ms 14572 KB Output is correct
59 Correct 137 ms 13420 KB Output is correct
60 Correct 77 ms 13408 KB Output is correct
61 Correct 94 ms 13276 KB Output is correct
62 Correct 106 ms 13548 KB Output is correct
63 Correct 94 ms 12780 KB Output is correct
64 Correct 120 ms 13548 KB Output is correct
65 Correct 101 ms 13548 KB Output is correct
66 Correct 97 ms 12908 KB Output is correct
67 Correct 59 ms 10472 KB Output is correct
68 Correct 101 ms 12012 KB Output is correct
69 Correct 105 ms 13676 KB Output is correct
70 Correct 95 ms 13036 KB Output is correct
71 Correct 69 ms 10348 KB Output is correct
72 Correct 71 ms 11372 KB Output is correct
73 Correct 102 ms 13676 KB Output is correct
74 Correct 90 ms 12652 KB Output is correct
75 Correct 164 ms 13548 KB Output is correct
76 Correct 118 ms 13036 KB Output is correct
77 Correct 120 ms 13292 KB Output is correct
78 Correct 63 ms 9836 KB Output is correct
79 Correct 78 ms 10988 KB Output is correct
80 Correct 132 ms 13548 KB Output is correct
81 Correct 103 ms 13420 KB Output is correct
82 Correct 110 ms 13292 KB Output is correct