Submission #375900

# Submission time Handle Problem Language Result Execution time Memory
375900 2021-03-10T08:33:10 Z topovik Papričice (COCI20_papricice) C++14
110 / 110
366 ms 19316 KB
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define len(x) x.size()
#define debug(x) cout << #x << " is " << x << endl

using namespace std;

typedef long long ll;
typedef long double ld;

const ll N = 1e6 + 100;
const ll oo = 1e9 + 7;

set <int> anc;
set <int> not_anc;
set<int> :: iterator mn;

vector <vector <int> > a;

int sz[N];
int n;
int ans = oo;

int calc_sz(int x, int y)
{
    sz[x] = 1;
    for (int i = 0; i < len(a[x]); i++)
        if (a[x][i] != y) sz[x] += calc_sz(a[x][i], x);
    return sz[x];
}

void update(int x, int y, int z)
{
    ans = min(ans, max(max(x, y), z) - min(min(x, y), z));
}

void Check(int x)
{
    if (!anc.empty())
    {
        mn = anc.lower_bound((n + sz[x]) / 2);
        if (mn != anc.end()) update(sz[x], *mn - sz[x], n - *mn);
        if (mn != anc.begin())
        {
            mn--;
            update(sz[x], *mn - sz[x], n - *mn);
        }
    }
    if (!not_anc.empty())
    {
        mn = not_anc.lower_bound((n - sz[x]) / 2);
        if (mn != not_anc.end()) update(sz[x], *mn, n - sz[x] - *mn);
        if (mn != not_anc.begin())
        {
            mn--;
            update(sz[x], *mn, n - sz[x] - *mn);
        }
    }
}

void Rec(int x, int y)
{
    Check(x);
    anc.insert(sz[x]);
    for (int i = 0; i < len(a[x]); i++)
        if (a[x][i] != y) Rec(a[x][i], x);
    not_anc.insert(sz[x]);
    anc.erase(sz[x]);
}

int main()
{
    cin >> n;
    a.resize(n);
    for (int i = 0; i < n - 1; i++)
    {
        int x, y;
        cin >> x >> y;
        a[--x].pb(--y);
        a[y].pb(x);
    }
    calc_sz(0, 0);
    Rec(0, 0);
    cout << ans;
}

Compilation message

papricice.cpp: In function 'int calc_sz(int, int)':
papricice.cpp:29:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     for (int i = 0; i < len(a[x]); i++)
      |                       ^
papricice.cpp: In function 'void Rec(int, int)':
papricice.cpp:67:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |     for (int i = 0; i < len(a[x]); i++)
      |                       ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 2 ms 492 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 2 ms 492 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 311 ms 12268 KB Output is correct
12 Correct 366 ms 12320 KB Output is correct
13 Correct 287 ms 12908 KB Output is correct
14 Correct 328 ms 12524 KB Output is correct
15 Correct 335 ms 12268 KB Output is correct
16 Correct 207 ms 12904 KB Output is correct
17 Correct 309 ms 12268 KB Output is correct
18 Correct 311 ms 19316 KB Output is correct
19 Correct 296 ms 12396 KB Output is correct
20 Correct 313 ms 12268 KB Output is correct