이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define foreach for
#define in :
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
typedef long long ll;
/*
Konichiwa
Konichiwa
Ara ~~ ara
Bob no taisuki - Shinobu Kocho
* * * * *
* *
* *
* * I love SHINOBU <3 <3 <3
* *
* *
* * * * *
*/
/*
_________________________
Author : Bob15324
_________________________
*/
template<class X , class Y>
bool Minimize(X & x , Y y)
{
if( x == -1 || x >y )
{
x = y;
return true;
}
return false;
}
template<class X , class Y>
bool Maximize(X & x , Y y)
{
if( x < y)
{
x = y;
return true;
}
return false;
}
/* End of templates. Let's see what do we have here */
const int N = 2e5+1;
vector<pair<int ,int >>vertices[N];
int sub_size[N];
bool dead[N];
int res = -1;
void AddEdge(int u ,int v ,int w)
{
vertices[u].push_back(make_pair(v , w));
vertices[v].push_back(make_pair(u , w));
}
int DFS(int u ,int daddy)
{
sub_size[u] = 1;
foreach(pair<int ,int > adj in vertices[u])
{
int v = adj.first;
if(v == daddy)
{
continue;
}
sub_size[u] += DFS(v , u);
}
return sub_size[u];
}
int centroid(int u ,int daddy , int lim)
{
foreach(pair<int ,int > adj in vertices[u])
{
int v = adj.first;
if(v == daddy)
{
continue;
}
if(sub_size[v] > lim)
{
return centroid(v , u , lim);
}
}
return u;
}
void dfs2(int u ,int daddy ,int base , int distance , map<int ,int > &cur , int s ,int k )
{
if(cur.count(s))
{
Minimize(cur[s] , distance);
}else
{
//cout << distance <<'\n';
cur[s] = distance;
}
foreach(pair<int ,int > adj in vertices[u])
{
int v = adj.first;
if(v == daddy || dead[v])
{
continue;
}
if(s + adj.second > k)
{
continue;
}
dfs2(v , u , base , distance + 1 ,cur , s + adj.second , k);
}
}
void Build(int u, int daddy ,int k )
{
int lim = DFS(u , daddy);
int _c = centroid(u , daddy , lim >> 1);
dead[_c] = true;
map<int ,int >dict;
foreach(pair<int ,int > adj in vertices[_c])
{
int v = adj.first;
if(dead[v])
{
continue;
}
map<int ,int >cur;
dfs2(v , _c ,_c, 1 ,cur , adj.second , k );
foreach(auto val in cur)
{
if(dict.count(k - val.first))
{
Minimize(res , dict[k - val.first] + val.second);
}
}
foreach(auto val in cur)
{
if(dict.count(val.first))
{
Minimize(dict[val.first] , val.second);
}else
{
dict[val.first] = val.second;
}
}
}
if(dict.count(k))
{
foreach(auto val in dict)
{
if(val.first == k)
Minimize(res , val.second);
}
}
dict.clear();
foreach(pair<int ,int >adj in vertices[_c])
{
int v = adj.first;
if(dead[v])
{
continue;
}
Build(v , _c , k );
}
}
int best_path(int n , int m ,int H[N][2] , int L[N])
{
for(int i = 0 ; i < n -1 ; i++)
{
AddEdge(H[i][0] + 1 , H[i][1] + 1 , L[i]);
}
Build(1 , - 1 , m);
return res;
}
//Ehem. My code is amazing. Pay me 2$.Thank you.
# | 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... |