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<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>E[100010], L[100010];
vector<int>P[100010];
struct Seg{
int L, num;
bool operator < (const Seg &p)const{
return L < p.L;
}
};
vector<Seg>w[100010];
int n, m, D[100010], L2[100010], Q[100010], par[100010], sz;
bool chk[100010], House[100010];
long long K;
void BFS(int a){
int h = 0, t = 0, i, x;
Q[++t] = a, D[a] = 1, L2[a] = 0;
while (h < t){
x = Q[++h];
for (i = 0; i < E[x].size(); i++){
if (!D[E[x][i]] && !chk[E[x][i]]){
D[E[x][i]] = 1;
Q[++t] = E[x][i];
L2[E[x][i]] = L2[x] + L[x][i];
par[E[x][i]] = x;
}
}
}
sz = t;
}
int GetMid(int a){
int i, x;
BFS(a);
for (i = sz; i >= 1; i--){
x = Q[i];
D[par[x]] += D[x];
if (D[x] >= sz / 2){
break;
}
}
for (i = 1; i <= sz; i++)D[Q[i]] = 0;
return x;
}
void Do(int a)
{
int Mid = GetMid(a);
if (sz == 1){
if (House[a])P[a].push_back(0);
return;
}
int i, x, j;
Seg tp;
chk[Mid] = true;
for (i = 0; i < E[Mid].size(); i++){
x = E[Mid][i];
if (!chk[x]){
Do(x);
for (j = 0; j < P[x].size(); j++){
tp.L = P[x][j] + L[Mid][i];
tp.num = x;
w[Mid].push_back(tp);
}
P[x].resize(0);
}
}
if (House[Mid]){
tp.L = 0, tp.num = Mid;
w[Mid].push_back(tp);
}
if (!w[Mid].empty()){
sort(w[Mid].begin(), w[Mid].end());
}
chk[Mid] = false;
BFS(a);
for (i = 1; i <= sz; i++){
D[Q[i]] = 0;
if (House[Q[i]])P[a].push_back(L2[Q[i]]);
}
}
int cnt[100010];
long long Gap(int Len)
{
int i, pv, j, sz;
long long S = 0;
for (i = 1; i <= n; i++){
if (w[i].empty())continue;
pv = -1;
sz = w[i].size();
for (j = sz - 1; j >= 0; j--){
while (pv + 1 < sz && w[i][pv + 1].L + w[i][j].L <= Len){
pv++;
cnt[w[i][pv].num]++;
}
S += pv + 1 - cnt[w[i][j].num];
}
for (j = 0; j <= pv; j++)cnt[w[i][j].num]--;
}
return S;
}
int main(){
int i, a, b, be, ed, mid, Res;
scanf("%d%d%lld", &n, &m, &K);
for (i = 2; i <= n; i++){
scanf("%d%d", &a, &b);
E[a].push_back(i);
E[i].push_back(a);
L[a].push_back(b);
L[i].push_back(b);
}
for (i = 0; i < m; i++){
scanf("%d", &a);
House[a] = true;
}
Do(1);
be = 1, ed = 250 * (n - 1);
while (be <= ed){
mid = (be + ed) >> 1;
if (Gap(mid) >= K * 2){
Res = mid;
ed = mid - 1;
}
else{
be = mid + 1;
}
}
printf("%d\n", Res);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |