Submission #600299

# Submission time Handle Problem Language Result Execution time Memory
600299 2022-07-20T17:04:47 Z penguinhacker Lampice (COCI19_lampice) C++17
110 / 110
1101 ms 14248 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ar array

const int mxN=5e4, P=129, M=1e9+696969;
int n, sz[mxN], ts, ans=1, pp[mxN];
string s;
vector<int> adj[mxN];
bool dead[mxN];
unordered_map<int, int> mp[mxN+1];

int add(int a, int b) {
	if ((a+=b)>=M)
		a-=M;
	return a;
}

int sub(int a, int b) {
	if ((a-=b)<0)
		a+=M;
	return a;
}

int mul(int a, int b) {
	return (ll)a*b%M;
}

struct Hash {
	int a=0, b=0, length=0;
};
vector<vector<Hash>> cmps;

Hash operator+(Hash a, Hash b) {
	return {add(mul(a.a, pp[b.length]), b.a), add(mul(b.b, pp[a.length]), a.b), a.length+b.length};
}

Hash mk(char c) {
	return {c, c, 1};
}

int dfs1(int u, int p=-1) {
	sz[u]=1;
	for (int v : adj[u])
		if (!dead[v]&&v!=p)
			sz[u]+=dfs1(v, u);
	return sz[u];
}

int dfs2(int u, int p=-1) {
	for (int v : adj[u])
		if (!dead[v]&&v!=p&&sz[v]>ts/2)
			return dfs2(v, u);
	return u;
}

void dfs3(int u, int p=-1, Hash h=Hash()) {
	h=h+mk(s[u]);
	cmps.back().push_back(h);
	for (int v : adj[u])
		if (!dead[v]&&v!=p)
			dfs3(v, u, h);
}

bool ok(int rt, int x) {
	for (int rep=0; rep<2; ++rep, ++x) {
		for (vector<Hash>& v : cmps)
			for (Hash& h : v)
				if (h.length+2<=x)
					++mp[h.length][sub(mul(h.b, pp[x-h.length]), h.a)];
		for (vector<Hash>& v : cmps) {
			for (Hash& h : v)
				if (h.length+2<=x)
					--mp[h.length][sub(mul(h.b, pp[x-h.length]), h.a)];
			for (Hash h : v) {
				if (h.length+1>=x)
					continue;
				swap(h.a, h.b);
				h=h+mk(s[rt]);
				int cur=sub(mul(h.a, pp[x-h.length]), h.b);
				auto it=mp[x-h.length].find(cur);
				if (it!=mp[x-h.length].end()&&it->second) {
					for (int i=1; i+2<=x; ++i)
						mp[i].clear();
					return 1;
				}
			}
			for (Hash& h : v)
				if (h.length+2<=x)
					++mp[h.length][sub(mul(h.b, pp[x-h.length]), h.a)];
		}
		for (int i=1; i+2<=x; ++i)
			mp[i].clear();
	}
	return 0;
}

void dfs4(int u=0) {
	ts=dfs1(u);
	u=dfs2(u);
	cmps.clear();
	for (int v : adj[u])
		if (!dead[v]) {
			cmps.push_back({});
			dfs3(v, u);
		}
	for (vector<Hash>& v : cmps)
		for (Hash& h : v) {
			Hash x=mk(s[u])+h;
			if (x.a==x.b)
				ans=max(ans, x.length);
		}
	if (ans>=ts)
		return;
	int lb=ans, rb=ts;
	while(lb<rb) {
		int mid=(lb+rb+1)/2;
		if (ok(u, mid))
			lb=mid;
		else
			rb=mid-1;
	}
	ans=max(ans, lb);
	dead[u]=1;
	for (int v : adj[u])
		if (!dead[v])
			dfs4(v);
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	pp[0]=1;
	for (int i=1; i<mxN; ++i)
		pp[i]=mul(pp[i-1], P);
	cin >> n >> s;
	for (int i=1; i<n; ++i) {
		int u, v;
		cin >> u >> v, --u, --v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs4();
	cout << ans;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4436 KB Output is correct
2 Correct 9 ms 4504 KB Output is correct
3 Correct 21 ms 4600 KB Output is correct
4 Correct 18 ms 4564 KB Output is correct
5 Correct 2 ms 4440 KB Output is correct
6 Correct 2 ms 4436 KB Output is correct
7 Correct 3 ms 4436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 466 ms 12968 KB Output is correct
2 Correct 363 ms 13160 KB Output is correct
3 Correct 164 ms 13320 KB Output is correct
4 Correct 214 ms 13828 KB Output is correct
5 Correct 261 ms 14248 KB Output is correct
6 Correct 82 ms 13384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 601 ms 10040 KB Output is correct
2 Correct 551 ms 9620 KB Output is correct
3 Correct 552 ms 10008 KB Output is correct
4 Correct 324 ms 9608 KB Output is correct
5 Correct 449 ms 9180 KB Output is correct
6 Correct 428 ms 8960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4436 KB Output is correct
2 Correct 9 ms 4504 KB Output is correct
3 Correct 21 ms 4600 KB Output is correct
4 Correct 18 ms 4564 KB Output is correct
5 Correct 2 ms 4440 KB Output is correct
6 Correct 2 ms 4436 KB Output is correct
7 Correct 3 ms 4436 KB Output is correct
8 Correct 466 ms 12968 KB Output is correct
9 Correct 363 ms 13160 KB Output is correct
10 Correct 164 ms 13320 KB Output is correct
11 Correct 214 ms 13828 KB Output is correct
12 Correct 261 ms 14248 KB Output is correct
13 Correct 82 ms 13384 KB Output is correct
14 Correct 601 ms 10040 KB Output is correct
15 Correct 551 ms 9620 KB Output is correct
16 Correct 552 ms 10008 KB Output is correct
17 Correct 324 ms 9608 KB Output is correct
18 Correct 449 ms 9180 KB Output is correct
19 Correct 428 ms 8960 KB Output is correct
20 Correct 835 ms 7608 KB Output is correct
21 Correct 1037 ms 8456 KB Output is correct
22 Correct 1073 ms 8624 KB Output is correct
23 Correct 618 ms 7012 KB Output is correct
24 Correct 381 ms 8556 KB Output is correct
25 Correct 508 ms 8016 KB Output is correct
26 Correct 859 ms 9912 KB Output is correct
27 Correct 1101 ms 9020 KB Output is correct
28 Correct 718 ms 7160 KB Output is correct
29 Correct 762 ms 7124 KB Output is correct
30 Correct 371 ms 9396 KB Output is correct
31 Correct 704 ms 7800 KB Output is correct
32 Correct 224 ms 10440 KB Output is correct
33 Correct 646 ms 8984 KB Output is correct