#include "factories.h"
#include <bits/stdc++.h>
#define int long long
#define F first
#define S second
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define kill(x) cout << x << endl, exit(0);
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
const int MAXN = (int)2e6 + 7;
const int MOD = 998244353;
const ll INF = (ll)1e18 + 7;
ll n, m, t, k, q, u, v, w, tmp, tmp2, tmp3, tmp4, tmp5, ans2, flag;
ll ans;
ll arr[MAXN], sz[MAXN];
bool hate[MAXN];
pair<pair<ll, ll>, pair<ll, ll> > love[MAXN];
vector<pair<ll, ll> > adj[MAXN];
vector<pair<ll, ll> > vec[MAXN];
void DFSsz(int v, int p=-1) {
sz[v] = 1;
for (auto cur:adj[v]) {
int u = cur.F;
if (!hate[u] && u != p) {
DFSsz(u, v);
sz[v] += sz[u];
}
}
}
int centroid(int tot, int v, int p=-1) {
for (auto cur:adj[v]) {
int u = cur.F;
if (!hate[u] && u != p && 2*sz[u] > tot) return centroid(tot, u, v);
}
return v;
}
void all_i_want(int cent, int v, int p, ll dis) {
for (auto cur:adj[v]) {
ll u = cur.F, w = cur.S;
if (!hate[u] && u != p) all_i_want(cent, u, v, dis+w);
}
vec[v].pb(mp(cent, dis));
}
void solve(int v) {
DFSsz(v);
int cent = centroid(sz[v], v);
hate[cent] = 1;
for (auto cur:adj[cent]) {
ll u = cur.F, w = cur.S;
if (!hate[u]) all_i_want(cent, u, cent, w);
}
vec[cent].pb(mp(cent, 0ll));
for (auto cur:adj[cent]) {
int u = cur.F;
if (!hate[u]) solve(u);
}
}
void add(int v, bool x=0) {
for (auto cur:vec[v]) {
int u = cur.F;
ll w = cur.S;
if (x) {
if (love[u].F.S != flag) {
love[u].F.S = flag;
love[u].F.F = w;
} else {
love[u].F.F = min(love[u].F.F, w);
}
} else {
if (love[u].S.S != flag) {
love[u].S.S = flag;
love[u].S.F = w;
} else {
love[u].S.F = min(love[u].S.F, w);
}
}
if (love[u].F.S == love[u].S.S && love[u].S.S == flag) ans = min(ans, love[u].F.F+love[u].S.F);
}
}
void Init(int32_t N, int32_t A[], int32_t B[], int32_t D[]) {
n = N;
for (int i=1; i<n; i++) {
u = A[i-1], v = B[i-1], w = D[i-1];
u++, v++;
adj[u].pb(mp(v, w));
adj[v].pb(mp(u, w));
}
solve(1);
}
int Query(int32_t SS, int32_t X[], int32_t T, int32_t Y[]) {
flag++;
ans = INF;
while (SS--) {
cin >> u; u++;
add(u, 0);
}
while (T--) {
cin >> v; v++;
add(v, 1);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
33 ms |
115536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
115544 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
33 ms |
115536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |