답안 #670471

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
670471 2022-12-09T07:54:54 Z jamezzz Village (BOI20_village) C++17
100 / 100
92 ms 19020 KB
#include <bits/stdc++.h>
using namespace std;
 
#define fi first
#define se second
#define sf scanf
#define pf printf
#define pb push_back
#define all(x) x.begin(),x.end()
typedef pair<int,int> ii;
typedef tuple<int,int,int> iii;
 
#define maxn 100005
 
int n,mnans,deg[maxn],mnpos[maxn],mxpos[maxn],sub[maxn];
long long mxans;
vector<int> AL[maxn];
vector<int> stuff[maxn];

void dfs(int u,int p){
    sub[u]=1;
    for(int v:AL[u]){
        if(v==p)continue;
        dfs(v,u);
        sub[u]+=sub[v];
    }
}

void dfs2(int u,int p,int p2){
    if(p2==-1){
        stuff[u].pb(u);
    }
    else stuff[p2].pb(u);
    for(int v:AL[u]){
        if(v==p)continue;
        dfs2(v,u,(p2==-1)?v:p2);
    }
}

int main(){
    sf("%d",&n);
    for(int i=1;i<n;++i){
        int a,b;sf("%d%d",&a,&b);
        AL[a].pb(b);
        AL[b].pb(a);
        ++deg[a],++deg[b];
    }
    queue<int> q;
    for(int i=1;i<=n;++i){
        mnpos[i]=i;
        if(deg[i]==1)q.push(i);
    }
    while(!q.empty()){
        int u=q.front();q.pop();
        if(mnpos[u]==u){
            int p=-1;
            for(int v:AL[u]){
                if(deg[v]!=0){
                    p=v;
                    break;
                }
            }
            if(p==-1){
                swap(mnpos[AL[u][0]], mnpos[u]);
            }
            else{
                swap(mnpos[p],mnpos[u]);
            }
            mnans+=2;
        }
        for(int v:AL[u]){
            if(deg[v]!=0){
                --deg[v],--deg[u];
                if(deg[v]<=1)q.push(v);
            }
        }
    }
    dfs(1,-1);
    int c=1;
    for(int i=1;i<=n;++i){
        if(i!=1){
            mxans+=2*min(sub[i],n-sub[i]);
        }
        bool pos=true;
        for(int v:AL[i]){
            if(sub[v]<sub[i]&&sub[v]>n/2){
                pos=false;
            }
        }
        if(n-sub[i]>n/2)pos=false;
        if(pos){
            c=i;
        }
    }
    dfs2(c,-1,-1);
    vector<ii> sz;
    for(int i=1;i<=n;++i){
        if(stuff[i].size()!=0)sz.pb({stuff[i].size(),i});
    }
    sort(all(sz),greater<ii>());
    queue<int> to;
    for(int i=1;i<sz.size();++i){
        for(int x:stuff[sz[i].se])to.push(x);
    }
    for(int i=0;i<sz.size();++i){
        for(int x:stuff[sz[i].se]){
            int y=to.front();to.pop();
            mxpos[x]=y;
            to.push(x);
        }
    }
    pf("%d %lld\n",mnans,mxans);
    for(int i=1;i<=n;++i){
        pf("%d ",mnpos[i]);
    }
    pf("\n");
    for(int i=1;i<=n;++i){
        pf("%d ",mxpos[i]);
    }
    pf("\n");
}

Compilation message

Village.cpp: In function 'int main()':
Village.cpp:102:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |     for(int i=1;i<sz.size();++i){
      |                 ~^~~~~~~~~~
Village.cpp:105:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |     for(int i=0;i<sz.size();++i){
      |                 ~^~~~~~~~~~
Village.cpp:41:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |     sf("%d",&n);
      |       ^
Village.cpp:43:19: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   43 |         int a,b;sf("%d%d",&a,&b);
      |                   ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5048 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 4948 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 3 ms 4948 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 4 ms 5076 KB Output is correct
30 Correct 4 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 5048 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 3 ms 5076 KB Output is correct
30 Correct 3 ms 5076 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 3 ms 5076 KB Output is correct
33 Correct 3 ms 5076 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 3 ms 5076 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 3 ms 5076 KB Output is correct
40 Correct 3 ms 5076 KB Output is correct
41 Correct 3 ms 5076 KB Output is correct
42 Correct 3 ms 4948 KB Output is correct
43 Correct 3 ms 5076 KB Output is correct
44 Correct 3 ms 4948 KB Output is correct
45 Correct 3 ms 5076 KB Output is correct
46 Correct 4 ms 5076 KB Output is correct
47 Correct 4 ms 5076 KB Output is correct
48 Correct 64 ms 11000 KB Output is correct
49 Correct 72 ms 11724 KB Output is correct
50 Correct 73 ms 11636 KB Output is correct
51 Correct 55 ms 10260 KB Output is correct
52 Correct 71 ms 11680 KB Output is correct
53 Correct 69 ms 10940 KB Output is correct
54 Correct 37 ms 11852 KB Output is correct
55 Correct 91 ms 19020 KB Output is correct
56 Correct 92 ms 15068 KB Output is correct
57 Correct 91 ms 13800 KB Output is correct
58 Correct 79 ms 12704 KB Output is correct
59 Correct 72 ms 11768 KB Output is correct
60 Correct 70 ms 15544 KB Output is correct
61 Correct 62 ms 12744 KB Output is correct
62 Correct 60 ms 12064 KB Output is correct
63 Correct 56 ms 11704 KB Output is correct
64 Correct 65 ms 12216 KB Output is correct
65 Correct 60 ms 12152 KB Output is correct
66 Correct 59 ms 11852 KB Output is correct
67 Correct 44 ms 10548 KB Output is correct
68 Correct 59 ms 11144 KB Output is correct
69 Correct 60 ms 12212 KB Output is correct
70 Correct 59 ms 11836 KB Output is correct
71 Correct 42 ms 9960 KB Output is correct
72 Correct 46 ms 10700 KB Output is correct
73 Correct 60 ms 12324 KB Output is correct
74 Correct 55 ms 11632 KB Output is correct
75 Correct 70 ms 11456 KB Output is correct
76 Correct 78 ms 11492 KB Output is correct
77 Correct 63 ms 11780 KB Output is correct
78 Correct 41 ms 9548 KB Output is correct
79 Correct 48 ms 10276 KB Output is correct
80 Correct 73 ms 11584 KB Output is correct
81 Correct 62 ms 12068 KB Output is correct
82 Correct 58 ms 11980 KB Output is correct