답안 #1108016

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1108016 2024-11-02T14:58:53 Z atom Lampice (COCI19_lampice) C++17
110 / 110
1704 ms 58448 KB
#include "bits/stdc++.h"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;
 
#ifdef JASPER
#include "debug.h"
#else
#define debug(...) 166
#endif
 
#define int long long
const int N = 5e4 + 5;
const int BASE = 256;
const int MOD = 1e9 + 7;
 
int n, k;
string a;
int sz[N];
bool rem[N];
vector <int> adj[N];
int pw[N];
 
int dfsSZ(int u, int p) {
    sz[u] = 1;
    for (int v: adj[u]) if (v != p && !rem[v]) {
        sz[u] += dfsSZ(v, u);
    }
    return sz[u];
}
 
int findCentroid(int u, int p, int n) {
    for (int v: adj[u]) if (v != p && !rem[v]) {
        if (2 * sz[v] > n)
            return findCentroid(v, u, n);
    }
    return u;
}
 
int h[N];
int maxHigh;
int hashU[N];
int hashD[N];
int value[N];
int nNode, node[N];
map<int, bool> mp[N];
 
void add(int u, int p, bool &found) {
    h[u] = h[p] + 1;
    if (h[u] + 1 > k) return;
    maxHigh = max(maxHigh, h[u]);
    hashU[u] = (1LL * hashU[p] * BASE + (a[u] - 'a' + 1)) % MOD;
    hashD[u] = (hashD[p] + 1LL * pw[h[u]] * (a[u] - 'a' + 1)) % MOD;
    int need = (1LL * pw[k - h[u] - 1] * hashD[u] - hashU[u] + MOD) % MOD;
    if (mp[k - h[u] - 1].find(need) != mp[k - h[u] - 1].end()) {
        found = true;
        return;
    }
    if (h[u] + 1 == k && hashD[u] == (hashU[u] + 1LL * pw[h[u]] * (a[u] - 'a' + 1)) % MOD) {
        found = true;
        return;
    }
    node[++nNode] = u; value[u] = need;
    for (int v: adj[u]) if (v != p && !rem[v]) {
        add(v, u, found);
        if (found) return;
    }
}
 
void solve(int u, int p, bool &found) {
    int n = dfsSZ(u, p), c = findCentroid(u, p, n); rem[c] = true;
 
    h[c] = 0;
    nNode = 0;
    maxHigh = 0;
    hashU[c] = 0;
    hashD[c] = a[c] - 'a' + 1;
    for (int v: adj[c]) if (v != p && !rem[v]) {
        add(v, c, found);
        if (found) return;
        while (nNode > 0) {
            int u = node[nNode--];
            mp[h[u]][value[u]] = 1;
        }
    }
    for (int i = 0; i <= maxHigh; ++i) {
        mp[i].clear();
    }
 
    for (int v: adj[c]) if (v != p && !rem[v]) {
        solve(v, c, found);
        if (found) return;
    }
}
 
bool func(int x) {
    memset(rem, false, sizeof rem);
    bool found = false; k = x;
    solve(1, 0, found);
    return found;
}
 
signed main() {
    cin.tie(0) -> sync_with_stdio(0);
 
    cin >> n >> a;
    a = "@" + a;
 
    for (int i = 1; i < n; ++i) {
    	int u, v;
    	cin >> u >> v;
    	adj[u].push_back(v);
    	adj[v].push_back(u);
    }
 
    pw[0] = 1;
    for (int i = 1; i <= n; ++i) {
    	pw[i] = (pw[i - 1] * BASE) % MOD;
    }
 
   	int ans = 1;
    for (int i = 0; i <= 1; ++i) {
        int l = 0, r = n / 2 + 1;
        while (l + 1 < r) {
            int mid = (l + r) / 2;
            if (func(i + 2 * mid)) 
            	l = mid; 
            else 
            	r = mid;
        }
        ans = max(ans, i + 2 * l);
    }
    cout << ans << "\n";
 
    return 0;
}
 
// Centroid: path of equal length
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6736 KB Output is correct
2 Correct 7 ms 6736 KB Output is correct
3 Correct 16 ms 6736 KB Output is correct
4 Correct 15 ms 6896 KB Output is correct
5 Correct 2 ms 6736 KB Output is correct
6 Correct 1 ms 6736 KB Output is correct
7 Correct 2 ms 6736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 669 ms 13392 KB Output is correct
2 Correct 602 ms 15432 KB Output is correct
3 Correct 441 ms 29512 KB Output is correct
4 Correct 595 ms 29244 KB Output is correct
5 Correct 884 ms 16208 KB Output is correct
6 Correct 290 ms 58448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1342 ms 12112 KB Output is correct
2 Correct 1670 ms 12020 KB Output is correct
3 Correct 1399 ms 12196 KB Output is correct
4 Correct 1234 ms 13128 KB Output is correct
5 Correct 1147 ms 18196 KB Output is correct
6 Correct 1035 ms 11088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6736 KB Output is correct
2 Correct 7 ms 6736 KB Output is correct
3 Correct 16 ms 6736 KB Output is correct
4 Correct 15 ms 6896 KB Output is correct
5 Correct 2 ms 6736 KB Output is correct
6 Correct 1 ms 6736 KB Output is correct
7 Correct 2 ms 6736 KB Output is correct
8 Correct 669 ms 13392 KB Output is correct
9 Correct 602 ms 15432 KB Output is correct
10 Correct 441 ms 29512 KB Output is correct
11 Correct 595 ms 29244 KB Output is correct
12 Correct 884 ms 16208 KB Output is correct
13 Correct 290 ms 58448 KB Output is correct
14 Correct 1342 ms 12112 KB Output is correct
15 Correct 1670 ms 12020 KB Output is correct
16 Correct 1399 ms 12196 KB Output is correct
17 Correct 1234 ms 13128 KB Output is correct
18 Correct 1147 ms 18196 KB Output is correct
19 Correct 1035 ms 11088 KB Output is correct
20 Correct 1047 ms 11556 KB Output is correct
21 Correct 1308 ms 13896 KB Output is correct
22 Correct 1661 ms 12360 KB Output is correct
23 Correct 361 ms 8784 KB Output is correct
24 Correct 1010 ms 10476 KB Output is correct
25 Correct 937 ms 10312 KB Output is correct
26 Correct 1384 ms 12580 KB Output is correct
27 Correct 1704 ms 11336 KB Output is correct
28 Correct 706 ms 9028 KB Output is correct
29 Correct 774 ms 8800 KB Output is correct
30 Correct 919 ms 11824 KB Output is correct
31 Correct 768 ms 9544 KB Output is correct
32 Correct 960 ms 15416 KB Output is correct
33 Correct 980 ms 11592 KB Output is correct