#include "factories.h"
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const ll maxN=2e6+10;
#define taskname "codeforce"
ll dp1[maxN],dp2[maxN],mn1[maxN],mn2[maxN];
const ll inf=1e15;
#define pli pair<ll,ll>
#define fi first
#define se second
vector<pli>g[maxN];
vector<pli>c[maxN];
ll vt[maxN];
void dfs1(ll u,ll p)
{
dp1[u]=inf;
mn1[u]=mn2[u]=inf;
for(auto vv:c[u])
{
ll v=vv.fi;
ll w=vv.se;
//if(v==p) {continue;}
dfs1(v,u);
dp1[u]=min(dp1[v]+w,dp1[u]);
if(dp1[v]+w<=mn1[u])
{
mn2[u]=mn1[u];
mn1[u]=dp1[v]+w;
}
else if(dp1[v]+w<=mn2[u])
{
mn2[u]=dp1[v]+w;
}
}
if(vt[u]>=2) dp1[u]=0;
}
void dfs2(ll u,ll p,ll w)
{
dp2[u]=inf;
if(vt[u]>=2) dp2[u]=0;
else
{
if(p!=-1)
{
if(vt[p]>=2) dp2[u]=w;
else
{
if(dp1[u]+w==mn1[p]) dp2[u]=mn2[p]+w;
else dp2[u]=mn1[p]+w;
dp2[u]=min(dp2[u],dp2[p]+w);
}
}
}
for(auto vv:c[u])
{
ll v=vv.fi;
ll w=vv.se;
//if(v==p) continue;
dfs2(v,u,w);
}
}
#define pb push_back
ll in[maxN],out[maxN];
bool cmp(ll x,ll y)
{
return in[x]<in[y];
}
ll cnt[maxN];
ll h[maxN];
ll dd[maxN],tg=0;
ll par[maxN][21];
void dfs(ll u=1,ll p=1)
{
in[u]=++tg;
par[u][0]=p;
for(int i=1;i<=20;i++) par[u][i]=par[par[u][i-1]][i-1];
for(auto vv:g[u])
{
ll v=vv.fi;
ll w=vv.se;
if(v!=p)
{
h[v]=h[u]+1;
dd[v]=dd[u]+w;
dfs(v,u);
}
}
out[u]=tg;
}
ll LCA(ll u, ll v)
{
if(h[u]<h[v]) swap(u,v);
ll log=log2(h[u])+1;
for(int i=log;i>=0;i--)
{
if(h[par[u][i]]>=h[v]) u=par[u][i];
}
if(u==v) return u;
for(int i=log;i>=0;i--)
{
if(par[u][i]!=par[v][i]) u=par[u][i],v=par[v][i];
}
return par[u][0];
}
ll dis(ll u,ll v)
{
return dd[u]+dd[v]-2*dd[LCA(u,v)];
}
bool isParent(ll x,ll y)
{
return in[x]<=in[y]&&out[y]<=out[x];
}
void Init(int N,int A[],int B[],int D[])
{
for(int i=0;i<N-1;i++)
{
g[A[i]].pb({B[i],D[i]});
g[B[i]].pb({A[i],D[i]});
}
dfs();
}
ll Query(int s,int x[],int t,int y[])
{
vector<ll> vv;
for(int i=0;i<s;i++) vv.pb(x[i]),vt[x[i]]+=1;
for(int i=0;i<t;i++) vv.pb(y[i]),vt[y[i]]+=2;
sort(vv.begin(),vv.end(),cmp);
ll k=vv.size();
for(int i=1;i<k;i++)
{
ll v=LCA(vv[i],vv[i-1]);
vv.pb(v);
}
sort(vv.begin(),vv.end(),cmp);
stack<int>st;
for(int i=0;i<vv.size();i++)
{
if(cnt[vv[i]]==0)
{
while(!st.empty()&&(!isParent(st.top(),vv[i]))) st.pop();
if(!st.empty()) c[st.top()].pb({vv[i],dis(st.top(),vv[i])});
st.push(vv[i]);
cnt[vv[i]]=1;
}
}
//cout << '\n';
dfs1(vv[0],-1);
dfs2(vv[0],-1,-1);
ll ans=inf;
for(int i=0;i<vv.size();i++)
{
if(vt[vv[i]]==1||vt[vv[i]]==3)
{
ans=min(ans,min(dp1[vv[i]],dp2[vv[i]]));
}
}
for(int i=0;i<vv.size();i++)
{
cnt[vv[i]]=0;
c[vv[i]].clear();
vt[vv[i]]=0;
}
return ans;
}
Compilation message
factories.cpp: In function 'll Query(int, int*, int, int*)':
factories.cpp:138:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
138 | for(int i=0;i<vv.size();i++)
| ~^~~~~~~~~~
factories.cpp:152:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
152 | for(int i=0;i<vv.size();i++)
| ~^~~~~~~~~~
factories.cpp:159:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
159 | for(int i=0;i<vv.size();i++)
| ~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
94680 KB |
Output is correct |
2 |
Correct |
1139 ms |
104132 KB |
Output is correct |
3 |
Correct |
1311 ms |
104160 KB |
Output is correct |
4 |
Correct |
1271 ms |
104376 KB |
Output is correct |
5 |
Correct |
1184 ms |
104324 KB |
Output is correct |
6 |
Correct |
710 ms |
104908 KB |
Output is correct |
7 |
Correct |
1362 ms |
104828 KB |
Output is correct |
8 |
Correct |
1294 ms |
105040 KB |
Output is correct |
9 |
Correct |
1197 ms |
105144 KB |
Output is correct |
10 |
Correct |
716 ms |
104888 KB |
Output is correct |
11 |
Correct |
1316 ms |
104840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
94540 KB |
Output is correct |
2 |
Correct |
1839 ms |
254408 KB |
Output is correct |
3 |
Correct |
4179 ms |
258144 KB |
Output is correct |
4 |
Correct |
1244 ms |
251948 KB |
Output is correct |
5 |
Correct |
3484 ms |
293664 KB |
Output is correct |
6 |
Correct |
4846 ms |
260052 KB |
Output is correct |
7 |
Correct |
3877 ms |
135368 KB |
Output is correct |
8 |
Correct |
1177 ms |
134768 KB |
Output is correct |
9 |
Correct |
2921 ms |
142832 KB |
Output is correct |
10 |
Correct |
3383 ms |
136616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
94680 KB |
Output is correct |
2 |
Correct |
1139 ms |
104132 KB |
Output is correct |
3 |
Correct |
1311 ms |
104160 KB |
Output is correct |
4 |
Correct |
1271 ms |
104376 KB |
Output is correct |
5 |
Correct |
1184 ms |
104324 KB |
Output is correct |
6 |
Correct |
710 ms |
104908 KB |
Output is correct |
7 |
Correct |
1362 ms |
104828 KB |
Output is correct |
8 |
Correct |
1294 ms |
105040 KB |
Output is correct |
9 |
Correct |
1197 ms |
105144 KB |
Output is correct |
10 |
Correct |
716 ms |
104888 KB |
Output is correct |
11 |
Correct |
1316 ms |
104840 KB |
Output is correct |
12 |
Correct |
46 ms |
94540 KB |
Output is correct |
13 |
Correct |
1839 ms |
254408 KB |
Output is correct |
14 |
Correct |
4179 ms |
258144 KB |
Output is correct |
15 |
Correct |
1244 ms |
251948 KB |
Output is correct |
16 |
Correct |
3484 ms |
293664 KB |
Output is correct |
17 |
Correct |
4846 ms |
260052 KB |
Output is correct |
18 |
Correct |
3877 ms |
135368 KB |
Output is correct |
19 |
Correct |
1177 ms |
134768 KB |
Output is correct |
20 |
Correct |
2921 ms |
142832 KB |
Output is correct |
21 |
Correct |
3383 ms |
136616 KB |
Output is correct |
22 |
Correct |
3922 ms |
266648 KB |
Output is correct |
23 |
Correct |
3447 ms |
267512 KB |
Output is correct |
24 |
Correct |
6073 ms |
272612 KB |
Output is correct |
25 |
Correct |
5860 ms |
275308 KB |
Output is correct |
26 |
Correct |
7774 ms |
264740 KB |
Output is correct |
27 |
Correct |
5703 ms |
307560 KB |
Output is correct |
28 |
Correct |
2025 ms |
264304 KB |
Output is correct |
29 |
Correct |
7508 ms |
262504 KB |
Output is correct |
30 |
Correct |
7567 ms |
263692 KB |
Output is correct |
31 |
Correct |
7613 ms |
264776 KB |
Output is correct |
32 |
Correct |
3067 ms |
155368 KB |
Output is correct |
33 |
Correct |
1333 ms |
145200 KB |
Output is correct |
34 |
Correct |
2235 ms |
140064 KB |
Output is correct |
35 |
Correct |
2172 ms |
139856 KB |
Output is correct |
36 |
Correct |
3540 ms |
140504 KB |
Output is correct |
37 |
Correct |
3693 ms |
139756 KB |
Output is correct |