Submission #922996

# Submission time Handle Problem Language Result Execution time Memory
922996 2024-02-06T12:12:43 Z ttamx Lampice (COCI19_lampice) C++14
17 / 110
5000 ms 25724 KB
#include<bits/stdc++.h>

using namespace std;

using ull = unsigned long long;
using pu = pair<ull,int>;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N=5e5+5;
const ull BASE=rng();

int n;
string s;
int a[N];
vector<int> adj[N];
ull hsh[26],pw[N],hsh2[N];
int sz[N];
bool used[N];
int len;
map<pu,bool> mp;

void init(){
    for(int i=1;i<=n;i++)used[i]=false;
}

int get_sz(int u,int p=0){
    sz[u]=1;
    for(auto v:adj[u])if(v!=p&&!used[v])sz[u]+=get_sz(v,u);
    return sz[u];
}

int centroid(int u,int cnt,int p=0){
    for(auto v:adj[u])if(v!=p&&!used[v]&&sz[v]>cnt/2)return centroid(v,cnt,u);
    return u;
}

bool dfs1(int u,int p,ull cur,ull rev,int sz){
    if(sz>len)return false;
    if(mp[pu(rev*pw[len-sz]-cur,len-sz)])return true;
    for(auto v:adj[u])if(v!=p&&!used[v]){
        if(dfs1(v,u,cur*BASE+a[v],rev+a[v]*pw[sz],sz+1)){
            return true;
        }
    }
    return false;
}

void dfs2(int u,int p,ull cur,ull rev,int sz){
    if(sz>len)return;
    mp[pu(rev*pw[len-sz]-cur,sz)]=true;
    for(auto v:adj[u])if(v!=p&&!used[v]){
        dfs2(v,u,cur*BASE+a[v],rev+a[v]*pw[sz],sz+1);
    }
}

bool decom(int u){
    u=centroid(u,get_sz(u));
    used[u]=true;
    mp.clear();
    mp[pu(0,0)]=true;
    for(auto v:adj[u])if(!used[v]){
        if(dfs1(v,u,a[u]*BASE+a[v],a[v]*BASE+a[u],2))return true;
        dfs2(v,u,a[v],a[v],1);
    }
    if(mp[pu(a[u]*pw[len-1]-a[u],len-1)])return true;
    for(auto v:adj[u])if(!used[v]&&decom(v))return true;
    return false;
}

int solve1(){
    int l=0,r=(n-1)/2;
    while(l<r){
        int m=(l+r+1)/2;
        len=m*2+1;
        init();
        if(decom(1))l=m;
        else r=m-1;
    }
    return l*2+1;
}

int solve2(){
    int l=0,r=n/2;
    while(l<r){
        int m=(l+r+1)/2;
        len=m*2;
        init();
        if(decom(1))l=m;
        else r=m-1;
    }
    return l*2;
}

int main(){
    cin.tie(nullptr)->sync_with_stdio(false);
    for(int i=0;i<26;i++)hsh[i]=rng();
    cin >> n >> s;
    pw[0]=1;
    for(int i=1;i<=n;i++)pw[i]=pw[i-1]*BASE;
    for(int i=1;i<=n;i++)a[i]=hsh[s[i-1]-'a'];
    for(int i=0;i<=n;i++)hsh2[i]=rng();
    for(int i=1;i<n;i++){
        int u,v;
        cin >> u >> v;
        adj[u].emplace_back(v);
        adj[v].emplace_back(u);
    }
    cout << max(solve1(),solve2());
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14168 KB Output is correct
2 Correct 13 ms 14172 KB Output is correct
3 Correct 46 ms 14172 KB Output is correct
4 Correct 47 ms 14172 KB Output is correct
5 Correct 4 ms 13912 KB Output is correct
6 Correct 4 ms 13916 KB Output is correct
7 Correct 3 ms 13916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2694 ms 25560 KB Output is correct
2 Incorrect 2681 ms 25724 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3865 ms 24808 KB Output is correct
2 Execution timed out 5035 ms 24320 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14168 KB Output is correct
2 Correct 13 ms 14172 KB Output is correct
3 Correct 46 ms 14172 KB Output is correct
4 Correct 47 ms 14172 KB Output is correct
5 Correct 4 ms 13912 KB Output is correct
6 Correct 4 ms 13916 KB Output is correct
7 Correct 3 ms 13916 KB Output is correct
8 Correct 2694 ms 25560 KB Output is correct
9 Incorrect 2681 ms 25724 KB Output isn't correct
10 Halted 0 ms 0 KB -