Submission #922258

# Submission time Handle Problem Language Result Execution time Memory
922258 2024-02-05T10:00:51 Z Beerus13 Factories (JOI14_factories) C++14
100 / 100
4462 ms 372804 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 45832 KB Output is correct
2 Correct 235 ms 60224 KB Output is correct
3 Correct 233 ms 60740 KB Output is correct
4 Correct 217 ms 60500 KB Output is correct
5 Correct 236 ms 60912 KB Output is correct
6 Correct 157 ms 59608 KB Output is correct
7 Correct 220 ms 60656 KB Output is correct
8 Correct 222 ms 60736 KB Output is correct
9 Correct 261 ms 61096 KB Output is correct
10 Correct 158 ms 59732 KB Output is correct
11 Correct 219 ms 60536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 45660 KB Output is correct
2 Correct 2041 ms 227008 KB Output is correct
3 Correct 2944 ms 298160 KB Output is correct
4 Correct 737 ms 120788 KB Output is correct
5 Correct 3476 ms 372804 KB Output is correct
6 Correct 3131 ms 298156 KB Output is correct
7 Correct 754 ms 99812 KB Output is correct
8 Correct 321 ms 76188 KB Output is correct
9 Correct 1002 ms 112468 KB Output is correct
10 Correct 846 ms 100492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 45832 KB Output is correct
2 Correct 235 ms 60224 KB Output is correct
3 Correct 233 ms 60740 KB Output is correct
4 Correct 217 ms 60500 KB Output is correct
5 Correct 236 ms 60912 KB Output is correct
6 Correct 157 ms 59608 KB Output is correct
7 Correct 220 ms 60656 KB Output is correct
8 Correct 222 ms 60736 KB Output is correct
9 Correct 261 ms 61096 KB Output is correct
10 Correct 158 ms 59732 KB Output is correct
11 Correct 219 ms 60536 KB Output is correct
12 Correct 11 ms 45660 KB Output is correct
13 Correct 2041 ms 227008 KB Output is correct
14 Correct 2944 ms 298160 KB Output is correct
15 Correct 737 ms 120788 KB Output is correct
16 Correct 3476 ms 372804 KB Output is correct
17 Correct 3131 ms 298156 KB Output is correct
18 Correct 754 ms 99812 KB Output is correct
19 Correct 321 ms 76188 KB Output is correct
20 Correct 1002 ms 112468 KB Output is correct
21 Correct 846 ms 100492 KB Output is correct
22 Correct 2422 ms 229640 KB Output is correct
23 Correct 2432 ms 233356 KB Output is correct
24 Correct 3737 ms 302332 KB Output is correct
25 Correct 3771 ms 304920 KB Output is correct
26 Correct 3612 ms 303472 KB Output is correct
27 Correct 4462 ms 372796 KB Output is correct
28 Correct 935 ms 127100 KB Output is correct
29 Correct 3565 ms 302720 KB Output is correct
30 Correct 3596 ms 302572 KB Output is correct
31 Correct 3679 ms 303092 KB Output is correct
32 Correct 1043 ms 112512 KB Output is correct
33 Correct 263 ms 76776 KB Output is correct
34 Correct 618 ms 93124 KB Output is correct
35 Correct 551 ms 93776 KB Output is correct
36 Correct 762 ms 99028 KB Output is correct
37 Correct 826 ms 99024 KB Output is correct