#include <bits/stdc++.h>
#include "factories.h"
using namespace std;
#define N 500005
#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
bool vis[N];
vector <pair <int, int> > edg[N];
int table[21][N], deep[N], cnt;
ll dist[N];
void dfs(int u){
vis[u] = 1;
deep[u] = cnt;
cnt++;
for (auto v : edg[u])
if (!vis[v.F]){
dist[v.F] = dist[u] + v.S;
table[0][v.F] = u;
dfs(v.F);
}
cnt--;
}
int lca(int u, int v) {
if(deep[u] < deep[v]) swap(u, v);
int z = deep[u] - deep[v];
for(int i = 0; i < 20; i++)
if((z >> i) & 1) u = table[i][u];
if(u == v) return u;
for(int i = 20; i >= 0; i--)
if(table[i][u] != table[i][v]) {
u = table[i][u];
v = table[i][v];
}
return table[0][u];
}
void Init(int n, int C1[], int C2[], int ZAI[]) {
for (int i = 0;i < n - 1;i++){
edg[C1[i]].pb(mp(C2[i], ZAI[i]));
edg[C2[i]].pb(mp(C1[i], ZAI[i]));
}
dfs(0);
table[0][0] = -1;
for (int i = 1;i <= 3;i++)
for (int j = 0;j < n;j++){
if (table[i - 1][j] != -1) table[i][j] = table[i - 1][table[i - 1][j]];
else table[i][j] = -1;
}
}
long long Query(int S1, int X1[], int S2, int X2[]) {
ll ans = 1e18;
for (int i = 0;i < S1;i++)
for (int j = 0;j < S2;j++)
ans = min(ans, dist[X1[i]] + dist[X2[j]] - 2 * dist[lca(X1[i], X2[j])]);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
12544 KB |
Output is correct |
2 |
Execution timed out |
8037 ms |
29832 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
12404 KB |
Output is correct |
2 |
Incorrect |
2354 ms |
58632 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
12544 KB |
Output is correct |
2 |
Execution timed out |
8037 ms |
29832 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |