#include<bits/stdc++.h>
#include "factories.h"
#define ll long long
#define pii pair<ll,ll>
#define ff first
#define ss second
using namespace std;
int pre[500009],td[500009],fst[500009];
ll dep[500009],inv[500009];
int st[22][1500009],cnt = 0;
vector<int> G[500009],d[500009];
vector<int> G2[500009];
vector<ll> d2[500009];
vector<int> ov;
void DFS(int u,int p)
{
cnt++;
pre[u] = cnt;
inv[pre[u]] = u;
fst[u] = ov.size();
ov.push_back(pre[u]);
for(int i = 0;i < G[u].size();i++)
{
int v = G[u][i];
if(v == p) continue;
dep[v] = dep[u] + d[u][i];
td[v] = td[u] + 1;
DFS(v,u);
if(i + 1 < G[u].size()) ov.push_back(pre[u]);
}
}
int LCA(int a,int b)
{
if(a == b) return a;
if(a == 0 || b == 0) return 0LL;
if(fst[a] > fst[b]) swap(a,b);
int ret;
int tp = __lg(fst[b]-fst[a]);
//cout << fst[a] << " " << fst[b] << " " << tp << " " << min(st[tp][fst[a]],st[tp][fst[b] - (1<<tp)]) << endl;
return inv[min(st[tp][fst[a]],st[tp][fst[b] - (1<<tp)])];
// ret = min()
}
const bool cmp(const int &a,const int & b)
{
return pre[a] < pre[b];
}
void Init(int N, int A[], int B[], int D[]) {
for(int i = 0;i < N-1;i++)
{
A[i]++;
B[i]++;
G[A[i]].push_back(B[i]);
G[B[i]].push_back(A[i]);
d[A[i]].push_back(D[i]);
d[B[i]].push_back(D[i]);
}
td[1] = 1;
ov.push_back(0);
DFS(1,0);
for(int i = 0;i < ov.size();i++)
{
st[0][i] = ov[i];
}
for(int i = 1;i < 22;i++)
{
for(int j = 0;j+(1<<(i-1)) < ov.size();j++)
{
st[i][j] = min(st[i-1][j],st[i-1][j+(1<<(i-1))]);
//cout << st[i][j] << " ";
}
//cout << endl;
}
}
vector<ll> vs;
int temp[500009];
int stk[500009];
ll ans = 10000000000000000;
int s,x[500009],t,y[500009];
void add_edge(int a,int b)
{
ll tp = abs(dep[a]-dep[b]);
//cerr << a << "_" << b << "_" << tp << endl;
G2[a].push_back(b);
G2[b].push_back(a);
d2[a].push_back(tp);
d2[b].push_back(tp);
}
ll iss[500009],ist[500009];
ll dp1[500009],dp2[500009];
set<int> used;
void DFS2(int u,int p)
{
if(iss[u]) dp1[u] = 0;
if(ist[u]) dp2[u] = 0;
for(int i = 0;i < G2[u].size();i++)
{
int v = G2[u][i];
if(v == p) continue;
DFS2(v,u);
dp1[u] = min(dp1[v]+d2[u][i],dp1[u]);
dp2[u] = min(dp2[v]+d2[u][i],dp2[u]);
}
//cout << u << "_" << dp1[u] << "_" << dp2[u] << endl;
ans = min(ans,dp1[u] + dp2[u]);
}
void DKS()
{
int i;
for(i = 0;i < s;i++)
{
iss[x[i]] = 1;
}
for(i = 0;i < t;i++)
{
ist[y[i]] = 1;
}
for(auto tt : used)
{
dp1[tt] = dp2[tt] = 10000000000000000;
}
DFS2(x[0],0);
for(i = 0;i < s;i++)
{
iss[x[i]] = 0;
}
for(i = 0;i < t;i++)
{
ist[y[i]] = 0;
}
}
long long Query(int S, int X[], int T, int Y[]) {
vs.clear();
s = S;
used.clear();
for(int i = 0;i < S;i++)
{
X[i]++;
used.insert(X[i]);
x[i] = X[i];
G2[X[i]].clear();
d2[X[i]].clear();
temp[X[i]] = 1;
vs.push_back(X[i]);
}
int fg = 0;
t = T;
for(int i = 0;i < T;i++)
{
Y[i]++;
used.insert(Y[i]);
y[i] = Y[i];
G2[Y[i]].clear();
d2[Y[i]].clear();
vs.push_back(Y[i]);
if(temp[Y[i]] == 1) fg = 1;
}
for(int i = 0;i < S;i++) temp[X[i]] = 0;
if(fg == 1) return 0;
sort(vs.begin(),vs.end(),cmp);
stk[0] = 0;
int sz = 1;
ans = 10000000000000000;
for(int i = 0;i < vs.size();i++)
{
ll u = vs[i];
ll p = LCA(u,stk[sz-1]);
//cerr << vs[i] << " p = " << p << endl;
if(!used.count(p))
{
used.insert(p);
G2[p].clear();
d2[p].clear();
}
if(p == stk[sz-1]) stk[sz++] = u;
else
{
while(sz > 1 && td[stk[sz-2]] >= td[p])
{
add_edge(stk[sz-1],stk[sz-2]);
sz--;
}
if(stk[sz-1] != p)
{
add_edge(stk[sz-1],p);
stk[sz-1] = p;
}
stk[sz++] = u;
}
}
for (int i = 1; i < sz - 1; ++i) add_edge(stk[i], stk[i + 1]);
DKS();
return ans;
}
Compilation message
factories.cpp: In function 'void DFS(int, int)':
factories.cpp:22:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for(int i = 0;i < G[u].size();i++)
| ~~^~~~~~~~~~~~~
factories.cpp:29:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | if(i + 1 < G[u].size()) ov.push_back(pre[u]);
| ~~~~~~^~~~~~~~~~~~~
factories.cpp: In function 'int LCA(int, int)':
factories.cpp:37:9: warning: unused variable 'ret' [-Wunused-variable]
37 | int ret;
| ^~~
factories.cpp: In function 'void Init(int, int*, int*, int*)':
factories.cpp:60:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for(int i = 0;i < ov.size();i++)
| ~~^~~~~~~~~~~
factories.cpp:67:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int j = 0;j+(1<<(i-1)) < ov.size();j++)
| ~~~~~~~~~~~~~^~~~~~~~~~~
factories.cpp: In function 'void DFS2(int, int)':
factories.cpp:97:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for(int i = 0;i < G2[u].size();i++)
| ~~^~~~~~~~~~~~~~
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:167:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
167 | for(int i = 0;i < vs.size();i++)
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
47980 KB |
Output is correct |
2 |
Correct |
1536 ms |
57196 KB |
Output is correct |
3 |
Correct |
1554 ms |
57068 KB |
Output is correct |
4 |
Correct |
1604 ms |
57452 KB |
Output is correct |
5 |
Correct |
1340 ms |
57596 KB |
Output is correct |
6 |
Correct |
1102 ms |
57456 KB |
Output is correct |
7 |
Correct |
1534 ms |
57056 KB |
Output is correct |
8 |
Correct |
1537 ms |
57292 KB |
Output is correct |
9 |
Correct |
1349 ms |
57580 KB |
Output is correct |
10 |
Correct |
1164 ms |
57448 KB |
Output is correct |
11 |
Correct |
1600 ms |
57292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
47724 KB |
Output is correct |
2 |
Correct |
2367 ms |
220884 KB |
Output is correct |
3 |
Correct |
2525 ms |
219952 KB |
Output is correct |
4 |
Correct |
1813 ms |
236616 KB |
Output is correct |
5 |
Correct |
2261 ms |
245208 KB |
Output is correct |
6 |
Correct |
2637 ms |
222004 KB |
Output is correct |
7 |
Correct |
2580 ms |
89576 KB |
Output is correct |
8 |
Correct |
1611 ms |
93400 KB |
Output is correct |
9 |
Correct |
1816 ms |
92896 KB |
Output is correct |
10 |
Correct |
2428 ms |
90728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
47980 KB |
Output is correct |
2 |
Correct |
1536 ms |
57196 KB |
Output is correct |
3 |
Correct |
1554 ms |
57068 KB |
Output is correct |
4 |
Correct |
1604 ms |
57452 KB |
Output is correct |
5 |
Correct |
1340 ms |
57596 KB |
Output is correct |
6 |
Correct |
1102 ms |
57456 KB |
Output is correct |
7 |
Correct |
1534 ms |
57056 KB |
Output is correct |
8 |
Correct |
1537 ms |
57292 KB |
Output is correct |
9 |
Correct |
1349 ms |
57580 KB |
Output is correct |
10 |
Correct |
1164 ms |
57448 KB |
Output is correct |
11 |
Correct |
1600 ms |
57292 KB |
Output is correct |
12 |
Correct |
36 ms |
47724 KB |
Output is correct |
13 |
Correct |
2367 ms |
220884 KB |
Output is correct |
14 |
Correct |
2525 ms |
219952 KB |
Output is correct |
15 |
Correct |
1813 ms |
236616 KB |
Output is correct |
16 |
Correct |
2261 ms |
245208 KB |
Output is correct |
17 |
Correct |
2637 ms |
222004 KB |
Output is correct |
18 |
Correct |
2580 ms |
89576 KB |
Output is correct |
19 |
Correct |
1611 ms |
93400 KB |
Output is correct |
20 |
Correct |
1816 ms |
92896 KB |
Output is correct |
21 |
Correct |
2428 ms |
90728 KB |
Output is correct |
22 |
Correct |
6805 ms |
234604 KB |
Output is correct |
23 |
Correct |
5071 ms |
236808 KB |
Output is correct |
24 |
Correct |
7624 ms |
238888 KB |
Output is correct |
25 |
Correct |
7637 ms |
240768 KB |
Output is correct |
26 |
Correct |
5565 ms |
229240 KB |
Output is correct |
27 |
Correct |
5305 ms |
250904 KB |
Output is correct |
28 |
Correct |
4175 ms |
252268 KB |
Output is correct |
29 |
Correct |
4897 ms |
227660 KB |
Output is correct |
30 |
Correct |
4899 ms |
226876 KB |
Output is correct |
31 |
Correct |
4756 ms |
227484 KB |
Output is correct |
32 |
Correct |
3169 ms |
113832 KB |
Output is correct |
33 |
Correct |
2679 ms |
114548 KB |
Output is correct |
34 |
Correct |
3539 ms |
102368 KB |
Output is correct |
35 |
Correct |
3273 ms |
102184 KB |
Output is correct |
36 |
Correct |
3529 ms |
102372 KB |
Output is correct |
37 |
Correct |
3341 ms |
102240 KB |
Output is correct |