답안 #670470

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
670470 2022-12-09T07:53:50 Z jamezzz Village (BOI20_village) C++17
75 / 100
102 ms 19532 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,mxans,deg[maxn],mnpos[maxn],mxpos[maxn],sub[maxn];
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 %d\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:101: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]
  101 |     for(int i=1;i<sz.size();++i){
      |                 ~^~~~~~~~~~
Village.cpp:104: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]
  104 |     for(int i=0;i<sz.size();++i){
      |                 ~^~~~~~~~~~
Village.cpp:40:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |     sf("%d",&n);
      |       ^
Village.cpp:42:19: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |         int a,b;sf("%d%d",&a,&b);
      |                   ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5008 KB Output is correct
3 Correct 3 ms 5008 KB Output is correct
4 Correct 3 ms 5020 KB Output is correct
5 Correct 2 ms 5008 KB Output is correct
6 Correct 3 ms 5008 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 3 ms 5004 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 5008 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5012 KB Output is correct
14 Correct 2 ms 5004 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 4 ms 4948 KB Output is correct
17 Correct 4 ms 5012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5008 KB Output is correct
2 Correct 3 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 5 ms 5044 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 5012 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 4 ms 5076 KB Output is correct
15 Correct 3 ms 5132 KB Output is correct
16 Correct 4 ms 5012 KB Output is correct
17 Correct 3 ms 5012 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 5012 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 3 ms 5076 KB Output is correct
30 Correct 3 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5008 KB Output is correct
3 Correct 3 ms 5008 KB Output is correct
4 Correct 3 ms 5020 KB Output is correct
5 Correct 2 ms 5008 KB Output is correct
6 Correct 3 ms 5008 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 3 ms 5004 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 5008 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5012 KB Output is correct
14 Correct 2 ms 5004 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 4 ms 4948 KB Output is correct
17 Correct 4 ms 5012 KB Output is correct
18 Correct 3 ms 5008 KB Output is correct
19 Correct 3 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 5 ms 5044 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 5012 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 4 ms 5076 KB Output is correct
32 Correct 3 ms 5132 KB Output is correct
33 Correct 4 ms 5012 KB Output is correct
34 Correct 3 ms 5012 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 5012 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 3 ms 5076 KB Output is correct
47 Correct 3 ms 5076 KB Output is correct
48 Correct 68 ms 11448 KB Output is correct
49 Correct 79 ms 12220 KB Output is correct
50 Correct 82 ms 12104 KB Output is correct
51 Correct 57 ms 10700 KB Output is correct
52 Correct 85 ms 12152 KB Output is correct
53 Correct 66 ms 11304 KB Output is correct
54 Correct 38 ms 12108 KB Output is correct
55 Partially correct 102 ms 19532 KB Partially correct
56 Partially correct 92 ms 15516 KB Partially correct
57 Correct 88 ms 14256 KB Output is correct
58 Correct 84 ms 13184 KB Output is correct
59 Correct 89 ms 12372 KB Output is correct
60 Correct 83 ms 16080 KB Output is correct
61 Correct 71 ms 13208 KB Output is correct
62 Correct 72 ms 12620 KB Output is correct
63 Correct 66 ms 12280 KB Output is correct
64 Correct 67 ms 12588 KB Output is correct
65 Correct 68 ms 12616 KB Output is correct
66 Correct 62 ms 12332 KB Output is correct
67 Correct 49 ms 11024 KB Output is correct
68 Correct 58 ms 11496 KB Output is correct
69 Correct 65 ms 12664 KB Output is correct
70 Correct 60 ms 12352 KB Output is correct
71 Correct 48 ms 10432 KB Output is correct
72 Correct 51 ms 11084 KB Output is correct
73 Correct 66 ms 12664 KB Output is correct
74 Correct 59 ms 12020 KB Output is correct
75 Correct 80 ms 12020 KB Output is correct
76 Correct 79 ms 11960 KB Output is correct
77 Correct 67 ms 12232 KB Output is correct
78 Correct 46 ms 10008 KB Output is correct
79 Correct 57 ms 10828 KB Output is correct
80 Correct 82 ms 12024 KB Output is correct
81 Correct 69 ms 12548 KB Output is correct
82 Correct 69 ms 12508 KB Output is correct