/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 324.0 KiB
#2 Accepted 147ms 1.176 MiB
#3 Accepted 133ms 616.0 KiB
#4 Accepted 137ms 756.0 KiB
#5 Accepted 152ms 1.184 MiB
#6 Accepted 181ms 2.75 MiB
#7 Accepted 314ms 16.523 MiB
#8 Accepted 283ms 16.539 MiB
#9 Accepted 309ms 16.547 MiB
#10 Accepted 305ms 16.531 MiB
#11 Accepted 263ms 16.535 MiB
#12 Accepted 273ms 16.512 MiB
#13 Time Exceeded ≥1094ms ≥23.77 MiB
#14 Time Exceeded ≥1081ms ≥23.93 MiB

Code

/*
 *   Copyright (c) 2024 Emon Thakur
 *   All rights reserved.
 */
#include<bits/stdc++.h>
using namespace std;
vector<int> g[100005];
#define READ(f)          freopen(f, "r", stdin)
#define WRITE(f)         freopen(f, "w", stdout)
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()
{
    // READ("input12.txt");
   //  WRITE("output12.txt") ;
    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 23:33:37
Judged At
2024-11-11 03:05:24
Judged By
Score
60
Total Time
≥1094ms
Peak Memory
≥23.93 MiB