/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 4ms 580.0 KiB
#2 Accepted 4ms 632.0 KiB
#3 Accepted 4ms 532.0 KiB
#4 Accepted 4ms 712.0 KiB
#5 Accepted 5ms 996.0 KiB
#6 Accepted 5ms 996.0 KiB
#7 Accepted 298ms 4.746 MiB
#8 Accepted 317ms 4.695 MiB
#9 Accepted 320ms 4.848 MiB
#10 Accepted 318ms 4.828 MiB
#11 Accepted 4ms 580.0 KiB
#12 Accepted 4ms 636.0 KiB
#13 Accepted 4ms 576.0 KiB
#14 Accepted 316ms 4.676 MiB

Code

#include<bits/stdc++.h>
using namespace std;
const long long M=2e5+10,MOD=2000000007;
typedef long long ll;
vector<int>edge[M];
int par[M];
int pos[1001][1001];
int counti[10001];
int vis[10001];
int maximumlenght[10001];
void dfs(int x){
  vis[x]=1;
  for(int id:edge[x]){
    if(!vis[id]){
        par[id]=x;
        dfs(id);
    }
  }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t=1;
   // cin>>t;
    while(t--){
    int n;
    cin>>n;
    string s;
    cin>>s;
    for(int i=1;i<n;i++){
        int x,y;
        cin>>x>>y;
        edge[x].push_back(y);
        edge[y].push_back(x);
    }
    par[1]=1;
    dfs(1);
    for(int i=1;i<=n;i++){
        vector<int>v;
        v.push_back(i);
        int x=i;
        while(par[x]!=x){
            x=par[x];
            v.push_back(x);
           // x=par[x];
        }
        reverse(v.begin(),v.end());
        //for(int k:v)cout<<k<<" ";
           // cout<<endl;
        int l=1,r=v.size();
        maximumlenght[i]=v.size();
        int cnt=0;
        while(l<=r){
           if(l==r){
             pos[i][v[l-1]]=v[l-1];
             cnt++;
           }
           else{
            if(s[v[l-1]-1]==s[v[r-1]-1]){
                cnt+=2;
            }
            pos[i][v[l-1]]=v[r-1];
            pos[i][v[r-1]]=v[l-1];
           }
           l++;
           r--;
        }
        counti[i]=cnt;

    }
    int q;
    cin>>q;
    while(q--){
        int node;
        char ch;
        cin>>node>>ch;
        int res=1;
        for(int i=1;i<=n;i++){
            int x=pos[i][node];
            //cout<<i<<" ";
            if(x>0 && x!=node){
              if(s[node-1]==s[x-1]){
                counti[i]--;
                if(node!=x)counti[i]--;
              }
              if(ch==s[x-1]){
                counti[i]++;
                if(node!=x)counti[i]++;
              }
            }
           // cout<<counti[i]<<endl;
            if(counti[i]==maximumlenght[i])res=max(res,counti[i]);
        }
        s[node-1]=ch;
     //  cout<<s<<endl;
        cout<<res<<"\n";
    }





    
   }
   return 0;
 
}

Information

Submit By
Type
Submission
Problem
P1045 Largest palindrome in tree
Language
C++20 (G++ 13.2.0)
Submit At
2024-03-26 17:26:53
Judged At
2024-10-03 13:58:44
Judged By
Score
100
Total Time
320ms
Peak Memory
4.848 MiB