比赛 期末考试1 评测结果 AAAAAAAAAA
题目名称 Output Only 最终得分 100
用户昵称 2_16鸡扒拌面 运行时间 3.742 s
代码语言 C++ 内存使用 43.03 MiB
提交时间 2026-02-08 10:03:09
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 200005;
int N, k, Q;
int color[MAXN];
vector<int> ch[MAXN];
int pa[MAXN];
unordered_map<int,int> ccc[MAXN];
void dfs(int u,int p) {
    pa[u]=p;
    for (int v:ch[u]) {
        if (v==p) continue;
        dfs(v,u);
    }
}

int main() {
    freopen("tioj_outplay.in","r",stdin);
    freopen("tioj_outplay.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>N>>k>>Q;
    for (int i=1;i<=N;i++) cin>>color[i];
    for (int i=0;i<N-1;i++) 
    {
        int u,v;
        cin>>u>>v;
        ch[u].push_back(v);
        ch[v].push_back(u);
    }
    dfs(1,0);
    for (int i=2;i<=N;i++) 
    {
        int p=pa[i];
        ccc[p][color[i]]++;
    }
    int ans=(color[1]!=0)?1:0;
    for (int i = 2; i <= N; i++)
        if (color[pa[i]] != color[i])
            ans++;
    while (Q--) {
        int w,nc;
        cin >> w >> nc;
        int oc = color[w];
        if (oc == nc) {
            cout << ans << '\n';
            continue;
        }
        int de = 0;
        if (w == 1) {
            de += (nc != 0) - (oc != 0);
        } else {
            int pc=color[pa[w]];
            de+=(pc!=nc)-(pc!=oc);
        }
        int t=(oc!=nc)?1:0;
        int co=ccc[w][oc];
        int cn=ccc[w][nc];
        de+=t*(co-cn);
        ans+=de;
        color[w]=nc;
        if (w != 1) 
        {
            int p = pa[w];
            ccc[p][oc]--;
            if (ccc[p][oc] == 0)
                ccc[p].erase(oc);
            ccc[p][nc]++;
        }
        cout<<ans<<endl;
    }
    return 0;
}