/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 588.0 KiB
#2 Accepted 3ms 2.574 MiB
#3 Accepted 3ms 584.0 KiB
#4 Accepted 3ms 516.0 KiB
#5 Accepted 4ms 984.0 KiB
#6 Accepted 5ms 4.574 MiB
#7 Accepted 471ms 4.863 MiB
#8 Accepted 460ms 5.266 MiB
#9 Accepted 473ms 4.836 MiB
#10 Accepted 470ms 4.852 MiB
#11 Accepted 3ms 540.0 KiB
#12 Accepted 3ms 584.0 KiB
#13 Accepted 3ms 680.0 KiB
#14 Accepted 430ms 4.836 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-11-11 03:40:11
Judged By
Score
100
Total Time
473ms
Peak Memory
5.266 MiB