// 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]; ll d[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;
}
vector<ii> List[MAXN];
void prepare(int u, int par, ll len, int root)
{
List[u].push_back({root, 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, 0, 0, c);
for(auto [v, _] : g[c]) if((v ^ par) && !del[v]) {
buildCentroid(v, c);
}
}
long long Query(int S, int X[], int T, int Y[]){
// cin >> S;cin >> T;
// For(i, 0, S - 1) cin >> X[i];For(i, 0, T - 1) cin >> Y[i];
For(i, 0, S - 1){
for(auto [par, w] : List[X[i] + 1]) minimize(d[par], w);
}
ll ans = oo;
For(i, 0, T - 1){
int u = Y[i] + 1;
for(auto [par, w] : List[u]){
minimize(ans, d[par] + w);
}
}
For(i, 0, S - 1){
for(auto [par, w] : List[X[i] + 1]) d[par] = oo;
}
return ans;
}
int n, a[MAXN], b[MAXN], w[MAXN], S, T, x[MAXN], y[MAXN];
void Init(int n, int a[], int b[], int w[]){
// cin >> n;
// int q; cin >> q;
// For(i, 0, n - 2) {
// cin >> a[i] >> b[i] >> w[i];
// }
For(i, 0, n - 2){
++a[i], ++b[i];
g[a[i]].push_back({b[i], w[i]});
g[b[i]].push_back({a[i], w[i]});
}
For(i, 0, n + 5) d[i] = oo;
buildCentroid(1, 0);
// while(q--){
// cout << Query(S, x, T, y) << '\n';
// }
}
//void Solve() {
// Init(n, a, b, w);
//}
//
//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;
//}
Compilation message
factories.cpp: In function 'void dfsSize(int, int)':
factories.cpp:43:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
43 | for(auto [v, w] : g[u]) if((v ^ par) && !del[v]){
| ^
factories.cpp: In function 'int find(int, int, int)':
factories.cpp:51:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
51 | for(auto [v, w] : g[u])if(v ^ par){
| ^
factories.cpp: In function 'void prepare(int, int, long long int, int)':
factories.cpp:62:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
62 | for(auto [v, w] : g[u]) if(v != par && !del[v]) prepare(v, u, len + w, root);
| ^
factories.cpp: In function 'void buildCentroid(int, int)':
factories.cpp:72:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
72 | for(auto [v, _] : g[c]) if((v ^ par) && !del[v]) {
| ^
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:83:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
83 | for(auto [par, w] : List[X[i] + 1]) minimize(d[par], w);
| ^
factories.cpp:88:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
88 | for(auto [par, w] : List[u]){
| ^
factories.cpp:93:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
93 | for(auto [par, w] : List[X[i] + 1]) d[par] = oo;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
70480 KB |
Output is correct |
2 |
Correct |
213 ms |
85020 KB |
Output is correct |
3 |
Correct |
277 ms |
84816 KB |
Output is correct |
4 |
Correct |
217 ms |
85284 KB |
Output is correct |
5 |
Correct |
240 ms |
85576 KB |
Output is correct |
6 |
Correct |
175 ms |
84296 KB |
Output is correct |
7 |
Correct |
230 ms |
85280 KB |
Output is correct |
8 |
Correct |
239 ms |
85284 KB |
Output is correct |
9 |
Correct |
251 ms |
85580 KB |
Output is correct |
10 |
Correct |
154 ms |
84296 KB |
Output is correct |
11 |
Correct |
224 ms |
85320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
70224 KB |
Output is correct |
2 |
Correct |
1513 ms |
256584 KB |
Output is correct |
3 |
Correct |
2280 ms |
315884 KB |
Output is correct |
4 |
Correct |
604 ms |
136160 KB |
Output is correct |
5 |
Correct |
3042 ms |
396060 KB |
Output is correct |
6 |
Correct |
2344 ms |
322388 KB |
Output is correct |
7 |
Correct |
612 ms |
122696 KB |
Output is correct |
8 |
Correct |
267 ms |
94348 KB |
Output is correct |
9 |
Correct |
716 ms |
135912 KB |
Output is correct |
10 |
Correct |
615 ms |
121240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
70480 KB |
Output is correct |
2 |
Correct |
213 ms |
85020 KB |
Output is correct |
3 |
Correct |
277 ms |
84816 KB |
Output is correct |
4 |
Correct |
217 ms |
85284 KB |
Output is correct |
5 |
Correct |
240 ms |
85576 KB |
Output is correct |
6 |
Correct |
175 ms |
84296 KB |
Output is correct |
7 |
Correct |
230 ms |
85280 KB |
Output is correct |
8 |
Correct |
239 ms |
85284 KB |
Output is correct |
9 |
Correct |
251 ms |
85580 KB |
Output is correct |
10 |
Correct |
154 ms |
84296 KB |
Output is correct |
11 |
Correct |
224 ms |
85320 KB |
Output is correct |
12 |
Correct |
12 ms |
70224 KB |
Output is correct |
13 |
Correct |
1513 ms |
256584 KB |
Output is correct |
14 |
Correct |
2280 ms |
315884 KB |
Output is correct |
15 |
Correct |
604 ms |
136160 KB |
Output is correct |
16 |
Correct |
3042 ms |
396060 KB |
Output is correct |
17 |
Correct |
2344 ms |
322388 KB |
Output is correct |
18 |
Correct |
612 ms |
122696 KB |
Output is correct |
19 |
Correct |
267 ms |
94348 KB |
Output is correct |
20 |
Correct |
716 ms |
135912 KB |
Output is correct |
21 |
Correct |
615 ms |
121240 KB |
Output is correct |
22 |
Correct |
1813 ms |
254024 KB |
Output is correct |
23 |
Correct |
1822 ms |
255648 KB |
Output is correct |
24 |
Correct |
2823 ms |
331928 KB |
Output is correct |
25 |
Correct |
2845 ms |
330824 KB |
Output is correct |
26 |
Correct |
2655 ms |
333360 KB |
Output is correct |
27 |
Correct |
3364 ms |
404784 KB |
Output is correct |
28 |
Correct |
646 ms |
157164 KB |
Output is correct |
29 |
Correct |
2572 ms |
326612 KB |
Output is correct |
30 |
Correct |
3089 ms |
327096 KB |
Output is correct |
31 |
Correct |
2695 ms |
332556 KB |
Output is correct |
32 |
Correct |
694 ms |
134412 KB |
Output is correct |
33 |
Correct |
252 ms |
99436 KB |
Output is correct |
34 |
Correct |
424 ms |
111772 KB |
Output is correct |
35 |
Correct |
467 ms |
116624 KB |
Output is correct |
36 |
Correct |
545 ms |
115472 KB |
Output is correct |
37 |
Correct |
566 ms |
114380 KB |
Output is correct |