답안 #661094

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
661094 2022-11-24T10:30:50 Z ktkerem Village (BOI20_village) C++17
100 / 100
92 ms 16396 KB
/*#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")/**/
#include<bits/stdc++.h>
/**/
//typedef int ll;
typedef long long ll;
typedef unsigned long long ull;
typedef std::string str;
/*typedef __int128 vll;
typedef unsigned __int128 uvll;*/
#define llll std::pair<ll , ll>
#define pb push_back
#define pf push_front
#define halo cout << "hello\n"
#define fi first
#define sec second
#define all(a) a.begin() , a.end()
const ll limit = 1e14 + 7; 
const ll ous = 1e5 + 7;
const ll dx[4] = {-1 , 0 , 1 , 0} , dy[4] = {0,1,0,-1};
ll n , m;std::vector<ll> ar(ous) , mnvr(ous) , mxvr(ous);
ll mna , mxa , ct;
std::vector<ll> adj[ous];
std::vector<ll> v;
bool cmp(ll x , ll y){
    return ar[x] > ar[y];
}
void jd(ll crt , ll prv){
    v.pb(crt);
    for(auto j:adj[crt]){
        if(j != prv){
            jd(j , crt);
        }
    }
}
void klp(ll crt){
    std::sort(all(adj[crt]) , cmp);
    for(auto j:adj[crt]){
        jd(j , crt);
    }
    v.pb(crt);
    ll s = ar[v[0]];
    for(ll i = s;v.size()>i;i++){
        mxvr[v[i - s]] = v[i];
    }
    for(ll i=0;s>i;i++){
        mxvr[v[n - s + i]] = v[i];
    }
}
void mdfs(ll crt , ll prv){
    for(auto j:adj[crt]){
        if(j != prv){
            mdfs(j , crt);
        }
    }
    if(crt == mnvr[crt]){
        mna+=2;
        if(prv == -1){
            ll tmp = mnvr[crt];
            mnvr[crt] = mnvr[adj[crt][0]];
            mnvr[adj[crt][0]] = tmp;
        }
        else{
            ll tmp = mnvr[crt];
            mnvr[crt] = mnvr[prv];
            mnvr[prv] = tmp;
        }
    }
}

ll fndcnt(ll crt , ll prv){
    ll z = 1 , mxx = 1;
    for(auto j:adj[crt]){
        if(j != prv){
            ll x = fndcnt(j , crt);
            z+=x;
            mxx = std::max(mxx , x);
        }
    }
    if(n - z <= n/2 && n/2 >= mxx){
        ct = crt;
    }
    return z;
}
ll fndsub(ll crt , ll prv){
    ll z = 1;
    for(auto j:adj[crt]){
        if(j != prv){
            ll x = fndsub(j , crt);
            z+=x;
        }
    }
    if(prv != -1){
        mxa += std::min(z , n - z) * 2;
    }
    ar[crt] = z;
    return z;
}
void solve(){
    std::cin >> n;
    for(ll i = 1;n>i;i++){
        ll x , y;std::cin >> x >> y;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    for(ll i = 0;n>=i;i++){
        mnvr[i] = i;
        mxvr[i] = i;
    }
    mdfs(1 , -1);
    fndcnt(1 , -1);
    fndsub(ct , -1);
    klp(ct);
    std::cout << mna << " " << mxa << "\n";
    for(ll i = 1;n>=i;i++){
        std::cout << mnvr[i] << " ";
    }
    std::cout << "\n";
    for(ll i = 1;n>=i;i++){
        std::cout << mxvr[i] << " ";   
    }
    std::cout << "\n";
    return;/**/
}
signed main(){
    std::ios_base::sync_with_stdio(false);std::cin.tie(NULL);
    ll t=1;
    //std::cin >> t;
    ll o = 1;
    while(t--){ 
        //cout << "Case " << o++ << ":\n";
        solve();
    }
    return 0;
}/**/

Compilation message

Village.cpp:5:78: warning: "/*" within comment [-Wcomment]
    5 | #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")/**/
      |                                                                               
Village.cpp: In function 'void klp(ll)':
Village.cpp:46:26: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   46 |     for(ll i = s;v.size()>i;i++){
      |                  ~~~~~~~~^~
Village.cpp: In function 'int main()':
Village.cpp:132:8: warning: unused variable 'o' [-Wunused-variable]
  132 |     ll o = 1;
      |        ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 2 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 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 3 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 2 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 2 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5072 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 4 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 2 ms 5076 KB Output is correct
14 Correct 3 ms 5092 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 4 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 2 ms 5068 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 2 ms 5120 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 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 2 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 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 3 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 2 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 2 ms 4948 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 5072 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 4 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 2 ms 5076 KB Output is correct
31 Correct 3 ms 5092 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 4 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 2 ms 5068 KB Output is correct
43 Correct 3 ms 5076 KB Output is correct
44 Correct 3 ms 5076 KB Output is correct
45 Correct 3 ms 5076 KB Output is correct
46 Correct 2 ms 5120 KB Output is correct
47 Correct 3 ms 5076 KB Output is correct
48 Correct 76 ms 10380 KB Output is correct
49 Correct 60 ms 10848 KB Output is correct
50 Correct 65 ms 10936 KB Output is correct
51 Correct 47 ms 9544 KB Output is correct
52 Correct 62 ms 10716 KB Output is correct
53 Correct 65 ms 10252 KB Output is correct
54 Correct 31 ms 10484 KB Output is correct
55 Correct 81 ms 16396 KB Output is correct
56 Correct 77 ms 13572 KB Output is correct
57 Correct 75 ms 12536 KB Output is correct
58 Correct 92 ms 11856 KB Output is correct
59 Correct 61 ms 10948 KB Output is correct
60 Correct 45 ms 10988 KB Output is correct
61 Correct 50 ms 10936 KB Output is correct
62 Correct 49 ms 11200 KB Output is correct
63 Correct 45 ms 10816 KB Output is correct
64 Correct 59 ms 11208 KB Output is correct
65 Correct 48 ms 11328 KB Output is correct
66 Correct 44 ms 10852 KB Output is correct
67 Correct 34 ms 9536 KB Output is correct
68 Correct 42 ms 10304 KB Output is correct
69 Correct 54 ms 11424 KB Output is correct
70 Correct 42 ms 10944 KB Output is correct
71 Correct 36 ms 9348 KB Output is correct
72 Correct 38 ms 10012 KB Output is correct
73 Correct 56 ms 11464 KB Output is correct
74 Correct 43 ms 10856 KB Output is correct
75 Correct 65 ms 11332 KB Output is correct
76 Correct 57 ms 10724 KB Output is correct
77 Correct 54 ms 11200 KB Output is correct
78 Correct 36 ms 9088 KB Output is correct
79 Correct 42 ms 9652 KB Output is correct
80 Correct 67 ms 11280 KB Output is correct
81 Correct 51 ms 11464 KB Output is correct
82 Correct 49 ms 10952 KB Output is correct