답안 #947422

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
947422 2024-03-16T06:59:45 Z weakweakweak 수도 (JOI20_capital_city) C++17
0 / 100
111 ms 62804 KB
//30分真的就那麼水嗎?三小。先寫下來當練實作,等下再想,想不到就去抄解
#include <bits/stdc++.h>
using namespace std;

int lt[510000], rt[510000], cnt[510000], n, k, col[510000], revlt[510000], now = 0, l[510000], r[510000];
int pos[510000] = {0};

vector<int> e[510000];
vector<int> qs[510000];

int a[510000] = {0};
void update (int i, int val) {
    for (; i <= n; i += i & -i) a[i] += val;
}
int query (int i) {
    int res = 0;
    for (; i > 0; i -= i & -i) res += a[i];
    return res;
}
int query2 (int l, int r) {return query(r) - query(l - 1);}


void dfs (int i, int par) {
    lt[i] = ++now, revlt[now] = i;
    for (int j : e[i]) if (j != par) dfs(j, i);
    rt[i] = now;
}

int main () {
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> n >> k;
    for (int i = 1; i < n; i++) {
        int x, y;
        cin >> x >> y;
        e[x].push_back(y);
        e[y].push_back(x);
    }
    int rt = 1;
    for (int i = 1; i <= n; i++) if (e[i].size() == 1) rt = i;
    dfs(rt, rt);

    for (int i = 1; i <= n; i++) {
        cin >> col[lt[i]];
        if (cnt[col[lt[i]]]++ == 0) l[col[lt[i]]] = r[col[lt[i]]] = lt[i];
        else l[col[lt[i]]] = min(l[col[lt[i]]], lt[i]),  r[col[lt[i]]] = max(r[col[lt[i]]], lt[i]); 
    }

    for (int i = 1; i <= k; i++) qs[r[i]].push_back(l[i]);

    int ans = k;
    for (int i = 1; i <= n; i++) {
        if (pos[col[i]]) update(pos[col[i]], -1);
        pos[col[i]] = i;
        update(i, 1);
        for (int j : qs[i]) {
            ans = min(ans, query2(j, i));
        }
    }

    cout << ans - 1 << '\n';
return 0;}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 39256 KB Output is correct
2 Correct 8 ms 39256 KB Output is correct
3 Correct 8 ms 39260 KB Output is correct
4 Correct 8 ms 39260 KB Output is correct
5 Correct 7 ms 39260 KB Output is correct
6 Correct 8 ms 39456 KB Output is correct
7 Incorrect 9 ms 39260 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 39256 KB Output is correct
2 Correct 8 ms 39256 KB Output is correct
3 Correct 8 ms 39260 KB Output is correct
4 Correct 8 ms 39260 KB Output is correct
5 Correct 7 ms 39260 KB Output is correct
6 Correct 8 ms 39456 KB Output is correct
7 Incorrect 9 ms 39260 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 58936 KB Output is correct
2 Correct 76 ms 62788 KB Output is correct
3 Correct 95 ms 62800 KB Output is correct
4 Correct 74 ms 62804 KB Output is correct
5 Correct 96 ms 62384 KB Output is correct
6 Correct 75 ms 62548 KB Output is correct
7 Correct 97 ms 62288 KB Output is correct
8 Correct 73 ms 62120 KB Output is correct
9 Incorrect 111 ms 61028 KB Output isn't correct
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 39256 KB Output is correct
2 Correct 8 ms 39256 KB Output is correct
3 Correct 8 ms 39260 KB Output is correct
4 Correct 8 ms 39260 KB Output is correct
5 Correct 7 ms 39260 KB Output is correct
6 Correct 8 ms 39456 KB Output is correct
7 Incorrect 9 ms 39260 KB Output isn't correct
8 Halted 0 ms 0 KB -