답안 #1114782

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1114782 2024-11-19T15:24:28 Z _8_8_ Unique Cities (JOI19_ho_t5) C++17
0 / 100
212 ms 121572 KB
#include <bits/stdc++.h>
    
using namespace std;
    
typedef long long ll;
    
const int  N = (1 << 20) + 12, MOD = (int)1e9 + 7;

int n, m, a[N], vis[N], timer, p[N];
vector<int> g[N], gt[N];
bool is[N];
int f(int d) {
    timer++;
    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) {
                q.push(to);
                p[to] = v;
                vis[to] = timer;
            }
        }
    }
    int x = lst;
    timer++;
    vis[d] = timer;
    while(x != d) {
        vis[x] = timer;
        x = p[x];
    }
    return lst;
}
int dist[N], col[N], dep[N], mxd[N], res[N];
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]);
    }
}

vector<int> st;
set<int> cur[N];
void go(int v) {
    set<int> raz;
    for(int i = 0; i < (int)st.size() && mxd[v] - dep[v] < dep[v] - dep[st[i]]; i++) {
        raz.insert(a[st[i]]);
        cur[v].insert(a[st[i]]);
    }
    res[v] += (int)raz.size();
    if(gt[v].empty()) return;
    int sz = (int)gt[v].size();
    vector<int> p(sz), s(sz);
    p[0] = mxd[gt[v][0]];
    s[sz - 1] = mxd[gt[v][sz - 1]];
    for(int i = 1; i < sz; i++) {
        p[i] = max(p[i - 1], mxd[gt[v][i]]);
    }
    for(int i = sz - 1; i >= 0; i--) {
        s[i] = max(s[i + 1], mxd[gt[v][i]]);
    }
    for(int i = 0; i < sz; i++) {
        int to = gt[v][i];
        int mx = dep[v];
        if(i) mx = max(mx, p[i - 1]);
        if(i < sz - 1) mx = max(mx, s[i + 1]);
        mx -= dep[v];
        vector<int> bf = st;
        while(!st.empty() && dep[v] - dep[st.back()] <= mx) {
            st.pop_back();
        }
        st.push_back(v);
        go(to);
        st = bf;
    }
}
void solve(int root) {
    for(int i = 1; i <= n; i++) {
        gt[i].clear();
    }
    st.clear();
    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];
        if((int)g[i].size() == 1) {
            is[i] = 1;
        }
    }
    int d = f(1), d1 = f(d);

    solve(d);solve(d1);

    for(int i = 1; i <= n; i++) {
        // if(is[i] && i != d && i != d1) res[i]--;
        // cout << res[i] << '\n';
        cout << (int)cur[i].size() << '\n';
    }
    cout << '\n';

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

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

    while(t--) 
        test();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 100848 KB Output is correct
2 Incorrect 44 ms 101336 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 161 ms 112456 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 212 ms 121572 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 100848 KB Output is correct
2 Incorrect 44 ms 101336 KB Output isn't correct
3 Halted 0 ms 0 KB -