# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
221460 | patrikpavic2 | Capital City (JOI20_capital_city) | C++17 | 1273 ms | 37216 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <cstdio>
#include <vector>
#include <queue>
#define PB push_back
using namespace std;
const int N = 2e5 + 500;
int sub[N], uzeo[N], C[N], cnt[N], zabr[N], par[N], centr[N], kol[N], uk[N];
int fin = N, n;
vector < int > moji, koji[N], v[N];
void dfs(int x, int lst){
sub[x] = 1; moji.PB(x);
par[x] = lst;
for(int y : v[x]){
if(centr[y] || lst == y) continue;
dfs(y, x); sub[x] += sub[y];
}
}
int nadi(int x){
dfs(x, x);
int nn = (int)moji.size();
int dos = x;
for(int y : moji){
if(2 * sub[y] >= nn && sub[y] < sub[dos])
dos = y;
}
moji.clear();
return dos;
}
void rijesi(int x){
dfs(x, x);
queue < int > q; q.push(x);
for(int y : moji) koji[C[y]].PB(y);
for(;!q.empty();q.pop()){
if(uzeo[q.front()])
continue;
int cur = q.front();
while(!uzeo[cur]){
if(!kol[C[cur]]){
for(int y : koji[C[cur]])
q.push(y);
}
uzeo[cur] = 1; kol[C[cur]]++;
cur = par[cur];
}
}
for(int y : moji) koji[C[y]].clear(), uzeo[y] = 0;
int mogu = 1, ret = 0;
for(int y : moji){
if(!kol[C[y]]) continue;
if(kol[C[y]] < uk[C[y]]){
mogu = 0; kol[C[y]] = 0;
}
kol[C[y]] = 0, ret++;
}
if(mogu)
fin = min(fin, ret);
moji.clear();
}
void glupost(int x){
int cen = nadi(x);
rijesi(cen);
centr[cen] = 1;
for(int y : v[cen]){
if(!centr[y])
glupost(y);
}
}
int main(){
int bla; scanf("%d%d", &n, &bla);
for(int i = 1;i < n;i++){
int x, y; scanf("%d%d", &x, &y);
v[x].PB(y), v[y].PB(x);
}
for(int i = 1;i <= n;i++)
scanf("%d", C + i), uk[C[i]]++;
glupost(1);
printf("%d\n", fin - 1);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |