//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]) {
cout << j << ' ' << i << '\n';
ans = min(ans, query2(j, i));
}
}
cout << ans - 1 << '\n';
return 0;}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
39260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
39260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
119 ms |
60240 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
39260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |