Submission #366437

# Submission time Handle Problem Language Result Execution time Memory
366437 2021-02-14T08:01:16 Z Vimmer Papričice (COCI20_papricice) C++14
0 / 110
12 ms 14572 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

//#pragma GCC optimize("-O3")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-Ofast")

#define N 200500
#define NN 205000
#define PB push_back
#define endl '\n'
#define pri(x) cout << x << endl
#define _ << " " <<
#define all(x) x.begin(), x.end()
#define sz(x) int(x.size())
#define M ll(1e9 + 7)
#define F first
#define S second

using namespace std;
//using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef short int si;
typedef unsigned long long ull;

//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

vector <int> g[N];

int ans = 1e9, siz[N], n;

set <pair <int, int> > se[N];

void dfs(int v, int p)
{
    siz[v] = 1;

    se[v].clear();

    for (auto it : g[v])
    {
        if (it == p) continue;

        dfs(it, v);

        siz[v] += siz[it];

        if (sz(se[it]) > sz(se[v]))
            swap(se[it], se[v]);

        for (auto itr : se[it])
          se[v].insert(itr);
    }

    int val = (siz[v] + 1) / 2;

    int kol = 100;

    auto it = se[v].lower_bound({val, -1});

    while (kol)
    {
        int vt = (*it).S;

        int a1 = n - siz[v], a2 = siz[v] - siz[vt], a3 = siz[vt];

        ans = min(ans, max(a1, max(a2, a3)) - min(a1, min(a2, a3)));

        kol--;

        if (it == se[v].begin())
            break;

        it--;
    }

    se[v].insert({siz[v], v});
}

int main()
{
    ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    freopen("1.in", "r", stdin);

    cin >> n;

    for (int i = 1; i < n; i++)
    {
        int x, y;

        cin >> x >> y;

        g[x].PB(y);

        g[y].PB(x);
    }

    for (int i = 1; i <= n; i++)
      dfs(i, -1);

    pri(ans);
}

Compilation message

papricice.cpp: In function 'int main()':
papricice.cpp:87:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   87 |     freopen("1.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 12 ms 14572 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 12 ms 14572 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 12 ms 14572 KB Output isn't correct
2 Halted 0 ms 0 KB -