#include "factories.h"
#include <bits/stdc++.h>
#define ii pair<int,int>
#define f first
#define s second
#define pb push_back
#define ll long long
#define endl '\n'
using namespace std;
vector<ii> w[500005];
vector<pair<int,ll> > w2[500005];
int level[500005][33];
int First[500005];
int End[500005];
ll dist[500005];
int x = __lg(500005) + 2;
int DFStime;
int color[500005];
int s[500005];
int t[500005];
ll mindist;
void DFS(int u,int par)
{
First[u] = ++DFStime;
level[u][0] = par;
for(int i = 1;i < x;i++)
{
level[u][i] = level[level[u][i - 1]][i - 1];
}
for(auto v:w[u])
{
if(v.first != par)
{
dist[v.first] = dist[u] + v.second;
DFS(v.first,u);
}
}
End[u] = DFStime;
}
bool check(int u,int v)
{
return (First[u] <= First[v] and End[v] <= End[u]);
}
int LCA(int u,int v)
{
if(check(u,v))
{
return u;
}
if(check(v,u))
{
return v;
}
for(int i = 32;i >= 0;i--)
{
if(!check(level[u][i],v))
{
u = level[u][i];
}
}
return level[u][0];
}
void Init(int n, int a[], int b[], int d[])
{
for(int i = 0;i < n - 1;i++)
{
w[a[i]].pb({b[i],d[i]}),
w[b[i]].pb({a[i],d[i]});
}
DFS(0,0);
}
void edge(int u,int v)
{
if(u == v)
{
return;
}
ll d = abs(dist[u] - dist[v]);
w2[u].push_back({v,d});
w2[v].push_back({u,d});
}
int virtual_tree(vector<int> &nodes)
{
sort(nodes.begin(),nodes.end(),[](int &i, int &j){return (First[i] < First[j]);});
for(int i = (int) (nodes.size()) - 2;i >= 0;i--)
{
int res = LCA(nodes[i],nodes[i + 1]);
nodes.push_back(res);
}
sort(nodes.begin(),nodes.end(),[](int &i, int &j){return (First[i] < First[j]);});
vector<int> s;
s.push_back(nodes[0]);
for(int i = 1;i < nodes.size();i++)
{
while(!check(s.back(),nodes[i]))
{
edge(s.back(),s[s.size() - 2]);
s.pop_back();
}
s.push_back(nodes[i]);
}
while(s.size() > 1)
{
edge(s.back(),s[s.size() - 2]);
s.pop_back();
}
return s[0];
}
pair<ll,ll> DFS2(int u,int par)
{
ll min1 = 1e18;
ll min2 = 1e18;
if(color[u] == 1)
{
min1 = 0;
}
else if(color[u] == 2)
{
min2 = 0;
}
for(auto v:w2[u])
{
if(v.first != par)
{
pair<ll,ll> tree = DFS2(v.first,u);
min1 = min(min1,v.second + tree.first);
min2 = min(min2,v.second + tree.second);
}
}
mindist = min(mindist,min1 + min2);
return {min1,min2};
}
long long Query(int n, int x[], int m, int y[])
{
vector<int> nodes;
for(int i = 0;i < n;i++)
{
nodes.push_back(x[i]);
color[x[i]] = 1;
}
for(int i = 0;i < m;i++)
{
nodes.push_back(y[i]);
color[y[i]] = 2;
}
int r = virtual_tree(nodes);
mindist = 1e18;
DFS2(r,r);
for(auto x:nodes)
w2[x].clear(),
color[x] = 0;
return mindist;
}
Compilation message
factories.cpp: In function 'int virtual_tree(std::vector<int>&)':
factories.cpp:93:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for(int i = 1;i < nodes.size();i++)
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
24148 KB |
Output is correct |
2 |
Correct |
791 ms |
33072 KB |
Output is correct |
3 |
Correct |
746 ms |
33168 KB |
Output is correct |
4 |
Correct |
733 ms |
33148 KB |
Output is correct |
5 |
Correct |
506 ms |
33304 KB |
Output is correct |
6 |
Correct |
663 ms |
33016 KB |
Output is correct |
7 |
Correct |
738 ms |
33052 KB |
Output is correct |
8 |
Correct |
699 ms |
33260 KB |
Output is correct |
9 |
Correct |
534 ms |
33296 KB |
Output is correct |
10 |
Correct |
669 ms |
33024 KB |
Output is correct |
11 |
Correct |
769 ms |
33024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
24020 KB |
Output is correct |
2 |
Correct |
1304 ms |
143644 KB |
Output is correct |
3 |
Correct |
1902 ms |
146532 KB |
Output is correct |
4 |
Correct |
1042 ms |
144420 KB |
Output is correct |
5 |
Correct |
1224 ms |
174032 KB |
Output is correct |
6 |
Correct |
1937 ms |
148236 KB |
Output is correct |
7 |
Correct |
1521 ms |
57252 KB |
Output is correct |
8 |
Correct |
1000 ms |
57172 KB |
Output is correct |
9 |
Correct |
612 ms |
61872 KB |
Output is correct |
10 |
Correct |
1348 ms |
58376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
24148 KB |
Output is correct |
2 |
Correct |
791 ms |
33072 KB |
Output is correct |
3 |
Correct |
746 ms |
33168 KB |
Output is correct |
4 |
Correct |
733 ms |
33148 KB |
Output is correct |
5 |
Correct |
506 ms |
33304 KB |
Output is correct |
6 |
Correct |
663 ms |
33016 KB |
Output is correct |
7 |
Correct |
738 ms |
33052 KB |
Output is correct |
8 |
Correct |
699 ms |
33260 KB |
Output is correct |
9 |
Correct |
534 ms |
33296 KB |
Output is correct |
10 |
Correct |
669 ms |
33024 KB |
Output is correct |
11 |
Correct |
769 ms |
33024 KB |
Output is correct |
12 |
Correct |
14 ms |
24020 KB |
Output is correct |
13 |
Correct |
1304 ms |
143644 KB |
Output is correct |
14 |
Correct |
1902 ms |
146532 KB |
Output is correct |
15 |
Correct |
1042 ms |
144420 KB |
Output is correct |
16 |
Correct |
1224 ms |
174032 KB |
Output is correct |
17 |
Correct |
1937 ms |
148236 KB |
Output is correct |
18 |
Correct |
1521 ms |
57252 KB |
Output is correct |
19 |
Correct |
1000 ms |
57172 KB |
Output is correct |
20 |
Correct |
612 ms |
61872 KB |
Output is correct |
21 |
Correct |
1348 ms |
58376 KB |
Output is correct |
22 |
Correct |
2575 ms |
154304 KB |
Output is correct |
23 |
Correct |
2339 ms |
154596 KB |
Output is correct |
24 |
Correct |
2854 ms |
157892 KB |
Output is correct |
25 |
Correct |
2835 ms |
160568 KB |
Output is correct |
26 |
Correct |
2892 ms |
152240 KB |
Output is correct |
27 |
Correct |
1970 ms |
175560 KB |
Output is correct |
28 |
Correct |
1803 ms |
152084 KB |
Output is correct |
29 |
Correct |
3082 ms |
151016 KB |
Output is correct |
30 |
Correct |
3064 ms |
150372 KB |
Output is correct |
31 |
Correct |
3038 ms |
150916 KB |
Output is correct |
32 |
Correct |
898 ms |
63524 KB |
Output is correct |
33 |
Correct |
1078 ms |
60752 KB |
Output is correct |
34 |
Correct |
1474 ms |
56400 KB |
Output is correct |
35 |
Correct |
1456 ms |
56196 KB |
Output is correct |
36 |
Correct |
1572 ms |
56840 KB |
Output is correct |
37 |
Correct |
1435 ms |
56792 KB |
Output is correct |