| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 375899 | topovik | Papričice (COCI20_papricice) | C++14 | 345 ms | 21740 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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, mx;
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);
mx = not_anc.end();
if (mn != not_anc.begin() && !not_anc.empty()) {mx = --mn; mn++;}
if (mn != not_anc.end()) update(sz[x], *mn, n - sz[x] - *mn);
if (mx != not_anc.end()) update(sz[x], *mx, n - sz[x] - *mx);
}
}
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;
}
컴파일 시 표준 에러 (stderr) 메시지
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
