Submission #922259

# Submission time Handle Problem Language Result Execution time Memory
922259 2024-02-05T10:02:46 Z Beerus13 Factories (JOI14_factories) C++14
100 / 100
4351 ms 368464 KB
#include <bits/stdc++.h>
// #include "factories.h"
using namespace std;
#define ll long long
#define pii pair<int, ll>
const int N = 5e5 + 5;
const ll inf = 1e15;
 
int n, q, S, T, centroid;
int sz[N], vt[N];
vector<pii> g[N], ancestors[N];
bool del[N];
ll dp[N];

int dfs_size(int u, int p = 0) {
    sz[u] = 1;
    for(auto [v, w] : g[u]) if(v != p && !del[v]) {
        sz[u] += dfs_size(v, u);
    }
    return sz[u];
}
 
int find_centroid(int u, int p, int num) {
    for(auto [v, w] : g[u]) if(v != p && !del[v] && sz[v] > num / 2) return find_centroid(v, u, num);
    return u;
}
 
void dfs_centroid(int u, int par, ll dis) {
    for(auto [v, w] : g[u]) if(v != par && !del[v]) dfs_centroid(v, u, dis + w);
    ancestors[u].emplace_back(centroid, dis);
}

void decomposition(int u) {
    centroid = find_centroid(u, 0, dfs_size(u));
    del[centroid] = 1;
    dfs_centroid(centroid, centroid, 0);
    for(auto [v, w] : g[centroid]) {
        if(!del[v]) decomposition(v);
    }
}
 
void add(int u) {
    for(auto [v, w] : ancestors[u]) dp[v] = min(dp[v], w);
}
 
void remove(int u) {
    for(auto [v, w] : ancestors[u]) dp[v] = inf;
}
 
ll get(int u) {
    ll ans = inf;
    for(auto [v, w] : ancestors[u]) ans = min(ans, w + dp[v]);
    return ans;
}

void Init(int N, int A[], int B[], int D[]) {
    for(int i = 0; i < N - 1; ++i) {
        A[i]++; B[i]++;
        g[A[i]].emplace_back(B[i], D[i]);
        g[B[i]].emplace_back(A[i], D[i]);
    }
    for(int i = 1; i <= N; ++i) dp[i] = inf;
    decomposition(1);
}
 
ll Query(int S, int X[], int T, int Y[]) {
	for (int i = 0; i < S; i++)
		add(X[i] + 1);
 
	ll ans = 1e15;
	for (int i = 0; i < T; i++)
		ans = min(ans, get(Y[i] + 1));
 
	for (int i = 0; i < S; i++)
		remove(X[i] + 1);
	return ans;
}

// https://oj.uz/problem/view/JOI14_factories

Compilation message

factories.cpp: In function 'int dfs_size(int, int)':
factories.cpp:17:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   17 |     for(auto [v, w] : g[u]) if(v != p && !del[v]) {
      |              ^
factories.cpp: In function 'int find_centroid(int, int, int)':
factories.cpp:24:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   24 |     for(auto [v, w] : g[u]) if(v != p && !del[v] && sz[v] > num / 2) return find_centroid(v, u, num);
      |              ^
factories.cpp: In function 'void dfs_centroid(int, int, long long int)':
factories.cpp:29:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   29 |     for(auto [v, w] : g[u]) if(v != par && !del[v]) dfs_centroid(v, u, dis + w);
      |              ^
factories.cpp: In function 'void decomposition(int)':
factories.cpp:37:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   37 |     for(auto [v, w] : g[centroid]) {
      |              ^
factories.cpp: In function 'void add(int)':
factories.cpp:43:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   43 |     for(auto [v, w] : ancestors[u]) dp[v] = min(dp[v], w);
      |              ^
factories.cpp: In function 'void remove(int)':
factories.cpp:47:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   47 |     for(auto [v, w] : ancestors[u]) dp[v] = inf;
      |              ^
factories.cpp: In function 'long long int get(int)':
factories.cpp:52:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   52 |     for(auto [v, w] : ancestors[u]) ans = min(ans, w + dp[v]);
      |              ^
# Verdict Execution time Memory Grader output
1 Correct 14 ms 45912 KB Output is correct
2 Correct 202 ms 55632 KB Output is correct
3 Correct 219 ms 56116 KB Output is correct
4 Correct 241 ms 56100 KB Output is correct
5 Correct 226 ms 56404 KB Output is correct
6 Correct 160 ms 55032 KB Output is correct
7 Correct 224 ms 56308 KB Output is correct
8 Correct 214 ms 56144 KB Output is correct
9 Correct 223 ms 56488 KB Output is correct
10 Correct 154 ms 55136 KB Output is correct
11 Correct 212 ms 55888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 45656 KB Output is correct
2 Correct 2024 ms 218040 KB Output is correct
3 Correct 3115 ms 287344 KB Output is correct
4 Correct 775 ms 116436 KB Output is correct
5 Correct 4351 ms 368464 KB Output is correct
6 Correct 3494 ms 296312 KB Output is correct
7 Correct 909 ms 98624 KB Output is correct
8 Correct 267 ms 74688 KB Output is correct
9 Correct 922 ms 110948 KB Output is correct
10 Correct 782 ms 98984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 45912 KB Output is correct
2 Correct 202 ms 55632 KB Output is correct
3 Correct 219 ms 56116 KB Output is correct
4 Correct 241 ms 56100 KB Output is correct
5 Correct 226 ms 56404 KB Output is correct
6 Correct 160 ms 55032 KB Output is correct
7 Correct 224 ms 56308 KB Output is correct
8 Correct 214 ms 56144 KB Output is correct
9 Correct 223 ms 56488 KB Output is correct
10 Correct 154 ms 55136 KB Output is correct
11 Correct 212 ms 55888 KB Output is correct
12 Correct 10 ms 45656 KB Output is correct
13 Correct 2024 ms 218040 KB Output is correct
14 Correct 3115 ms 287344 KB Output is correct
15 Correct 775 ms 116436 KB Output is correct
16 Correct 4351 ms 368464 KB Output is correct
17 Correct 3494 ms 296312 KB Output is correct
18 Correct 909 ms 98624 KB Output is correct
19 Correct 267 ms 74688 KB Output is correct
20 Correct 922 ms 110948 KB Output is correct
21 Correct 782 ms 98984 KB Output is correct
22 Correct 2335 ms 226852 KB Output is correct
23 Correct 2551 ms 230724 KB Output is correct
24 Correct 3657 ms 299512 KB Output is correct
25 Correct 3627 ms 302076 KB Output is correct
26 Correct 3565 ms 300776 KB Output is correct
27 Correct 4251 ms 367384 KB Output is correct
28 Correct 942 ms 121304 KB Output is correct
29 Correct 3552 ms 296908 KB Output is correct
30 Correct 3605 ms 296696 KB Output is correct
31 Correct 3476 ms 297056 KB Output is correct
32 Correct 1174 ms 111184 KB Output is correct
33 Correct 310 ms 74700 KB Output is correct
34 Correct 547 ms 91752 KB Output is correct
35 Correct 570 ms 92648 KB Output is correct
36 Correct 830 ms 97676 KB Output is correct
37 Correct 879 ms 97764 KB Output is correct