Submission #773554

# Submission time Handle Problem Language Result Execution time Memory
773554 2023-07-05T06:44:49 Z 1bin Unique Cities (JOI19_ho_t5) C++14
4 / 100
2000 ms 170684 KB
#include <bits/stdc++.h>

using namespace std;

#define all(v) v.begin(), v.end()
typedef long long ll;
const int NMAX = 2e5 + 5;
int n, m, C[NMAX], D[NMAX], a, b, mxd, ix, r1, r2, ans[NMAX], cur, cnt[NMAX], mx[NMAX][2], idx[NMAX][2];
vector<int> adj[NMAX];

void dfs2(int x, int p, int d){
    if(d > mxd) mxd = d, ix = x;
    for(int&nx : adj[x])
        if(nx != p) dfs2(nx, x, d + 1);
    return;
}

void go(int x, int p){
    mx[x][0] = mx[x][1] = 0;
    for(int& nx : adj[x])
        if(nx != p){
            go(nx, x);
            if(mx[nx][0] + 1 > mx[x][1]) mx[x][1] = mx[nx][0] + 1, idx[x][1] = nx;
            if(mx[x][0] < mx[x][1]){
                swap(mx[x][0], mx[x][1]);
                swap(idx[x][0], idx[x][1]);
            }
        }
    return;
}

void f(int x, int add){
    if(add){
        if(++cnt[C[x]] == 1) cur++;
    }
    else{
        if(--cnt[C[x]] == 0) cur--;
    }
    return;
}

stack<int> st;
void dfs(int x, int p, int d){
    stack<int> t;
    D[d] = x;
    
    auto calc = [&] (int mxd){
        while(st.size() && st.top() >= mxd){
            f(D[st.top()], 0);
            t.emplace(st.top()); st.pop();
        }
    };
    if(mx[x][0]){
        int nx = idx[x][0];
        if(mx[x][1]) calc(d - mx[x][1]);
        st.emplace(d); f(x, 1);
        dfs(nx, x, d + 1);
        st.pop(); f(x, 0);
        
        calc(d - mx[x][0]);
        for(int& nx : adj[x])
            if(nx != p && nx != idx[x][0]){
                st.emplace(d); f(x, 1);
                dfs(nx, x, d + 1);
                st.pop(); f(x, 0);
            }
    }
    ans[x] = max(ans[x], cur);
    while(t.size()){
        f(D[t.top()], 1);
        st.emplace(t.top()); t.pop();
    }
    return;
}

int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    
    cin >> n >> m;
    for(int i = 1; i < n; i++){
        cin >> a >> b;
        adj[a].emplace_back(b);
        adj[b].emplace_back(a);
    }    
    for(int i = 1; i <= n; i++) cin >> C[i];
    dfs2(1, -1, 0);
    r1 = ix;
    mxd = 0; dfs2(r1, -1, 0);
    r2 = ix;
    
    go(r1, -1); dfs(r1, -1, 0);
    go(r2, -1); dfs(r2, -1, 0);
    for(int i = 1; i <= n; i++) cout << ans[i] << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 4 ms 5204 KB Output is correct
3 Correct 11 ms 5808 KB Output is correct
4 Correct 11 ms 5844 KB Output is correct
5 Correct 5 ms 5204 KB Output is correct
6 Correct 27 ms 6740 KB Output is correct
7 Correct 10 ms 5936 KB Output is correct
8 Correct 5 ms 5176 KB Output is correct
9 Correct 4 ms 5332 KB Output is correct
10 Correct 3 ms 5332 KB Output is correct
11 Correct 4 ms 5232 KB Output is correct
12 Correct 3 ms 5204 KB Output is correct
13 Correct 24 ms 6724 KB Output is correct
14 Correct 5 ms 5716 KB Output is correct
15 Correct 5 ms 5716 KB Output is correct
16 Correct 4 ms 5168 KB Output is correct
17 Correct 15 ms 6140 KB Output is correct
18 Correct 10 ms 6000 KB Output is correct
19 Correct 5 ms 5196 KB Output is correct
20 Correct 31 ms 6808 KB Output is correct
21 Correct 11 ms 6004 KB Output is correct
22 Correct 5 ms 5204 KB Output is correct
23 Correct 4 ms 5332 KB Output is correct
24 Correct 4 ms 5332 KB Output is correct
25 Correct 4 ms 5304 KB Output is correct
26 Correct 4 ms 5204 KB Output is correct
27 Correct 14 ms 6420 KB Output is correct
28 Correct 13 ms 6140 KB Output is correct
29 Correct 8 ms 5812 KB Output is correct
30 Correct 4 ms 5096 KB Output is correct
31 Correct 16 ms 6268 KB Output is correct
32 Correct 11 ms 5972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 107 ms 12048 KB Output is correct
2 Execution timed out 2035 ms 104192 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 136 ms 15012 KB Output is correct
2 Execution timed out 2076 ms 170684 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 4 ms 5204 KB Output is correct
3 Correct 11 ms 5808 KB Output is correct
4 Correct 11 ms 5844 KB Output is correct
5 Correct 5 ms 5204 KB Output is correct
6 Correct 27 ms 6740 KB Output is correct
7 Correct 10 ms 5936 KB Output is correct
8 Correct 5 ms 5176 KB Output is correct
9 Correct 4 ms 5332 KB Output is correct
10 Correct 3 ms 5332 KB Output is correct
11 Correct 4 ms 5232 KB Output is correct
12 Correct 3 ms 5204 KB Output is correct
13 Correct 24 ms 6724 KB Output is correct
14 Correct 5 ms 5716 KB Output is correct
15 Correct 5 ms 5716 KB Output is correct
16 Correct 4 ms 5168 KB Output is correct
17 Correct 15 ms 6140 KB Output is correct
18 Correct 10 ms 6000 KB Output is correct
19 Correct 5 ms 5196 KB Output is correct
20 Correct 31 ms 6808 KB Output is correct
21 Correct 11 ms 6004 KB Output is correct
22 Correct 5 ms 5204 KB Output is correct
23 Correct 4 ms 5332 KB Output is correct
24 Correct 4 ms 5332 KB Output is correct
25 Correct 4 ms 5304 KB Output is correct
26 Correct 4 ms 5204 KB Output is correct
27 Correct 14 ms 6420 KB Output is correct
28 Correct 13 ms 6140 KB Output is correct
29 Correct 8 ms 5812 KB Output is correct
30 Correct 4 ms 5096 KB Output is correct
31 Correct 16 ms 6268 KB Output is correct
32 Correct 11 ms 5972 KB Output is correct
33 Correct 107 ms 12048 KB Output is correct
34 Execution timed out 2035 ms 104192 KB Time limit exceeded
35 Halted 0 ms 0 KB -