답안 #1115921

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1115921 2024-11-21T05:07:49 Z _8_8_ Unique Cities (JOI19_ho_t5) C++17
36 / 100
792 ms 274432 KB
#include <bits/stdc++.h>
    
using namespace std;
    
typedef long long ll;

const int  N = 2e5 + 12, MOD = (int)1e9 + 7;

int n, m, a[N];
bool vis[N], timer;
vector<int> g[N], gt[N];
vector<vector<int>> dist;
bool zap;
int f(int d) {
    vector<int> D(n + 1, 0);
    timer = 1;
    for(int i = 1; i <= n; i++) {
        vis[i] = 0;
    }
    vis[d] = timer;
    int lst = d;
    queue<int> q;
    q.push(d);
    while(!q.empty()) {
        int v = q.front();
        lst = v;
        q.pop();
        for(int to : g[v]) {
            if(vis[to] != timer) {
                D[to] = D[v] + 1;
                q.push(to);
                vis[to] = timer;
            }
        }
    }
    dist.push_back(D);
    return lst;
}
int dep[N], mxd[N], res[N], d, d1, o, cr;
void dfs(int v, int pr = -1) {
    mxd[v] = dep[v];
    for(int to:g[v]) if(to != pr) {
        gt[v].push_back(to);
        dep[to] = dep[v] + 1;
        dfs(to, v);
        mxd[v] = max(mxd[v], mxd[to]);
    }
    for(int i = 0; i < (int)gt[v].size(); i++) {
        if(mxd[gt[v][i]] == mxd[v]) {
            swap(gt[v][0], gt[v][i]);
        }
    }
}
int b = 17, it = 0, cur = 0, ver[N];
int up[N][18];
struct node{
    node *l = 0, *r = 0;
    int sum = 0;
    node(){};
    node(int v) {
        sum = v;
    }
    node (node *L, node *R) {
        l = L;
        r = R;
        sum = l->sum + r->sum;
    }
};
using pnode = node *;
pnode tr[N];    
pnode build(int tl = 1, int tr = m) {
    if(tl == tr) {
        return new node();
    }
    int tm = (tl + tr) >> 1;
    return new node(build(tl, tm), build(tm + 1, tr));
}
pnode upd(int pos, pnode v, int tl = 1, int tr = m) {
    if(tl == tr) {
        return new node(1);
    }
    int tm = (tl + tr) >> 1;
    if(pos <= tm) 
        return new node(upd(pos, v->l, tl, tm), v->r);
    return new node(v->l, upd(pos, v->r, tm + 1, tr));
}
void go(int v) {
    if(dist[cr][v] > dist[o][v] || (dist[cr][v] == dist[o][v] && zap)) {
        int f = cur, val = -mxd[v] + dep[v] * 2;
        set<int> r;
        if(dep[ver[f]] >= val) {
            for(int i = b - 1; i >= 0; i--) {
                int nv = ver[up[f][i]];
                if(dep[nv] >= val) {
                    f = up[f][i];
                }
            }
            f = up[f][0];
        }
        res[v] += tr[f]->sum;
    }
    if(gt[v].empty()) return;
    int sz = (int)gt[v].size(), mx1 = dep[v];
    vector<int> s(sz);
    s[sz - 1] = mxd[gt[v][sz - 1]];
    for(int i = 1; i < sz; i++) {
        mx1 = max(mx1, mxd[gt[v][i]]);
    }
    for(int i = sz - 2; i >= 0; i--) {
        s[i] = max(s[i + 1], mxd[gt[v][i]]);
    }
    vector<int> del;
    int bf = cur;
    for(int i = 0; i < sz; i++) {
        int to = gt[v][i];
        int mx = dep[v];
        if(i) mx = mxd[gt[v][0]];
        if(i < sz - 1) mx = max(mx, mx1);
        mx -= dep[v];
        if(i == 1) {
            cur = up[cur][0];
        }
        if(i <= 1 && dep[v] - dep[ver[cur]] <= mx) {
            for(int i = b - 1; i >= 0; i--) {
                int nv = up[cur][i];
                if(dep[v] - dep[ver[nv]] <= mx) {
                    cur = nv;
                }
            }
            cur = up[cur][0];
        }
        if(i <= 1) {
            it++;
            ver[it] = v;
            up[it][0] = cur;
            tr[it] = upd(a[v], tr[cur]);
            for(int i = 1; i < b; i++) {
                up[it][i] = up[up[it][i - 1]][i - 1];
            }
            cur = it;
        }
        go(to);
    }
    cur = bf;
}
void solve(int root) {
    it = cur = 0;
    for(int i = 1; i <= n; i++) {
        gt[i].clear();
    }
    ver[0] = 0;
    dep[0] = -(int)1e9;
    tr[0] = build();
    dep[root] = 1;
    dfs(root);

    go(root);

}
void test() {
    cin >> n >> m;
    for(int i = 1; i <= n - 1; i++) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }

    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    d = f(1), d1 = f(d);
    f(d1);
    o = 2;cr = 1;
    solve(d);
    zap = 1;
    o = 1;cr = 2;
    solve(d1);
    for(int i = 1; i <= n; i++) {
        cout << res[i] << '\n';
    }

}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int t = 1; 
    // cin >> t;

    while(t--) 
        test();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 16720 KB Output is correct
