답안 #1093073

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1093073 2024-09-25T19:40:55 Z Jakub_Wozniak Village (BOI20_village) C++14
50 / 100
101 ms 51380 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].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 < vec.size() ;i++)
    {
        tabmin[vec[i]] = vec[(i+1)%vec.size()];
    }
}

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

    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:66:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |     for(int i = 0 ; i < vec.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:115:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  115 |     for(int i = 0; i < t[v].size() ;i++)
      |                    ~~^~~~~~~~~~~~~
Village.cpp: In function 'void DFS3(int, int, int)':
Village.cpp:134:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  134 |     for(int i = 0 ;i < t[v].size() ;i++)
      |                    ~~^~~~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:169:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  169 |     for(int i = 0 ; i < t[cen].size() ;i++)
      |                     ~~^~~~~~~~~~~~~~~
Village.cpp:222:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  222 |     for(int i = 1 ; i <= N ; i++)cout << tabmin[i] << ' '; cout << '\n';
      |     ^~~
Village.cpp:222:60: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  222 |     for(int i = 1 ; i <= N ; i++)cout << tabmin[i] << ' '; cout << '\n';
      |                                                            ^~~~
Village.cpp:223:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  223 |     for(int i = 1 ; i <= N ; i++)cout << tabmax[i] << ' '; cout << '\n';
      |     ^~~
Village.cpp:223:60: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  223 |     for(int i = 1 ; i <= N ; i++)cout << tabmax[i] << ' '; cout << '\n';
      |                                                            ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9820 KB Output is correct
2 Partially correct 4 ms 9816 KB Partially correct
3 Correct 4 ms 9820 KB Output is correct
4 Correct 4 ms 9824 KB Output is correct
5 Correct 4 ms 9820 KB Output is correct
6 Correct 4 ms 9820 KB Output is correct
7 Correct 4 ms 9816 KB Output is correct
8 Correct 4 ms 9820 KB Output is correct
9 Correct 3 ms 9820 KB Output is correct
10 Correct 3 ms 9820 KB Output is correct
11 Correct 4 ms 9760 KB Output is correct
12 Correct 4 ms 9820 KB Output is correct
13 Correct 3 ms 9820 KB Output is correct
14 Correct 4 ms 9820 KB Output is correct
15 Correct 3 ms 9908 KB Output is correct
16 Correct 4 ms 9820 KB Output is correct
17 Correct 3 ms 9912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 5 ms 9820 KB Output is correct
3 Correct 5 ms 9816 KB Output is correct
4 Partially correct 4 ms 9820 KB Partially correct
5 Correct 5 ms 9820 KB Output is correct
6 Correct 4 ms 9820 KB Output is correct
7 Correct 4 ms 10076 KB Output is correct
8 Correct 4 ms 9820 KB Output is correct
9 Correct 4 ms 9820 KB Output is correct
10 Correct 4 ms 10076 KB Output is correct
11 Partially correct 4 ms 10076 KB Partially correct
12 Correct 4 ms 10072 KB Output is correct
13 Correct 4 ms 10076 KB Output is correct
14 Correct 4 ms 9820 KB Output is correct
15 Correct 4 ms 9820 KB Output is correct
16 Correct 4 ms 9820 KB Output is correct
17 Correct 4 ms 9820 KB Output is correct
18 Correct 4 ms 9820 KB Output is correct
19 Correct 5 ms 10076 KB Output is correct
20 Correct 5 ms 9820 KB Output is correct
21 Correct 4 ms 9820 KB Output is correct
22 Correct 4 ms 9820 KB Output is correct
23 Correct 4 ms 9816 KB Output is correct
24 Correct 4 ms 9820 KB Output is correct
25 Correct 4 ms 9836 KB Output is correct
26 Correct 5 ms 10072 KB Output is correct
27 Correct 4 ms 9820 KB Output is correct
28 Correct 4 ms 10044 KB Output is correct
29 Correct 5 ms 9820 KB Output is correct
30 Correct 4 ms 9820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9820 KB Output is correct
2 Partially correct 4 ms 9816 KB Partially correct
3 Correct 4 ms 9820 KB Output is correct
4 Correct 4 ms 9824 KB Output is correct
5 Correct 4 ms 9820 KB Output is correct
6 Correct 4 ms 9820 KB Output is correct
7 Correct 4 ms 9816 KB Output is correct
8 Correct 4 ms 9820 KB Output is correct
9 Correct 3 ms 9820 KB Output is correct
10 Correct 3 ms 9820 KB Output is correct
11 Correct 4 ms 9760 KB Output is correct
12 Correct 4 ms 9820 KB Output is correct
13 Correct 3 ms 9820 KB Output is correct
14 Correct 4 ms 9820 KB Output is correct
15 Correct 3 ms 9908 KB Output is correct
16 Correct 4 ms 9820 KB Output is correct
17 Correct 3 ms 9912 KB Output is correct
18 Correct 4 ms 9816 KB Output is correct
19 Correct 5 ms 9820 KB Output is correct
20 Correct 5 ms 9816 KB Output is correct
21 Partially correct 4 ms 9820 KB Partially correct
22 Correct 5 ms 9820 KB Output is correct
23 Correct 4 ms 9820 KB Output is correct
24 Correct 4 ms 10076 KB Output is correct
25 Correct 4 ms 9820 KB Output is correct
26 Correct 4 ms 9820 KB Output is correct
27 Correct 4 ms 10076 KB Output is correct
28 Partially correct 4 ms 10076 KB Partially correct
29 Correct 4 ms 10072 KB Output is correct
30 Correct 4 ms 10076 KB Output is correct
31 Correct 4 ms 9820 KB Output is correct
32 Correct 4 ms 9820 KB Output is correct
33 Correct 4 ms 9820 KB Output is correct
34 Correct 4 ms 9820 KB Output is correct
35 Correct 4 ms 9820 KB Output is correct
36 Correct 5 ms 10076 KB Output is correct
37 Correct 5 ms 9820 KB Output is correct
38 Correct 4 ms 9820 KB Output is correct
39 Correct 4 ms 9820 KB Output is correct
40 Correct 4 ms 9816 KB Output is correct
41 Correct 4 ms 9820 KB Output is correct
42 Correct 4 ms 9836 KB Output is correct
43 Correct 5 ms 10072 KB Output is correct
44 Correct 4 ms 9820 KB Output is correct
45 Correct 4 ms 10044 KB Output is correct
46 Correct 5 ms 9820 KB Output is correct
47 Correct 4 ms 9820 KB Output is correct
48 Correct 60 ms 20840 KB Output is correct
49 Correct 59 ms 21864 KB Output is correct
50 Correct 60 ms 21800 KB Output is correct
51 Correct 46 ms 19232 KB Output is correct
52 Correct 58 ms 21840 KB Output is correct
53 Correct 66 ms 20720 KB Output is correct
54 Correct 45 ms 29452 KB Output is correct
55 Correct 89 ms 51380 KB Output is correct
56 Partially correct 101 ms 37020 KB Partially correct
57 Correct 71 ms 32336 KB Output is correct
58 Correct 73 ms 27304 KB Output is correct
59 Correct 72 ms 22352 KB Output is correct
60 Correct 90 ms 31792 KB Output is correct
61 Correct 69 ms 25828 KB Output is correct
62 Correct 58 ms 22248 KB Output is correct
63 Correct 52 ms 21828 KB Output is correct
64 Correct 51 ms 22352 KB Output is correct
65 Correct 51 ms 22480 KB Output is correct
66 Correct 52 ms 22228 KB Output is correct
67 Correct 48 ms 20900 KB Output is correct
68 Correct 42 ms 20384 KB Output is correct
69 Correct 53 ms 22484 KB Output is correct
70 Correct 47 ms 21840 KB Output is correct
71 Correct 36 ms 18772 KB Output is correct
72 Correct 42 ms 19792 KB Output is correct
73 Correct 57 ms 22404 KB Output is correct
74 Correct 50 ms 21332 KB Output is correct
75 Correct 58 ms 22220 KB Output is correct
76 Correct 53 ms 21748 KB Output is correct
77 Correct 53 ms 21968 KB Output is correct
78 Correct 35 ms 18012 KB Output is correct
79 Correct 41 ms 19536 KB Output is correct
80 Correct 57 ms 22224 KB Output is correct
81 Correct 51 ms 22096 KB Output is correct
82 Correct 55 ms 21888 KB Output is correct