Submission #905962

# Submission time Handle Problem Language Result Execution time Memory
905962 2024-01-13T07:52:13 Z Amirreza_Fakhri Factories (JOI14_factories) C++17
100 / 100
5068 ms 374868 KB
// In the name of the God 
#include "factories.h"
#include <bits/stdc++.h>
#define ll long long
// #define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;

const ll inf = (1e9+7)*(1e9+7);
const ll mod = 998244353;
const ll maxn = 5e5+5;

ll n, sz[maxn];
ll mn[maxn];
bool mark[maxn];
vector <pair <ll, ll> > adj[maxn];
vector <pair <ll, ll> > c[maxn];

void dfs1(ll v, ll p) {
    sz[v] = 1;
    for (pair <ll, ll> e : adj[v]) {
        ll u = e.F;
        if (!mark[u] and u != p) {
            dfs1(u, v); sz[v] += sz[u];
        }
    }
}

ll dfs2(ll v, ll s, ll p) {
    for (pair <ll, ll> e : adj[v]) {
        ll u = e.F;
        if (!mark[u] and u != p and sz[u] > s/2) return dfs2(u, s, v);
    }
    return v;
}

void dfs3(ll v, ll h, ll p, ll b) {
    c[v].pb(mp(b, h));
    for (pair <ll, ll> e : adj[v]) {
        ll u = e.F;
        if (!mark[u] and u != p) dfs3(u, h+e.S, v, b);
    }
}

void cd(ll v) {
    dfs1(v, v);
    v = dfs2(v, sz[v], v);
    dfs3(v, 0, v, v);
    mark[v] = 1;
    for (pair <ll, ll> e : adj[v]) {
        ll u = e.F;
        if (!mark[u]) cd(u);
    }
}

void Init(int N, int A[], int B[], int D[]) {
    n = N;
    for (ll i = 0; i < n-1; i++) {
        adj[A[i]].pb(mp(B[i], D[i]));
        adj[B[i]].pb(mp(A[i], D[i]));
    }
    cd(0);
    fill(mn, mn+n, inf);
}

ll Query(int S, int X[], int T, int Y[]) {
    ll ans = inf;
    for (ll i = 0; i < S; i++) {
        ll v = X[i];
        for (pair <ll, ll> p : c[v]) smin(mn[p.F], p.S);
    }
    for (ll i = 0; i < T; i++) {
        ll v = Y[i];
        for (pair <ll, ll> p : c[v]) smin(ans, mn[p.F]+p.S);
    }
    for (ll i = 0; i < S; i++) {
        ll v = X[i];
        for (pair <ll, ll> p : c[v]) mn[p.F] = inf;
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 45656 KB Output is correct
2 Correct 195 ms 50848 KB Output is correct
3 Correct 235 ms 51376 KB Output is correct
4 Correct 236 ms 60712 KB Output is correct
5 Correct 225 ms 61160 KB Output is correct
6 Correct 177 ms 60020 KB Output is correct
7 Correct 205 ms 60828 KB Output is correct
8 Correct 234 ms 60860 KB Output is correct
9 Correct 232 ms 61092 KB Output is correct
10 Correct 154 ms 59736 KB Output is correct
11 Correct 210 ms 60756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 45868 KB Output is correct
2 Correct 2712 ms 208324 KB Output is correct
3 Correct 3842 ms 280120 KB Output is correct
4 Correct 883 ms 120676 KB Output is correct
5 Correct 4777 ms 372444 KB Output is correct
6 Correct 3881 ms 298324 KB Output is correct
7 Correct 900 ms 97788 KB Output is correct
8 Correct 252 ms 74000 KB Output is correct
9 Correct 993 ms 110996 KB Output is correct
10 Correct 965 ms 98336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 45656 KB Output is correct
2 Correct 195 ms 50848 KB Output is correct
3 Correct 235 ms 51376 KB Output is correct
4 Correct 236 ms 60712 KB Output is correct
5 Correct 225 ms 61160 KB Output is correct
6 Correct 177 ms 60020 KB Output is correct
7 Correct 205 ms 60828 KB Output is correct
8 Correct 234 ms 60860 KB Output is correct
9 Correct 232 ms 61092 KB Output is correct
10 Correct 154 ms 59736 KB Output is correct
11 Correct 210 ms 60756 KB Output is correct
12 Correct 14 ms 45868 KB Output is correct
13 Correct 2712 ms 208324 KB Output is correct
14 Correct 3842 ms 280120 KB Output is correct
15 Correct 883 ms 120676 KB Output is correct
16 Correct 4777 ms 372444 KB Output is correct
17 Correct 3881 ms 298324 KB Output is correct
18 Correct 900 ms 97788 KB Output is correct
19 Correct 252 ms 74000 KB Output is correct
20 Correct 993 ms 110996 KB Output is correct
21 Correct 965 ms 98336 KB Output is correct
22 Correct 3115 ms 229648 KB Output is correct
23 Correct 2921 ms 233300 KB Output is correct
24 Correct 4513 ms 302188 KB Output is correct
25 Correct 4350 ms 304868 KB Output is correct
26 Correct 4206 ms 303624 KB Output is correct
27 Correct 5068 ms 374868 KB Output is correct
28 Correct 949 ms 127244 KB Output is correct
29 Correct 4291 ms 302768 KB Output is correct
30 Correct 4166 ms 302812 KB Output is correct
31 Correct 3929 ms 302676 KB Output is correct
32 Correct 1287 ms 111012 KB Output is correct
33 Correct 260 ms 74176 KB Output is correct
34 Correct 700 ms 90968 KB Output is correct
35 Correct 635 ms 91988 KB Output is correct
36 Correct 860 ms 96784 KB Output is correct
37 Correct 809 ms 96872 KB Output is correct