2 Correct 6 ms 18168 KB Output is correct
3 Correct 5 ms 17744 KB Output is correct
4 Correct 6 ms 18160 KB Output is correct
5 Correct 5 ms 17232 KB Output is correct
6 Correct 5 ms 17916 KB Output is correct
7 Correct 7 ms 17488 KB Output is correct
8 Correct 5 ms 17232 KB Output is correct
9 Correct 5 ms 17488 KB Output is correct
10 Correct 5 ms 17232 KB Output is correct
11 Correct 5 ms 17500 KB Output is correct
12 Correct 4 ms 16976 KB Output is correct
13 Correct 6 ms 17912 KB Output is correct
14 Correct 5 ms 17232 KB Output is correct
15 Correct 6 ms 17488 KB Output is correct
16 Correct 4 ms 16976 KB Output is correct
17 Correct 5 ms 17488 KB Output is correct
18 Correct 5 ms 17216 KB Output is correct
19 Correct 9 ms 18000 KB Output is correct
20 Correct 7 ms 19024 KB Output is correct
21 Correct 8 ms 18768 KB Output is correct
22 Correct 6 ms 17744 KB Output is correct
23 Correct 6 ms 18512 KB Output is correct
24 Correct 6 ms 18000 KB Output is correct
25 Correct 7 ms 18120 KB Output is correct
26 Correct 5 ms 17232 KB Output is correct
27 Correct 7 ms 19024 KB Output is correct
28 Correct 6 ms 18256 KB Output is correct
29 Correct 6 ms 18512 KB Output is correct
30 Correct 3 ms 16976 KB Output is correct
31 Correct 5 ms 17940 KB Output is correct
32 Correct 7 ms 18424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 112 ms 40332 KB Output is correct
2 Correct 192 ms 66180 KB Output is correct
3 Correct 31 ms 25928 KB Output is correct
4 Correct 211 ms 56120 KB Output is correct
5 Correct 369 ms 101020 KB Output is correct
6 Correct 283 ms 79416 KB Output is correct
7 Correct 215 ms 52508 KB Output is correct
8 Correct 268 ms 59448 KB Output is correct
9 Correct 227 ms 58096 KB Output is correct
10 Correct 233 ms 58048 KB Output is correct
11 Correct 97 ms 34988 KB Output is correct
12 Correct 420 ms 81212 KB Output is correct
13 Correct 318 ms 70932 KB Output is correct
14 Correct 300 ms 72116 KB Output is correct
15 Correct 108 ms 32004 KB Output is correct
16 Correct 279 ms 79816 KB Output is correct
17 Correct 281 ms 74332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 471 ms 216888 KB Output is correct
2 Runtime error 792 ms 274432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 16720 KB Output is correct
2 Correct 6 ms 18168 KB Output is correct
3 Correct 5 ms 17744 KB Output is correct
4 Correct 6 ms 18160 KB Output is correct
5 Correct 5 ms 17232 KB Output is correct
6 Correct 5 ms 17916 KB Output is correct
7 Correct 7 ms 17488 KB Output is correct
8 Correct 5 ms 17232 KB Output is correct
9 Correct 5 ms 17488 KB Output is correct
10 Correct 5 ms 17232 KB Output is correct
11 Correct 5 ms 17500 KB Output is correct
12 Correct 4 ms 16976 KB Output is correct
13 Correct 6 ms 17912 KB Output is correct
14 Correct 5 ms 17232 KB Output is correct
15 Correct 6 ms 17488 KB Output is correct
16 Correct 4 ms 16976 KB Output is correct
17 Correct 5 ms 17488 KB Output is correct
18 Correct 5 ms 17216 KB Output is correct
19 Correct 9 ms 18000 KB Output is correct
20 Correct 7 ms 19024 KB Output is correct
21 Correct 8 ms 18768 KB Output is correct
22 Correct 6 ms 17744 KB Output is correct
23 Correct 6 ms 18512 KB Output is correct
24 Correct 6 ms 18000 KB Output is correct
25 Correct 7 ms 18120 KB Output is correct
26 Correct 5 ms 17232 KB Output is correct
27 Correct 7 ms 19024 KB Output is correct
28 Correct 6 ms 18256 KB Output is correct
29 Correct 6 ms 18512 KB Output is correct
30 Correct 3 ms 16976 KB Output is correct
31 Correct 5 ms 17940 KB Output is correct
32 Correct 7 ms 18424 KB Output is correct
33 Correct 112 ms 40332 KB Output is correct
34 Correct 192 ms 66180 KB Output is correct
35 Correct 31 ms 25928 KB Output is correct
36 Correct 211 ms 56120 KB Output is correct
37 Correct 369 ms 101020 KB Output is correct
38 Correct 283 ms 79416 KB Output is correct
39 Correct 215 ms 52508 KB Output is correct
40 Correct 268 ms 59448 KB Output is correct
41 Correct 227 ms 58096 KB Output is correct
42 Correct 233 ms 58048 KB Output is correct
43 Correct 97 ms 34988 KB Output is correct
44 Correct 420 ms 81212 KB Output is correct
45 Correct 318 ms 70932 KB Output is correct
46 Correct 300 ms 72116 KB Output is correct
47 Correct 108 ms 32004 KB Output is correct
48 Correct 279 ms 79816 KB Output is correct
49 Correct 281 ms 74332 KB Output is correct
50 Correct 471 ms 216888 KB Output is correct
51 Runtime error 792 ms 274432 KB Execution killed with signal 9
52 Halted 0 ms 0 KB -