#include<bits/stdc++.h>
#include "factories.h"
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define ll long long
#define f first
#define s second
#define pii pair<ll,ll>
#define piii pair<ll,pair<ll,ll>>
#define vii vector<vector<ll>>
#define vi vector<ll>
#define cd complex<double>
#define endl '\n'
//#define multipletest
using namespace std;
const ll LIM=5e3;
const ll INF=1e18;
const string name="template";
ll n,m;
vector<pii> adj[LIM+5],adj_vt[LIM+5];
ll time_dfs=0;
pii euler_tour[100005],sparse_table[20][100005];
ll dist_X[LIM+5],dist_Y[LIM+5];
ll tin[LIM+5],tout[LIM+5],h[LIM+5],up_weight[20][LIM+5],low[20][LIM+5];
int A[LIM+5],B[LIM+5],D[LIM+5],X[LIM+5],Y[LIM+5];
vector<ll> List;
int color[LIM+5];
void dfs(ll u,ll parent,ll depth){
h[u] = depth;
tin[u] = time_dfs;
euler_tour[time_dfs++] = {depth,u};
low[0][u] = parent;
for(auto v:adj[u]){
if(v.f==parent) continue;
dfs(v.f,u,depth + 1);
euler_tour[time_dfs++] = {depth,u};
up_weight[0][v.f] = v.s;
}
tout[u] = time_dfs;
}
ll lca(ll u,ll v){
if(tin[u]>tin[v]){
swap(u,v);
}
ll bt = 63 - __builtin_clzll(tin[v]-tin[u]+1);
pii mn = min(sparse_table[bt][tin[u]],sparse_table[bt][tin[v] - (1<<bt) + 1]);
return mn.s;
}
ll lift_weight(ll u,ll k){
ll tmp=0;
ll w = 0;
while(k>0){
if(k%2==1){
w+=up_weight[tmp][u];
u=low[tmp][u];
}
k>>=1;
tmp++;
}
return w;
}
bool cmp(ll a,ll b){
return tin[a]<tin[b];
}
bool isSubtree(ll u,ll v)
{
return tin[u]<=tin[v] && tin[v]<=tout[u];
}
void dfs_to_find_sol(int u,int parent){
for(auto v:adj_vt[u]){
if(v.f==parent) continue;
dfs_to_find_sol(v.f,u);
dist_X[u] = min(dist_X[u],dist_X[v.f] + v.s);
dist_Y[u] = min(dist_Y[u],dist_Y[v.f] + v.s);
}
}
void Init(int N, int A[], int B[], int D[]){
n=N;
for(ll i=0;i<n-1;++i){
A[i]++;
B[i]++;
}
for(ll i=0;i<n-1;++i){
adj[A[i]].push_back({B[i],D[i]});
adj[B[i]].push_back({A[i],D[i]});
}
for(ll i=1;i<=n;++i){
dist_X[i]=INF;
dist_Y[i]=INF;
color[i] = -1;
}
dfs(1,0,0);
for(ll i=0;i<time_dfs;++i){
sparse_table[0][i] = euler_tour[i];
}
for(ll i=1;i<20;++i){
for(ll j=0;j+(1<<i)-1<time_dfs;++j){
sparse_table[i][j] = min(sparse_table[i-1][j],sparse_table[i-1][j + (1<<(i-1))]);
}
}
for(ll i=1;i<20;++i){
for(ll j=1;j<=n;++j){
low[i][j] =low[i-1][low[i-1][j]];
up_weight[i][j] = up_weight[i-1][j] + up_weight[i-1][low[i-1][j]];
}
}
}
long long Query(int S, int X[], int T, int Y[]){
for(auto x:List){
color[x] = -1;
adj_vt[x].clear();
dist_X[x]=dist_Y[x]=INF;
}
List.clear();
for(ll i=0;i<S;++i){
List.push_back(++X[i]);
color[X[i]] = 0;
dist_X[X[i]]=0;
dist_Y[X[i]]=INF;
}
bool chk = 0;
for(ll i=0;i<T;++i){
List.push_back(++Y[i]);
if(color[Y[i]]==0){
return 0;
}
color[Y[i]]=1;
dist_X[Y[i]]=INF;
dist_Y[Y[i]]=0;
}
sort(List.begin(),List.end(),cmp);
for(ll i=1;i<S+T;++i){
int LCA = lca(List[i-1],List[i]);
if(color[LCA]==-1){
dist_X[LCA] = dist_Y[LCA] = INF;
}
List.push_back(LCA);
}
int root;
sort(List.begin(),List.end(),cmp);
List.erase(unique(List.begin(),List.end()),List.end());
vector<ll> st;
for(ll i=0;i<List.size();++i){
ll u = List[i];
while(st.size()>=2 && !isSubtree(st.back(),u)){
ll x = st[st.size()-2];
ll y= st.back();
ll LCA = lca(x,y);
ll w=lift_weight(x,h[x] - h[LCA]) + lift_weight(y,h[y] - h[LCA]);
adj_vt[x].push_back({y,w});
adj_vt[y].push_back({x,w});
// cout<<x<<" "<<y<<" "<<w<<endl;
root=x;
st.pop_back();
}
st.push_back(u);
}
while(st.size()>=2){
ll x = st[st.size()-2];
ll y= st.back();
ll LCA = lca(x,y);
ll w=lift_weight(x,h[x] - h[LCA]) + lift_weight(y,h[y] - h[LCA]);
adj_vt[x].push_back({y,w});
adj_vt[y].push_back({x,w});
// cout<<x<<" "<<y<<" "<<w<<endl;
root=x;
st.pop_back();
}
dfs_to_find_sol(root,0);
ll ans=INF;
for(auto x:List){
ans = min(ans,dist_X[x] + dist_Y[x]);
}
return ans;
}
Compilation message
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:162:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
162 | for(ll i=0;i<List.size();++i){
| ~^~~~~~~~~~~~
factories.cpp:138:7: warning: unused variable 'chk' [-Wunused-variable]
138 | bool chk = 0;
| ^~~
factories.cpp:188:17: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
188 | dfs_to_find_sol(root,0);
| ~~~~~~~~~~~~~~~^~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
1364 KB |
Output is correct |
2 |
Correct |
727 ms |
13080 KB |
Output is correct |
3 |
Correct |
734 ms |
13120 KB |
Output is correct |
4 |
Correct |
734 ms |
13140 KB |
Output is correct |
5 |
Correct |
682 ms |
13424 KB |
Output is correct |
6 |
Correct |
425 ms |
12988 KB |
Output is correct |
7 |
Correct |
715 ms |
13272 KB |
Output is correct |
8 |
Correct |
720 ms |
13132 KB |
Output is correct |
9 |
Correct |
683 ms |
13420 KB |
Output is correct |
10 |
Correct |
448 ms |
12988 KB |
Output is correct |
11 |
Correct |
730 ms |
13060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1236 KB |
Output is correct |
2 |
Runtime error |
219 ms |
23040 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
1364 KB |
Output is correct |
2 |
Correct |
727 ms |
13080 KB |
Output is correct |
3 |
Correct |
734 ms |
13120 KB |
Output is correct |
4 |
Correct |
734 ms |
13140 KB |
Output is correct |
5 |
Correct |
682 ms |
13424 KB |
Output is correct |
6 |
Correct |
425 ms |
12988 KB |
Output is correct |
7 |
Correct |
715 ms |
13272 KB |
Output is correct |
8 |
Correct |
720 ms |
13132 KB |
Output is correct |
9 |
Correct |
683 ms |
13420 KB |
Output is correct |
10 |
Correct |
448 ms |
12988 KB |
Output is correct |
11 |
Correct |
730 ms |
13060 KB |
Output is correct |
12 |
Correct |
2 ms |
1236 KB |
Output is correct |
13 |
Runtime error |
219 ms |
23040 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |