QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#827161#3561. Capital CityRedDiamond#0 0ms3552kbC++202.2kb2024-12-22 19:59:282024-12-22 19:59:29

Judging History

This is the latest submission verdict.

  • [2024-12-22 19:59:29]
  • Judged
  • Verdict: 0
  • Time: 0ms
  • Memory: 3552kb
  • [2024-12-22 19:59:28]
  • Submitted

answer

#include <bits/stdc++.h>


using namespace std;


#define ll long long
#define ld long double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()


int n, k;
vector<vector<int>> g, anc;
vector<int> c, dep, par;


void dfs(int u, int p = 0) {
    par[u] = p;
    anc[0][u] = p;
    dep[u] = dep[p] + 1;
    for (auto v : g[u]) {
        if (v != p) {
            dfs(v, u);
        }
    }
}


int lca(int a, int b) {
    if (dep[a] < dep[b]) {
        swap(a, b);
    }
    for (int h = 19; h >= 0; h -= 1) {
        if (dep[a] - (1 << h) >= dep[b]) {
            a = anc[h][a];
        }
    }
    if (a == b) {
        return a;
    }
    for (int h = 19; h >= 0; h -= 1) {
        if (anc[h][a] != anc[h][b]) {
            a = anc[h][a];
            b = anc[h][b];
        }
    }
    return anc[0][a];
}


int32_t main() {
    if (1) {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
	}
	cin >> n >> k;
    g.resize(n + 1);
    for (int i = 1; i < n; i += 1) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dep.resize(n + 1);
    anc.assign(20, vector<int>(n + 1));
    par.resize(n + 1);
    dfs(1);
    for (int h = 1; h < 20; h += 1) {
        for (int i = 1; i <= n; i += 1) {
            anc[h][i] = anc[h - 1][anc[h - 1][i]];
        }
    }


    c.resize(n + 1);
    vector<vector<int>> occ(n + 1);
    for (int i = 1; i <= n; i += 1) {
        cin >> c[i];
        occ[c[i]].push_back(i);
    }
    vector<int> cnt(k + 1);
    int ans = k;
    for (int i = 1; i <= k; i += 1) {
        int x = occ[i][0];
        for (auto j : occ[i]) {
            x = lca(x, j);
        }
        int f = 0;
        for (auto j : occ[i]) {
            int u = j;
            while (u != x) {
                cnt[c[u]] += 1;
                if (cnt[c[u]] == 1) {
                    f += 1;
                }
                u = par[u];
            }
        }
        ans = min(ans, f - 1);
        for (int j = 1; j <= k; j += 1) {
            cnt[j] = 0;
        }
    }
    cout << ans << "\n";
    return 0;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3552kb

input:

20 3
16 10
10 3
18 2
4 5
8 6
11 12
2 14
1 2
6 3
1 11
1 4
7 20
3 2
9 7
3 13
15 19
5 7
17 6
12 15
2
2
1
1
1
2
2
1
3
3
1
3
1
3
2
2
1
2
2
3

output:

1

result:

wrong answer 1st lines differ - expected: '2', found: '1'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Time Limit Exceeded

Test #31:

score: 0
Time Limit Exceeded

input:

200000 100000
185785 19011
19011 181550
181550 117972
117972 192238
192238 137685
137685 10126
10126 193657
193657 130856
130856 119980
119980 37122
37122 24497
24497 162102
162102 104298
104298 61332
61332 103789
103789 71060
71060 54044
54044 12075
12075 55296
55296 70106
70106 27512
27512 190160
...

output:


result:


Subtask #4:

score: 0
Skipped

Dependency #1:

0%