# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
148166 | emaborevkovic | Torrent (COI16_torrent) | C++14 | 193 ms | 35248 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
#define pb push_back
#define trace(x) cerr << #x << " " << x << endl
const int N = 3*1e5+11;
int n, a, b;
vector <int> ls[N];
int boje[N];
int nasao;
int d[N];
vector <int> tr[N];
int bio[N];
void dfs (int x, int p) {
if (x == b) {
nasao = 1;
boje[x] = 1;
return;
}
for (int i=0;i<ls[x].size();i++) {
if (ls[x][i] == p) continue;
dfs (ls[x][i], x);
if (nasao) {
boje[x] = 1;
return;
}
}
}
int prekomp (int x, int p) {
if (ls[x].size() == 1) return d[x] = 0;
vector <int> v;
for (int i=0;i<ls[x].size();i++) {
if (ls[x][i] == p) continue;
v.pb(prekomp(ls[x][i], x));
}
sort(v.begin(), v.end());
int dod = v.size();
int ret = 0;
for (int i=0;i<v.size();i++) {
ret = max(v[i]+dod, ret);
dod--;
}
return d[x] = ret;
}
void func (int x, int kol) {
int kad = tr[x].size();
if (kol < 0) return;
for (int i=tr[x].size()-1;i>=0;i--) {
if (tr[x][i] > kol) return;
if (tr[x][i] == kol) kad = i;
}
kad = tr[x].size()-kad;
bio[x] = 1;
for (int i=0;i<ls[x].size();i++) {
if (boje[ls[x][i]] && !bio[ls[x][i]]) {
func (ls[x][i], kol-kad-1);
break;
}
}
}
bool prov (int x) {
memset (bio, 0, sizeof bio);
func (a, x);
func (b, x);
for (int i=0;i<n;i++) {
if (boje[i] && !bio[i]) return 0;
}
return 1;
}
int main() {
cin >> n >> a >> b;
a--; b--;
for (int i=0;i<n-1;i++) {
int a1, a2;
scanf("%d%d", &a1, &a2);
a1--; a2--;
ls[a1].pb(a2);
ls[a2].pb(a1);
}
dfs (a, a);
for (int i=0;i<n;i++) {
if (boje[i]) {
for (int j=0;j<ls[i].size();j++) {
if (boje[ls[i][j]]) continue;
prekomp (ls[i][j], i);
tr[i].pb(d[ls[i][j]]);
}
sort(tr[i].begin(), tr[i].end());
int dod = tr[i].size();
for (int j=0;j<tr[i].size();j++) {
tr[i][j] += dod;
dod--;
}
}
}
int lo = 0;
int hi = N;
while (lo < hi) {
int mid = (lo+hi)/2;
if (prov(mid)) hi = mid;
else lo = mid+1;
}
cout << lo;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |