Submission #1093083

# Submission time Handle Problem Language Result Execution time Memory
1093083 2024-09-25T19:59:57 Z Jakub_Wozniak Village (BOI20_village) C++14
100 / 100
80 ms 50256 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define nd second
#define st first
typedef pair<ll, ll> pii;
const int maxn = (1e+5)+9;
vector <ll> t[maxn];
ll siz[maxn];
ll dep[maxn];
ll sumdep[maxn];
ll cen = -1;
ll dp[maxn][2];
vector<pii> resmin[maxn][2];
ll tabmin[maxn];
ll tabmax[maxn];

vector <int> podd[maxn];
ll N;

void rek(int v , int o , int re)
{
    set <int> V[2];  ll mini = (1e+18); bool B = 0;
    int p , ind = -1;
    for(int i = 0 ;i < t[v].size();i++) 
    {
        p = t[v][i];
        if(p == o)continue;
        if(dp[p][1]+2 <= dp[p][0])
        {
            V[1].insert(p);
            B = 1;
        }
        else
        {
            V[0].insert(p);
            if(dp[p][1]+2-dp[p][0] < mini)ind = p;
            mini = min(dp[p][1]+2-dp[p][0] , mini);
        }
    }

    if(B == 0 && re == 0)
    {
        V[0].erase(ind);
        V[1].insert(ind);
    } 

    for(auto it = V[1].begin() ; it != V[1].end() ; it++)
    {
        rek(*it,v,1);
    }
    for(auto it = V[0].begin() ; it != V[0].end() ; it++)
    {
        rek(*it,v,0);
    }

    if(re == 0 || V[1].size() == 1)V[1].insert(v);

    vector <int> vec;
    for(auto it = V[1].begin() ; it != V[1].end() ; it++)
    {
        vec.push_back(*it);
    }

    for(int i = 0 ; i < (ll)(vec.size())-1ll ;i++)
    {
        swap(tabmin[vec[i]], tabmin[vec[i+1]]);
    }
}

void DFS(int v , int o)
{
    
    ll ind = -1;
    ll p , res = 0; bool B = 0; ll mini = (1e+18);
    for(int i = 0 ;i < t[v].size();i++) 
    {
        p = t[v][i];
        if(p == o)continue;
        DFS(p,v);
        if(dp[p][1]+2 <= dp[p][0])
        {
            res += dp[p][1]+2;
            B = 1;
        }
        else
        {
            res += dp[p][0];
            if(dp[p][1]+2-dp[p][0] < mini)ind = p;
            mini = min(dp[p][1]+2-dp[p][0] , mini);
        }
    }


    dp[v][1] = res;
    if(B == 0)
    {
        res += mini;
    }          
    dp[v][0] = res;
    if(t[v].size() == 1 && v != 1)dp[v][0] = (1e+18);


    
    if(v == 1)
    {
        rek(1,-1,0);
    }
}

void DFS2(int v, int o)
{
    siz[v] = 1;
    int p , pmax = 0;
    for(int i = 0; i < t[v].size() ;i++)
    {
        p = t[v][i];
        if(p == o)continue;
        DFS2(p,v);
        pmax = max((ll)pmax,siz[p]);
        siz[v] += siz[p];
    }

    pmax = max((ll)pmax,N-siz[v]);

    if(pmax <= N/2)cen = v;     
}

void DFS3(int v ,int o , int c)
{
    podd[c].push_back(v);
    sumdep[v] = dep[v];
    int p;
    for(int i = 0 ;i < t[v].size() ;i++)
    {
        p = t[v][i];
        if(p == o)continue;
        dep[p] = dep[v]+1;
        DFS3(p,v,c);
        sumdep[v] += sumdep[p];
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll A , B;
    cin >> N;
    for(int i = 0 ;i < N-1 ; i++)
    {
        cin >> A >> B;
        t[A].push_back(B);
        t[B].push_back(A);
    }

    if(N == 2)
    {
        cout << 2 << ' ' << 2 << '\n';
        cout << 2 << ' ' << 1 << '\n';
        cout << 2 << ' ' << 1 << '\n';
        return 0;
    }

    for(int i = 1 ; i <= N ; i++)tabmin[i] = i;

    priority_queue <pii> q;
    ll ansmax = 0;
    DFS2(1,-1);
    podd[cen].push_back(cen);
    for(int i = 0 ; i < t[cen].size() ;i++)
    {
        dep[t[cen][i]] = 1;
        DFS3(t[cen][i],cen,t[cen][i]);
        ansmax += sumdep[t[cen][i]];
        q.push({podd[t[cen][i]].size(),t[cen][i]});
    }


    if(N%2)
    {
        A = q.top().nd;
        q.pop();
        B = q.top().nd;
        q.pop();

        tabmax[podd[A][podd[A].size()-1]] = cen;
        tabmax[cen] = podd[B][podd[B].size()-1];
        tabmax[podd[B][podd[B].size()-1]] = podd[A][podd[A].size()-1];
        podd[A].pop_back();
        podd[B].pop_back();

        if(podd[A].size() > 0)q.push({podd[A].size(),A});
        if(podd[B].size() > 0)q.push({podd[B].size(),B});
    }
    else
    {
        q.push({1,cen});
    }
    
    
    while(!q.empty())
    {
        A = q.top().nd;
        q.pop();
        B = q.top().nd;
        q.pop();

        tabmax[podd[A][podd[A].size()-1]] = podd[B][podd[B].size()-1];
        tabmax[podd[B][podd[B].size()-1]] = podd[A][podd[A].size()-1];
        podd[A].pop_back();
        podd[B].pop_back();

        if(podd[A].size() > 0)q.push({podd[A].size(),A});
        if(podd[B].size() > 0)q.push({podd[B].size(),B});
    }



    DFS(1,-1);


    cout << dp[1][0] << ' ' << ansmax*2 << '\n';
    for(int i = 1 ; i <= N ; i++)cout << tabmin[i] << ' '; cout << '\n';
    for(int i = 1 ; i <= N ; i++)cout << tabmax[i] << ' '; cout << '\n';



    


    return 0;
}

Compilation message

Village.cpp: In function 'void rek(int, int, int)':
Village.cpp:26:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |     for(int i = 0 ;i < t[v].size();i++)
      |                    ~~^~~~~~~~~~~~~
Village.cpp: In function 'void DFS(int, int)':
Village.cpp:77:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |     for(int i = 0 ;i < t[v].size();i++)
      |                    ~~^~~~~~~~~~~~~
Village.cpp:75:8: warning: variable 'ind' set but not used [-Wunused-but-set-variable]
   75 |     ll ind = -1;
      |        ^~~
Village.cpp: In function 'void DFS2(int, int)':
Village.cpp:116:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |     for(int i = 0; i < t[v].size() ;i++)
      |                    ~~^~~~~~~~~~~~~
Village.cpp: In function 'void DFS3(int, int, int)':
Village.cpp:135:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  135 |     for(int i = 0 ;i < t[v].size() ;i++)
      |                    ~~^~~~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:172:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  172 |     for(int i = 0 ; i < t[cen].size() ;i++)
      |                     ~~^~~~~~~~~~~~~~~
Village.cpp:225:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  225 |     for(int i = 1 ; i <= N ; i++)cout << tabmin[i] << ' '; cout << '\n';
      |     ^~~
Village.cpp:225:60: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  225 |     for(int i = 1 ; i <= N ; i++)cout << tabmin[i] << ' '; cout << '\n';
      |                                                            ^~~~
Village.cpp:226:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  226 |     for(int i = 1 ; i <= N ; i++)cout << tabmax[i] << ' '; cout << '\n';
      |     ^~~
Village.cpp:226:60: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  226 |     for(int i = 1 ; i <= N ; i++)cout << tabmax[i] << ' '; cout << '\n';
      |                                                            ^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9816 KB Output is correct
2 Correct 3 ms 11352 KB Output is correct
3 Correct 3 ms 11356 KB Output is correct
4 Correct 3 ms 11356 KB Output is correct
5 Correct 3 ms 11352 KB Output is correct
6 Correct 3 ms 11356 KB Output is correct
7 Correct 3 ms 11352 KB Output is correct
8 Correct 3 ms 11480 KB Output is correct
9 Correct 3 ms 11356 KB Output is correct
10 Correct 3 ms 11356 KB Output is correct
11 Correct 4 ms 11356 KB Output is correct
12 Correct 3 ms 11448 KB Output is correct
13 Correct 3 ms 11352 KB Output is correct
14 Correct 4 ms 11352 KB Output is correct
15 Correct 4 ms 11352 KB Output is correct
16 Correct 3 ms 11356 KB Output is correct
17 Correct 3 ms 11352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 11352 KB Output is correct
2 Correct 4 ms 11356 KB Output is correct
3 Correct 4 ms 11356 KB Output is correct
4 Correct 4 ms 11356 KB Output is correct
5 Correct 4 ms 11460 KB Output is correct
6 Correct 4 ms 11352 KB Output is correct
7 Correct 5 ms 11608 KB Output is correct
8 Correct 4 ms 11356 KB Output is correct
9 Correct 5 ms 11444 KB Output is correct
10 Correct 5 ms 11864 KB Output is correct
11 Correct 5 ms 11608 KB Output is correct
12 Correct 4 ms 11608 KB Output is correct
13 Correct 5 ms 11504 KB Output is correct
14 Correct 5 ms 11356 KB Output is correct
15 Correct 4 ms 11612 KB Output is correct
16 Correct 4 ms 11356 KB Output is correct
17 Correct 5 ms 11356 KB Output is correct
18 Correct 4 ms 11356 KB Output is correct
19 Correct 4 ms 11612 KB Output is correct
20 Correct 4 ms 11384 KB Output is correct
21 Correct 5 ms 11608 KB Output is correct
22 Correct 4 ms 11356 KB Output is correct
23 Correct 4 ms 11356 KB Output is correct
24 Correct 4 ms 11356 KB Output is correct
25 Correct 4 ms 11352 KB Output is correct
26 Correct 4 ms 11356 KB Output is correct
27 Correct 4 ms 11356 KB Output is correct
28 Correct 5 ms 11356 KB Output is correct
29 Correct 4 ms 11356 KB Output is correct
30 Correct 4 ms 11356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9816 KB Output is correct
2 Correct 3 ms 11352 KB Output is correct
3 Correct 3 ms 11356 KB Output is correct
4 Correct 3 ms 11356 KB Output is correct
5 Correct 3 ms 11352 KB Output is correct
6 Correct 3 ms 11356 KB Output is correct
7 Correct 3 ms 11352 KB Output is correct
8 Correct 3 ms 11480 KB Output is correct
9 Correct 3 ms 11356 KB Output is correct
10 Correct 3 ms 11356 KB Output is correct
11 Correct 4 ms 11356 KB Output is correct
12 Correct 3 ms 11448 KB Output is correct
13 Correct 3 ms 11352 KB Output is correct
14 Correct 4 ms 11352 KB Output is correct
15 Correct 4 ms 11352 KB Output is correct
16 Correct 3 ms 11356 KB Output is correct
17 Correct 3 ms 11352 KB Output is correct
18 Correct 4 ms 11352 KB Output is correct
19 Correct 4 ms 11356 KB Output is correct
20 Correct 4 ms 11356 KB Output is correct
21 Correct 4 ms 11356 KB Output is correct
22 Correct 4 ms 11460 KB Output is correct
23 Correct 4 ms 11352 KB Output is correct
24 Correct 5 ms 11608 KB Output is correct
25 Correct 4 ms 11356 KB Output is correct
26 Correct 5 ms 11444 KB Output is correct
27 Correct 5 ms 11864 KB Output is correct
28 Correct 5 ms 11608 KB Output is correct
29 Correct 4 ms 11608 KB Output is correct
30 Correct 5 ms 11504 KB Output is correct
31 Correct 5 ms 11356 KB Output is correct
32 Correct 4 ms 11612 KB Output is correct
33 Correct 4 ms 11356 KB Output is correct
34 Correct 5 ms 11356 KB Output is correct
35 Correct 4 ms 11356 KB Output is correct
36 Correct 4 ms 11612 KB Output is correct
37 Correct 4 ms 11384 KB Output is correct
38 Correct 5 ms 11608 KB Output is correct
39 Correct 4 ms 11356 KB Output is correct
40 Correct 4 ms 11356 KB Output is correct
41 Correct 4 ms 11356 KB Output is correct
42 Correct 4 ms 11352 KB Output is correct
43 Correct 4 ms 11356 KB Output is correct
44 Correct 4 ms 11356 KB Output is correct
45 Correct 5 ms 11356 KB Output is correct
46 Correct 4 ms 11356 KB Output is correct
47 Correct 4 ms 11356 KB Output is correct
48 Correct 62 ms 20056 KB Output is correct
49 Correct 59 ms 20684 KB Output is correct
50 Correct 68 ms 20688 KB Output is correct
51 Correct 50 ms 18772 KB Output is correct
52 Correct 54 ms 20560 KB Output is correct
53 Correct 49 ms 19680 KB Output is correct
54 Correct 39 ms 29784 KB Output is correct
55 Correct 79 ms 50256 KB Output is correct
56 Correct 69 ms 35920 KB Output is correct
57 Correct 65 ms 31120 KB Output is correct
58 Correct 64 ms 26188 KB Output is correct
59 Correct 54 ms 21068 KB Output is correct
60 Correct 80 ms 30516 KB Output is correct
61 Correct 71 ms 24572 KB Output is correct
62 Correct 52 ms 21224 KB Output is correct
63 Correct 69 ms 20804 KB Output is correct
64 Correct 53 ms 21072 KB Output is correct
65 Correct 54 ms 21196 KB Output is correct
66 Correct 58 ms 21456 KB Output is correct
67 Correct 52 ms 20652 KB Output is correct
68 Correct 43 ms 19796 KB Output is correct
69 Correct 55 ms 21460 KB Output is correct
70 Correct 49 ms 20756 KB Output is correct
71 Correct 37 ms 18324 KB Output is correct
72 Correct 55 ms 19396 KB Output is correct
73 Correct 55 ms 21452 KB Output is correct
74 Correct 47 ms 20568 KB Output is correct
75 Correct 69 ms 21196 KB Output is correct
76 Correct 53 ms 20540 KB Output is correct
77 Correct 52 ms 20940 KB Output is correct
78 Correct 36 ms 17744 KB Output is correct
79 Correct 42 ms 18908 KB Output is correct
80 Correct 67 ms 21196 KB Output is correct
81 Correct 53 ms 20984 KB Output is correct
82 Correct 56 ms 20816 KB Output is correct