Submission #408883

# Submission time Handle Problem Language Result Execution time Memory
408883 2021-05-19T18:49:48 Z ScarletS Village (BOI20_village) C++17
100 / 100
119 ms 16632 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5+1;
int n, ct, sub[N], eC[N], ans[2][N], cost;
long long tot;
bitset<N> done;
vector<int> e[N], v;

void dfs(int c, int p)
{
    int mx=0;
    sub[c]=1;
    for (int i : e[c])
        if (i!=p)
        {
            dfs(i,c);
            sub[c]+=sub[i];
            tot+=min(sub[i],n-sub[i])*2;
            mx=max(mx,sub[i]);
        }
    if (max(mx,n-sub[c])<=n/2)
        ct=c;
}

void dfsAns(int c, int p)
{
    v.push_back(c);
    for (int i : e[c])
        if (i!=p)
            dfsAns(i,c);
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    int x,y;
    cin>>n;
    for (int i=1;i<n;++i)
    {
        cin>>x>>y;
        e[x].push_back(y);
        e[y].push_back(x);
        ans[0][i]=i;
    }
    ans[0][n]=n;
    stack<int> s;
    for (int i=1;i<=n;++i)
    {
        eC[i]=e[i].size();
        if (eC[i]==1)
            s.push(i);
    }
    while (!s.empty())
    {
        x=s.top();
        s.pop();
        done[x]=1;
        for (int i : e[x])
            if (!done[i])
            {
                if (--eC[i]==1)
                    s.push(i);
                if (ans[0][x]!=x)
                    continue;
                cost+=2;
                swap(ans[0][x],ans[0][i]);
            }
    }
    for (int i=1;i<=n;++i)
        if (ans[0][i]==i)
        {
            cost+=2;
            swap(ans[0][i],ans[0][e[i][0]]);
        }
    dfs(1,0);
    dfsAns(ct,0);
    cout<<cost<<" "<<tot<<"\n";
    for (int i=0;i<n;++i)
        ans[1][v[i]]=v[(i+n/2)%n];
    for (int j=0;j<2;++j)
    {
        for (int i=1;i<=n;++i)
            cout<<ans[j][i]<<" ";
        cout<<"\n";
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 3 ms 2636 KB Output is correct
5 Correct 3 ms 2636 KB Output is correct
6 Correct 3 ms 2636 KB Output is correct
7 Correct 3 ms 2764 KB Output is correct
8 Correct 3 ms 2636 KB Output is correct
9 Correct 3 ms 2636 KB Output is correct
10 Correct 3 ms 2764 KB Output is correct
11 Correct 3 ms 2636 KB Output is correct
12 Correct 3 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 3 ms 2636 KB Output is correct
17 Correct 3 ms 2636 KB Output is correct
18 Correct 3 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2764 KB Output is correct
22 Correct 3 ms 2748 KB Output is correct
23 Correct 3 ms 2732 KB Output is correct
24 Correct 3 ms 2636 KB Output is correct
25 Correct 3 ms 2620 KB Output is correct
26 Correct 3 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 3 ms 2636 KB Output is correct
29 Correct 3 ms 2636 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 3 ms 2636 KB Output is correct
22 Correct 3 ms 2636 KB Output is correct
23 Correct 3 ms 2636 KB Output is correct
24 Correct 3 ms 2764 KB Output is correct
25 Correct 3 ms 2636 KB Output is correct
26 Correct 3 ms 2636 KB Output is correct
27 Correct 3 ms 2764 KB Output is correct
28 Correct 3 ms 2636 KB Output is correct
29 Correct 3 ms 2636 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 2 ms 2636 KB Output is correct
32 Correct 2 ms 2636 KB Output is correct
33 Correct 3 ms 2636 KB Output is correct
34 Correct 3 ms 2636 KB Output is correct
35 Correct 3 ms 2636 KB Output is correct
36 Correct 2 ms 2636 KB Output is correct
37 Correct 2 ms 2636 KB Output is correct
38 Correct 2 ms 2764 KB Output is correct
39 Correct 3 ms 2748 KB Output is correct
40 Correct 3 ms 2732 KB Output is correct
41 Correct 3 ms 2636 KB Output is correct
42 Correct 3 ms 2620 KB Output is correct
43 Correct 3 ms 2636 KB Output is correct
44 Correct 2 ms 2636 KB Output is correct
45 Correct 3 ms 2636 KB Output is correct
46 Correct 3 ms 2636 KB Output is correct
47 Correct 2 ms 2636 KB Output is correct
48 Correct 71 ms 8624 KB Output is correct
49 Correct 89 ms 9292 KB Output is correct
50 Correct 89 ms 9324 KB Output is correct
51 Correct 78 ms 7844 KB Output is correct
52 Correct 78 ms 9176 KB Output is correct
53 Correct 73 ms 8588 KB Output is correct
54 Correct 39 ms 9356 KB Output is correct
55 Correct 96 ms 16632 KB Output is correct
56 Correct 88 ms 12752 KB Output is correct
57 Correct 82 ms 11524 KB Output is correct
58 Correct 86 ms 10360 KB Output is correct
59 Correct 85 ms 9292 KB Output is correct
60 Correct 61 ms 9976 KB Output is correct
61 Correct 71 ms 9788 KB Output is correct
62 Correct 73 ms 9872 KB Output is correct
63 Correct 63 ms 9408 KB Output is correct
64 Correct 74 ms 9712 KB Output is correct
65 Correct 71 ms 9792 KB Output is correct
66 Correct 71 ms 9440 KB Output is correct
67 Correct 48 ms 7872 KB Output is correct
68 Correct 60 ms 8700 KB Output is correct
69 Correct 71 ms 9896 KB Output is correct
70 Correct 63 ms 9408 KB Output is correct
71 Correct 45 ms 7612 KB Output is correct
72 Correct 52 ms 8404 KB Output is correct
73 Correct 66 ms 9916 KB Output is correct
74 Correct 66 ms 9248 KB Output is correct
75 Correct 78 ms 9020 KB Output is correct
76 Correct 76 ms 9144 KB Output is correct
77 Correct 68 ms 9452 KB Output is correct
78 Correct 46 ms 7180 KB Output is correct
79 Correct 55 ms 7912 KB Output is correct
80 Correct 119 ms 9076 KB Output is correct
81 Correct 70 ms 9664 KB Output is correct
82 Correct 67 ms 9664 KB Output is correct