/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 4ms 532.0 KiB
#2 Accepted 143ms 1.102 MiB
#3 Accepted 145ms 576.0 KiB
#4 Accepted 135ms 672.0 KiB
#5 Accepted 153ms 1.25 MiB
#6 Accepted 148ms 2.77 MiB
#7 Accepted 222ms 16.52 MiB
#8 Accepted 228ms 16.461 MiB
#9 Accepted 220ms 16.52 MiB
#10 Accepted 245ms 16.52 MiB
#11 Accepted 222ms 16.469 MiB
#12 Accepted 225ms 16.52 MiB

Code

/*
 *   Copyright (c) 2024 Emon Thakur
 *   All rights reserved.
 */
#include<bits/stdc++.h>
using namespace std;
vector<int> g[100005];
int dp[100005][26];
string s; 
int parent[100005];

void dpontree(int node,int par)
{
    dp[node][s[node-1]-'a']++;
    parent[node] = par;
    for(auto e:g[node])
    {
        if(e == par) continue;
        dpontree(e,node);
    }
    for(auto e:g[node])
    {
        if(e == par) continue;
        for(int i=0;i<26;i++)  dp[node][i] += dp[e][i];
    }
}

int main()
{
    int n; cin >> n;
    char cc; 
    for(int i=0;i<n;i++)
    {
        cin >> cc;
        s.push_back(cc);
    }
    for(int i=0;i<n-1;i++)
    {
        int u,v; cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dpontree(1,0);
    //for(int i=1;i<=n;i++) cout<<i<<" "<<parent[i]<<endl; cout<<endl;
    
    int q; cin >> q;
    while(q--)
    {
        int type,x,nd,cnt=0,ind;
        char c,prevc;
        cin >> type;

        if(type==1)
        {
            cin >> nd >> c;
            prevc = s[nd-1];
            s[nd-1] = c;

            while(nd != 0)
            {
                dp[nd][prevc-'a']--;
                dp[nd][c-'a']++;
                nd = parent[nd];
            }

            continue;
        }
        cin >> x;
        for(int i=25;i>=0;i--)
        {
            if(cnt <= dp[x][i]) 
            {
                cnt = dp[x][i];
                ind = i;
            }
        }
        cout << (char) ('a' + ind) <<endl;
    }
}

Information

Submit By
Type
Submission
Problem
P1091 Alphabetical Kingdom
Language
C++20 (G++ 13.2.0)
Submit At
2024-08-24 21:15:58
Judged At
2024-08-24 21:15:58
Judged By
Score
100
Total Time
245ms
Peak Memory
16.52 MiB