답안 #885786

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
885786 2023-12-10T18:03:45 Z AgentPengin Lampice (COCI19_lampice) C++14
110 / 110
1878 ms 29276 KB
/**
 *    author:  AgentPengin ( Độc cô cầu bại )
 *    created: 23.12.2022 10:08:02
 *    too lazy to update time
**/
#include<bits/stdc++.h>

#define EL '\n'
#define fi first
#define se second
#define NAME "TASK"
#define ll long long
#define lcm(a,b) (a/gcd(a,b))*b
#define db(val) "["#val" = " << (val) << "] "
#define bend(v) (v).begin(),(v).end()
#define sz(v) (int)(v).size()
#define ex exit(0)

using namespace std;

const ll mod = 1e9 + 7;
const int inf = 0x1FFFFFFF;
const int MAXN = 2e5 + 5;
const ll base = 35711;

int n,Len,valid[MAXN],child[MAXN],mx_depth;
char a[MAXN];
vector<int> adj[MAXN];
vector<pair<int,ll>> b;
ll po[MAXN];
unordered_map<ll,bool> f[MAXN];

void countChild(int u,int p) {
	child[u] = 1;
	for (auto v : adj[u]) {
		if (v != p && valid[v]) {
			countChild(v,u);
			child[u] += child[v];
		}
	}
}

bool dfs(int u,int p,int h,ll hashdown,ll hashup) {
	if (h > Len) return false;
	if (p) {
		hashdown = (hashdown * base + a[u]) % mod;
	}
	hashup = (hashup + 1LL * a[u] * po[h - 1]) % mod;
	ll x = (hashup * po[Len - h] - hashdown + mod) % mod;
	if (!p) f[h][x] = true;
	if (f[Len - h + 1].find(x) != f[Len - h + 1].end()) return true;
	
	for (auto v : adj[u]) {
		if (v != p && valid[v]) {
			if (!p) b.clear();
			if (dfs(v,u,h + 1,hashdown,hashup)) return true;
			if (!p) {
				for (auto x : b) {
					f[x.fi][x.se] = true;
				}
			}
		}
	}
	mx_depth = max(mx_depth,h);
	b.push_back({h,x});
	return false;
}

bool solve(int u,int n) {
	countChild(u,0);
	int flag = 1,half = n / 2;
	while(flag) {
		flag = 0;
		for (auto v : adj[u]) {
			if (valid[v] && child[v] < child[u] && child[v] > half) {
				u = v;
				flag = true;
				break;
			}
		}
	}
	countChild(u,0);
	if (dfs(u,0,1,0,0)) return true;
	for (int i = 1;i <= mx_depth;i++) f[i].clear();
	mx_depth = 0;
	valid[u] = false;
	for (auto v : adj[u]) {
		if (valid[v]) {
			if (solve(v,child[v])) return true;
		}
	}
	return false;
}

bool check(int len) {
	Len = len;
	for (int i = 1;i <= n;i++) {
		valid[i] = 1;
		f[i].clear();
	} 
	return solve(1,n);
}

signed main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    if (ifstream(NAME".inp")) {
        freopen(NAME".inp","r",stdin);
        freopen(NAME".out","w",stdout);
    }
    cin >> n;
    for (int i = 1;i <= n;i++) cin >> a[i];
    for (int i = 1;i < n;i++) {
    	int u,v; cin >> u >> v;
    	adj[u].push_back(v);
    	adj[v].push_back(u);
    }
    po[0] = 1;
    for (int i = 1;i <= n;i++) po[i] = po[i - 1] * base % mod;
    int l = 0,r = (n - 1) / 2;
    while(l < r) {
    	int mid = (l + r + 1) >> 1;
    	if (check(mid * 2 + 1)) {
    		l = mid;
    	} else {
    		r = mid - 1;
    	}
    }
    int ans = r * 2 + 1;
    l = 0,r = n / 2;
    while(l < r) {
    	int mid = (l + r + 1) >> 1;
    	if (check(mid * 2)) {
    		l = mid;
    	} else r = mid - 1;
    }
    cout << max(ans,r * 2);
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
    return 0;
}
// agent pengin wants to take apio (with anya-san)

Compilation message

lampice.cpp: In function 'int main()':
lampice.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |         freopen(NAME".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
lampice.cpp:108:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |         freopen(NAME".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 19032 KB Output is correct
2 Correct 8 ms 19036 KB Output is correct
3 Correct 20 ms 19036 KB Output is correct
4 Correct 27 ms 19284 KB Output is correct
5 Correct 4 ms 19036 KB Output is correct
6 Correct 4 ms 19060 KB Output is correct
7 Correct 5 ms 19036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 829 ms 28108 KB Output is correct
2 Correct 758 ms 28364 KB Output is correct
3 Correct 609 ms 28380 KB Output is correct
4 Correct 663 ms 28628 KB Output is correct
5 Correct 1078 ms 29276 KB Output is correct
6 Correct 147 ms 28632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1562 ms 24540 KB Output is correct
2 Correct 1878 ms 24012 KB Output is correct
3 Correct 1589 ms 24900 KB Output is correct
4 Correct 1588 ms 24284 KB Output is correct
5 Correct 1179 ms 23528 KB Output is correct
6 Correct 1135 ms 23404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 19032 KB Output is correct
2 Correct 8 ms 19036 KB Output is correct
3 Correct 20 ms 19036 KB Output is correct
4 Correct 27 ms 19284 KB Output is correct
5 Correct 4 ms 19036 KB Output is correct
6 Correct 4 ms 19060 KB Output is correct
7 Correct 5 ms 19036 KB Output is correct
8 Correct 829 ms 28108 KB Output is correct
9 Correct 758 ms 28364 KB Output is correct
10 Correct 609 ms 28380 KB Output is correct
11 Correct 663 ms 28628 KB Output is correct
12 Correct 1078 ms 29276 KB Output is correct
13 Correct 147 ms 28632 KB Output is correct
14 Correct 1562 ms 24540 KB Output is correct
15 Correct 1878 ms 24012 KB Output is correct
16 Correct 1589 ms 24900 KB Output is correct
17 Correct 1588 ms 24284 KB Output is correct
18 Correct 1179 ms 23528 KB Output is correct
19 Correct 1135 ms 23404 KB Output is correct
20 Correct 887 ms 21720 KB Output is correct
21 Correct 1026 ms 22736 KB Output is correct
22 Correct 1434 ms 23020 KB Output is correct
23 Correct 376 ms 21140 KB Output is correct
24 Correct 1252 ms 23160 KB Output is correct
25 Correct 1212 ms 22420 KB Output is correct
26 Correct 1495 ms 24800 KB Output is correct
27 Correct 1612 ms 23224 KB Output is correct
28 Correct 1020 ms 21316 KB Output is correct
29 Correct 1171 ms 21472 KB Output is correct
30 Correct 1205 ms 24088 KB Output is correct
31 Correct 1070 ms 22416 KB Output is correct
32 Correct 970 ms 25288 KB Output is correct
33 Correct 738 ms 22520 KB Output is correct