답안 #1014993

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1014993 2024-07-05T22:04:25 Z LeonidCuk Village (BOI20_village) C++17
100 / 100
64 ms 15824 KB
#include <bits/stdc++.h>
using namespace std;
vector<int>g[100000],sz(100000),vmin(100000),vmax(100000),temp;
int n;
long long int resmax=0,resmin=0;
void dfsmax(int a,int b,int c)
{
    if(c==0)
        {
            temp.push_back(a);
        }
    for(auto i:g[a])
    {
        if(i!=b&&c==1)
        {
            dfsmax(i,a,c);
            resmax+=(2*min(sz[i],n-sz[i]));
        }
        else if(i!=b)
        {
            dfsmax(i,a,c);
        }
    }

}
void dfsmin(int a,int b)
{
    for(auto i:g[a])
    {
        if(i!=b)
        {
            dfsmin(i,a);
            if(vmin[i]==i)
            {
                swap(vmin[i],vmin[a]);
                resmin+=2;
            }
        }
    }
}
void dfs(int a,int b)
{
    sz[a]=1;
    for(auto i:g[a])
    {
        if(i!=b)
        {
            dfs(i,a);
            sz[a]+=sz[i];
        }
    }
}
int findcen(int a)
{
    int b=a;
    while(true)
    {
        bool check=true;
        for(auto i:g[a])
        {
            if(i!=b&&sz[i]*2>n)
            {
                b=a;
                a=i;
                check=false;
                break;
            }
        }
        if(check)
        {
            break;
        }
    }
    return a;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int a,b;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        vmax[i]=i;
        vmin[i]=i;
    }
    for(int i=0;i<n-1;i++)
    {
        cin>>a>>b;
        a--;b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(0,0);
    int cen=findcen(0);
    dfs(cen,cen);
    a=-1;b=-1;
    dfsmin(cen,cen);
    dfsmax(cen,cen,1);
    for(auto i:g[cen])
    {
        if(sz[i]>b)
        {
            b=sz[i];
            a=i;
        }
    }
    temp.push_back(cen);
    dfsmax(a,cen,0);
    for(auto i:g[cen])
    {
        if(i!=a)
        {
           dfsmax(i,cen,0);
        }
    }
    for(int i=0;i<n;i++)
    {
        vmax[temp[i]]=temp[(n/2+i)%n];
    }
    if(vmax[cen]==cen)
    {
        swap(vmax[cen],vmax[g[cen][0]]);
    }
    if(vmin[cen]==cen)
    {
        swap(vmin[cen],vmin[g[cen][0]]);
        resmin+=2;
    }
    cout<<resmin<<" "<<resmax<<"\n";
    for(int i=0;i<n;i++)
    {
        cout<<vmin[i]+1<<" ";
    }
    cout<<"\n";
    for(int i=0;i<n;i++)
    {
        cout<<vmax[i]+1<<" ";
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3928 KB Output is correct
2 Correct 2 ms 3932 KB Output is correct
3 Correct 2 ms 3928 KB Output is correct
4 Correct 2 ms 3932 KB Output is correct
5 Correct 2 ms 3932 KB Output is correct
6 Correct 2 ms 3992 KB Output is correct
7 Correct 2 ms 3932 KB Output is correct
8 Correct 2 ms 3996 KB Output is correct
9 Correct 1 ms 3932 KB Output is correct
10 Correct 1 ms 3768 KB Output is correct
11 Correct 2 ms 3932 KB Output is correct
12 Correct 2 ms 3932 KB Output is correct
13 Correct 2 ms 3932 KB Output is correct
14 Correct 1 ms 3932 KB Output is correct
15 Correct 2 ms 3932 KB Output is correct
16 Correct 2 ms 3932 KB Output is correct
17 Correct 2 ms 3932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3928 KB Output is correct
2 Correct 2 ms 3932 KB Output is correct
3 Correct 1 ms 3932 KB Output is correct
4 Correct 2 ms 4012 KB Output is correct
5 Correct 4 ms 3932 KB Output is correct
6 Correct 2 ms 3928 KB Output is correct
7 Correct 2 ms 3932 KB Output is correct
8 Correct 2 ms 3932 KB Output is correct
9 Correct 2 ms 3932 KB Output is correct
10 Correct 3 ms 3928 KB Output is correct
11 Correct 2 ms 3928 KB Output is correct
12 Correct 2 ms 3932 KB Output is correct
13 Correct 2 ms 3932 KB Output is correct
14 Correct 2 ms 3932 KB Output is correct
15 Correct 2 ms 3932 KB Output is correct
16 Correct 2 ms 3932 KB Output is correct
17 Correct 2 ms 3928 KB Output is correct
18 Correct 2 ms 3928 KB Output is correct
19 Correct 2 ms 3928 KB Output is correct
20 Correct 2 ms 3928 KB Output is correct
21 Correct 2 ms 4004 KB Output is correct
22 Correct 2 ms 3932 KB Output is correct
23 Correct 3 ms 3932 KB Output is correct
24 Correct 2 ms 3932 KB Output is correct
25 Correct 2 ms 3932 KB Output is correct
26 Correct 2 ms 3932 KB Output is correct
27 Correct 2 ms 4004 KB Output is correct
28 Correct 3 ms 3928 KB Output is correct
29 Correct 2 ms 3932 KB Output is correct
30 Correct 2 ms 3932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3928 KB Output is correct
2 Correct 2 ms 3932 KB Output is correct
3 Correct 2 ms 3928 KB Output is correct
4 Correct 2 ms 3932 KB Output is correct
5 Correct 2 ms 3932 KB Output is correct
6 Correct 2 ms 3992 KB Output is correct
7 Correct 2 ms 3932 KB Output is correct
8 Correct 2 ms 3996 KB Output is correct
9 Correct 1 ms 3932 KB Output is correct
10 Correct 1 ms 3768 KB Output is correct
11 Correct 2 ms 3932 KB Output is correct
12 Correct 2 ms 3932 KB Output is correct
13 Correct 2 ms 3932 KB Output is correct
14 Correct 1 ms 3932 KB Output is correct
15 Correct 2 ms 3932 KB Output is correct
16 Correct 2 ms 3932 KB Output is correct
17 Correct 2 ms 3932 KB Output is correct
18 Correct 2 ms 3928 KB Output is correct
19 Correct 2 ms 3932 KB Output is correct
20 Correct 1 ms 3932 KB Output is correct
21 Correct 2 ms 4012 KB Output is correct
22 Correct 4 ms 3932 KB Output is correct
23 Correct 2 ms 3928 KB Output is correct
24 Correct 2 ms 3932 KB Output is correct
25 Correct 2 ms 3932 KB Output is correct
26 Correct 2 ms 3932 KB Output is correct
27 Correct 3 ms 3928 KB Output is correct
28 Correct 2 ms 3928 KB Output is correct
29 Correct 2 ms 3932 KB Output is correct
30 Correct 2 ms 3932 KB Output is correct
31 Correct 2 ms 3932 KB Output is correct
32 Correct 2 ms 3932 KB Output is correct
33 Correct 2 ms 3932 KB Output is correct
34 Correct 2 ms 3928 KB Output is correct
35 Correct 2 ms 3928 KB Output is correct
36 Correct 2 ms 3928 KB Output is correct
37 Correct 2 ms 3928 KB Output is correct
38 Correct 2 ms 4004 KB Output is correct
39 Correct 2 ms 3932 KB Output is correct
40 Correct 3 ms 3932 KB Output is correct
41 Correct 2 ms 3932 KB Output is correct
42 Correct 2 ms 3932 KB Output is correct
43 Correct 2 ms 3932 KB Output is correct
44 Correct 2 ms 4004 KB Output is correct
45 Correct 3 ms 3928 KB Output is correct
46 Correct 2 ms 3932 KB Output is correct
47 Correct 2 ms 3932 KB Output is correct
48 Correct 40 ms 9420 KB Output is correct
49 Correct 46 ms 10152 KB Output is correct
50 Correct 45 ms 9940 KB Output is correct
51 Correct 35 ms 8648 KB Output is correct
52 Correct 49 ms 10192 KB Output is correct
53 Correct 40 ms 9424 KB Output is correct
54 Correct 31 ms 9680 KB Output is correct
55 Correct 63 ms 15824 KB Output is correct
56 Correct 55 ms 12744 KB Output is correct
57 Correct 64 ms 11724 KB Output is correct
58 Correct 53 ms 10964 KB Output is correct
59 Correct 43 ms 9932 KB Output is correct
60 Correct 34 ms 10068 KB Output is correct
61 Correct 34 ms 10228 KB Output is correct
62 Correct 39 ms 10456 KB Output is correct
63 Correct 36 ms 10004 KB Output is correct
64 Correct 43 ms 10196 KB Output is correct
65 Correct 34 ms 10456 KB Output is correct
66 Correct 41 ms 9968 KB Output is correct
67 Correct 27 ms 8660 KB Output is correct
68 Correct 32 ms 9420 KB Output is correct
69 Correct 42 ms 10424 KB Output is correct
70 Correct 34 ms 9928 KB Output is correct
71 Correct 24 ms 8400 KB Output is correct
72 Correct 30 ms 9168 KB Output is correct
73 Correct 38 ms 10440 KB Output is correct
74 Correct 34 ms 9928 KB Output is correct
75 Correct 47 ms 9928 KB Output is correct
76 Correct 45 ms 9928 KB Output is correct
77 Correct 40 ms 10188 KB Output is correct
78 Correct 29 ms 8140 KB Output is correct
79 Correct 31 ms 8728 KB Output is correct
80 Correct 50 ms 9928 KB Output is correct
81 Correct 42 ms 10204 KB Output is correct
82 Correct 39 ms 10188 KB Output is correct