// author : anhtun_hi , nqg
#include <bits/stdc++.h>
#define ll long long
#define ii pair<ll, ll>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define reset(h, v) memset(h, v, sizeof h)
#define Forv(i, a) for(auto& i : a)
#define For(i, a, b) for(int i = a; i <= b; ++i)
#define Ford(i, a, b) for(int i = a; i >= b; --i)
#define c_bit(i) __builtin_popcountll(i)
#define Bit(mask, i) ((mask >> i) & 1LL)
#define onbit(mask, i) ((mask) bitor (1LL << i))
#define offbit(mask, i) ((mask) &~ (1LL << i))
using namespace std;
namespace std {
template <typename T, int D>
struct _vector : public vector <_vector <T, D - 1>> {
static_assert(D >= 1, "Dimension must be positive!");
template <typename... Args>
_vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
};
template <typename T> struct _vector <T, 1> : public vector <T> {
_vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
};
template <class A, class B> bool minimize(A &a, B b){return a > b ? a = b, true : false;}
template <class A, class B> bool maximize(A &a, B b){return a < b ? a = b, true : false;}
}
const int dx[] = {0, 0, +1, -1}, dy[] = {-1, +1, 0, 0}, LOG = 20, base = 311, inf = 1e9 + 5;
const int MAXN = 1e6 + 5;
const ll mod = 1e9 + 7;
const ll oo = 1e18;
//#define int long long
int sz[MAXN], p[MAXN]; bool del[MAXN];
vector<ii> g[MAXN];
void dfsSize(int u, int par){
sz[u] = 1;
for(auto [v, w] : g[u]) if((v ^ par) && !del[v]){
dfsSize(v, u);
sz[u] += sz[v];
}
}
int find(int u, int par, int n){
for(auto [v, w] : g[u])if(v ^ par){
if(sz[v] > n / 2 && !del[v]) return find(v, u, n);
}
return u;
}
map<int, ll> d[MAXN];
void prepare(int u, int par, ll len, int root){
d[root][u] = len;
for(auto [v, w] : g[u]) if(v ^ par && !del[v]) prepare(v, u, len + w, root);
}
void buildCentroid(int u, int par){
dfsSize(u, par);
int c = find(u, par, sz[u]);
del[c] = true;
p[c] = par;
prepare(c, par, 0, c);
for(auto [v, _] : g[c]) if((v ^ par) && !del[v]) {
buildCentroid(v, c);
}
}
multiset<ii> res[MAXN]; bool op[MAXN];
void update(int u){
int v = u;
while(v){
op[v] ^= 1;
if(op[v]) res[v].insert({d[v][u], u});
else res[v].erase({d[v][u], u});
v = p[v];
}
}
ll get(int u){
int v = u;
ll ans = 0;
// cerr << res[u].size() << '\n';
while(v){
if(!res[v].empty()){
auto it = res[v].begin();
++it;
minimize(ans, it -> fi + d[v][u]);
}
v = p[v];
}
return ans;
}
void Init(int n, int a[], int b[], int d[]){
cin >> n;
For(i, 0, n - 2) {
cin >> a[i] >> b[i] >> d[i];
}
For(i, 0, n - 2){
++a[i], ++b[i];
g[a[i]].push_back({b[i], d[i]});
g[b[i]].push_back({a[i], d[i]});
}
buildCentroid(1, 0);
}
long long Query(int S, int X[], int T, int Y[]){
cin >> S;
For(i, 0, S - 1) cin >> X[i];
cin >> T;
For(i, 0, T - 1) cin >> Y[i];
For(i, 0, S - 1){
int u = X[i] + 1;
update(u);
}
ll ans = oo;
For(i, 0, T - 1){
minimize(ans, get(Y[i] + 1));
}
For(i, 0, S - 1){
int u = X[i] + 1;
update(u);
}
return ans;
}
//void Solve() {
//// Init(n, a[], b[], d[]);
//}
//
//int32_t main() {
// cin.tie(0) -> sync_with_stdio(0);
// if(fopen("JOI14_factories.inp", "r")) {
// freopen("JOI14_factories.inp", "r", stdin);
// freopen("JOI14_factories.out", "w", stdout);
// }
//
// int t = 1;
//// cin >> t;
//
// for (int test = 1; test <= t; test++) {
// Solve();
// }
// return 0;
//}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
115 ms |
118344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
86 ms |
118088 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
115 ms |
118344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |