Submission #1093042

# Submission time Handle Problem Language Result Execution time Memory
1093042 2024-09-25T18:21:14 Z Jakub_Wozniak Village (BOI20_village) C++17
0 / 100
4 ms 9820 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 DFS(int v , int o)
{
    vector <int> V[2];
    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])
        {
            V[1].push_back(p);
            res += dp[p][1]+2;
            B = 1;
        }
        else
        {
            V[0].push_back(p);
            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;
    
     
}

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;
    }

    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]});

        /*cout << t[cen][i] << ": " ;
        for(int j = 0; j < podd[t[cen][i]].size() ; j++)
        {
            cout << podd[t[cen][i]][j] << ' ';
        }
        cout << '\n';*/
    }


    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});
    }

    cout << 1 << ' ' << 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 DFS(int, int)':
Village.cpp:28:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |     for(int i = 0 ;i < t[v].size();i++)
      |                    ~~^~~~~~~~~~~~~
Village.cpp:26:8: warning: variable 'ind' set but not used [-Wunused-but-set-variable]
   26 |     ll ind = -1;
      |        ^~~
Village.cpp: In function 'void DFS2(int, int)':
Village.cpp:64:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |     for(int i = 0; i < t[v].size() ;i++)
      |                    ~~^~~~~~~~~~~~~
Village.cpp: In function 'void DFS3(int, int, int)':
Village.cpp:83:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |     for(int i = 0 ;i < t[v].size() ;i++)
      |                    ~~^~~~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:118:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  118 |     for(int i = 0 ; i < t[cen].size() ;i++)
      |                     ~~^~~~~~~~~~~~~~~
Village.cpp:173:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  173 |     for(int i = 1 ; i <= N ; i++)cout << tabmin[i] << ' '; cout << '\n';
      |     ^~~
Village.cpp:173:60: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  173 |     for(int i = 1 ; i <= N ; i++)cout << tabmin[i] << ' '; cout << '\n';
      |                                                            ^~~~
Village.cpp:174:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  174 |     for(int i = 1 ; i <= N ; i++)cout << tabmax[i] << ' '; cout << '\n';
      |     ^~~
Village.cpp:174:60: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  174 |     for(int i = 1 ; i <= N ; i++)cout << tabmax[i] << ' '; cout << '\n';
      |                                                            ^~~~
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 9816 KB Integer parameter [name=vi] equals to 0, violates the range [1, 4]
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 9820 KB Integer parameter [name=vi] equals to 0, violates the range [1, 256]
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 9816 KB Integer parameter [name=vi] equals to 0, violates the range [1, 4]
2 Halted 0 ms 0 KB -