제출 #1332435

#제출 시각아이디문제언어결과실행 시간메모리
1332435sonarchtLampice (COCI19_lampice)C++20
17 / 110
813 ms14648 KiB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(v) begin(v), end(v)
#define pll pair<ll, ll>
#define fi first
#define se second
#define vll vector<ll>
#define mll map<ll, ll>
mt19937_64 rng(chrono::high_resolution_clock().now().time_since_epoch().count());
const ll N = 5e4 + 50, inf = 1e18 + 7, mod = 1e9 + 7;
ll n, m, k, q, ans, a[N], base = 29, pw[N], del[N], sz[N];
vll adj[N];
string s;
unordered_map<ll, bool> mp[N];

void dfs_sz(ll p, ll u) {
	sz[u] = 1;
	for (ll v : adj[u]) {
		if (v == p || del[v]) continue;
		dfs_sz(u, v);
		sz[u] += sz[v];
	}
}

ll centroid(ll p, ll u, ll n) {
	for (ll v : adj[u]) {
		if (v != p && sz[v] > n/2 && !del[v]) return centroid(u, v, n);
	}
	return u;
}

ll res = 0, mx_dep = 0;
vector<pll> cur;
void dfs_hsh(ll p, ll u, ll len, ll dep, ll h1, ll h2) {
	if (dep >= len) return;
	mx_dep = max(mx_dep, dep);
	ll x = (h1 * pw[len-dep-1] - h2 + mod) % mod;
	//cout << u << " " << dep << " " << h1 << " " << h2 << " " << x << "\n";
	if (p == -1) mp[dep][x] = 1; //update root
	//cout << "GET " << u << " " << x << "\n";
	if (mp[len-dep-1].count(x)) {
		res = 1;
		return;
	}
	for (ll v : adj[u]) {
		if (v == p || del[v]) continue;
		if (p == -1) cur.clear();
		ll t1 = ((s[v] - 'a' + 1) * pw[dep+1] + h1) % mod;
		ll t2 = (h2 * base + s[v] - 'a' + 1) % mod;
		dfs_hsh(u, v, len, dep+1, t1, t2);
		if (p == -1) {
			for (auto i : cur) mp[i.fi][i.se] = 1;
		}
	}
	//cout << "UPDATE " << u << " " << x << "\n";
	if (p != -1) cur.pb({dep, x});
}

void decomp(ll u) {
	dfs_sz(-1, u);
	ll root = centroid(-1, u, sz[u]);
	// cout << "subtree " << u << "\n";
	// cout << "centroid " << root << "\n";
	
	//dfs_hsh(-1, root, 5, 0, s[root] - 'a' + 1, 0);
	
	ll l = 1, r = n/2+1;
	while (l <= r) {
		ll mid = (l+r) / 2, len = mid*2;
		//cout << "LEN " << len << "\n";
		for (int i = 0; i <= mx_dep; ++i) mp[i].clear();
		res = mx_dep = 0;
		dfs_hsh(-1, root, len, 0, s[root] - 'a' + 1, 0);
		if (res) ans = max(ans, len), l = mid+1;
		else r = mid-1;
	}
	l = 1, r = n/2+1;
	while (l <= r) {
		ll mid = (l+r) / 2, len = mid*2+1;
		//cout << "LEN " << len << "\n";
		for (int i = 0; i <= mx_dep; ++i) mp[i].clear();
		res = mx_dep = 0;
		dfs_hsh(-1, root, len, 0, s[root] - 'a' + 1, 0);
		//if (len == 5 && res) cout << "!!!!!!!!!\n";
		if (res) ans = max(ans, len), l = mid+1;
		else r = mid-1;
	}
	//cout << "ans " << ans << "\n\n";
	del[root] = 1;
	for (ll v : adj[root]) {
		if (!del[v]) decomp(v);
	}
}


void solve() {
	cin >> n >> s;
	s = " " + s;
	pw[0] = 1;
	for (int i = 1; i <= n; ++i) pw[i] = pw[i-1] * base % mod;
	for (int i = 1; i < n; ++i) {
		ll x, y;
		cin >> x >> y;
		adj[x].pb(y);
		adj[y].pb(x);
	}
	ans = 1;
	decomp(1);
	cout << ans;
	return;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	if (fopen(".INP", "r")) {
		freopen(".INP", "r", stdin);
		freopen(".OUT", "w", stdout);
	}
	ll tc = 1;
//	cin >> tc;
	while (tc--) {
		solve();
	}
	return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

lampice.cpp: In function 'int main()':
lampice.cpp:122:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |                 freopen(".INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~
lampice.cpp:123:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  123 |                 freopen(".OUT", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...