/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define PI 3.1415926535
#define pb push_back
#define setp() cout << setprecision(15)
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " is " << x << '\n';
const int N = 1e6+100, M = 1e5+10, F = 2147483646, K = 20;
ll e[N], re[N], h[N], rh[N];
ll po(ll a, ll b){ll res = 1; while(b){if(b&1) (res*=a)%=MOD; b>>=1; (a*=a)%=MOD;} return res;}
struct Dsu {
vector<int> s, p;
vector<deque<char>> str;
vector<ll> ans;
Dsu(int n, string ss){
s.resize(n+1, 1);
p.resize(n+1);
ans.resize(n+1, 1);
for(int i = 0; i <= n; ++i) p[i] = i;
str.pb({'c'});
for(int i = 0; i < n; ++i) str.pb({ss[i]});
}
int find(int v){
if(p[v] == v) return v;
return (p[v] = find(p[v]));
}
ll merge(int a, int b){
a = find(a);
b = find(b);
if(a != b){
if(s[a] > s[b]){
for(char c: str[b]) str[a].pb(c);
swap(a, b);
}else{
for(int c = str[a].size() - 1; c >= 0; --c) str[b].push_front(str[a][c]);
}
s[b] += s[a];
p[a] = b;
int x = p[a];
ans[x] = 0;
set<int> ss, ss1;
h[0] = 0, rh[s[x] + 1] = 0;
for(int i = 1; i <= s[x]; ++i) h[i] = (h[i - 1] + e[i] * (str[x][i-1]-'0'+1)) % MOD;
for(int i = s[x]; i >= 1; --i) rh[i] = (rh[i + 1] + e[s[x] - i + 1] * (str[x][i-1]-'0'+1)) % MOD;
for(int i = 1; i <= str[x].size(); ++i){
// cout << str[x][i - 1];
for(int j = i; j <= str[x].size(); ++j){
int m = (i + j) >> 1;
bool ok = 1;
int has;
if((i - j) & 1){
ok = ((h[m] - h[i - 1] + MOD) * re[i - 1] % MOD) == ((rh[m + 1] - rh[j + 1] + MOD) * re[s[x] - j] % MOD);
has = ((h[m] - h[i - 1] + MOD) * re[i - 1] % MOD);
}else{
ok = ((h[m] - h[i - 1] + MOD) * re[i - 1] % MOD) == ((rh[m] - rh[j + 1] + MOD) * re[s[x] - j] % MOD);
has = ((h[m] - h[i - 1] + MOD) * re[i - 1] % MOD);
}
if(ok) if((i-j)%2) ss.insert(has); else ss1.insert(has);
}
}
return ans[x] = ss.size() + ss1.size();
}
return -1;
}
};
int n;
string s;
void solve(){
cin >> n >> s;
e[0] = re[0] = 1;
for(int i = 1; i < N; ++i) e[i] = (e[i - 1] * 3) % MOD;
re[N - 1] = po(e[N - 1], MOD - 2);
for(int i = N - 2; i >= 0; --i) re[i] = (re[i + 1] * 3) % MOD;
Dsu d(n, s);
if(n <= 1000){
for(int i = 0; i < n - 1; ++i){
int u, v; cin >> u >> v;
u = d.find(u);
v = d.find(v);
cout << d.merge(u, v) << '\n';
}
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int T = 1, aa;
// cin >> T;aa=T;
while(T--){
// cout << "Case #" << aa-T << ": ";
solve();
cout << '\n';
}
return 0;
}
Compilation message
Main.cpp: In member function 'll Dsu::merge(int, int)':
Main.cpp:53:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int i = 1; i <= str[x].size(); ++i){
| ~~^~~~~~~~~~~~~~~~
Main.cpp:55:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int j = i; j <= str[x].size(); ++j){
| ~~^~~~~~~~~~~~~~~~
Main.cpp:70:23: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
70 | if(ok) if((i-j)%2) ss.insert(has); else ss1.insert(has);
| ^
Main.cpp: In function 'int main()':
Main.cpp:108:16: warning: unused variable 'aa' [-Wunused-variable]
108 | int T = 1, aa;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16084 KB |
Output is correct |
2 |
Correct |
17 ms |
16072 KB |
Output is correct |
3 |
Correct |
17 ms |
15956 KB |
Output is correct |
4 |
Correct |
16 ms |
16060 KB |
Output is correct |
5 |
Correct |
16 ms |
15956 KB |
Output is correct |
6 |
Correct |
18 ms |
15992 KB |
Output is correct |
7 |
Correct |
19 ms |
16048 KB |
Output is correct |
8 |
Correct |
18 ms |
16000 KB |
Output is correct |
9 |
Correct |
16 ms |
15956 KB |
Output is correct |
10 |
Correct |
18 ms |
15972 KB |
Output is correct |
11 |
Correct |
17 ms |
16048 KB |
Output is correct |
12 |
Correct |
15 ms |
16036 KB |
Output is correct |
13 |
Correct |
15 ms |
15960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16084 KB |
Output is correct |
2 |
Correct |
17 ms |
16072 KB |
Output is correct |
3 |
Correct |
17 ms |
15956 KB |
Output is correct |
4 |
Correct |
16 ms |
16060 KB |
Output is correct |
5 |
Correct |
16 ms |
15956 KB |
Output is correct |
6 |
Correct |
18 ms |
15992 KB |
Output is correct |
7 |
Correct |
19 ms |
16048 KB |
Output is correct |
8 |
Correct |
18 ms |
16000 KB |
Output is correct |
9 |
Correct |
16 ms |
15956 KB |
Output is correct |
10 |
Correct |
18 ms |
15972 KB |
Output is correct |
11 |
Correct |
17 ms |
16048 KB |
Output is correct |
12 |
Correct |
15 ms |
16036 KB |
Output is correct |
13 |
Correct |
15 ms |
15960 KB |
Output is correct |
14 |
Correct |
15 ms |
15956 KB |
Output is correct |
15 |
Execution timed out |
1091 ms |
16880 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
59 ms |
81876 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16084 KB |
Output is correct |
2 |
Correct |
17 ms |
16072 KB |
Output is correct |
3 |
Correct |
17 ms |
15956 KB |
Output is correct |
4 |
Correct |
16 ms |
16060 KB |
Output is correct |
5 |
Correct |
16 ms |
15956 KB |
Output is correct |
6 |
Correct |
18 ms |
15992 KB |
Output is correct |
7 |
Correct |
19 ms |
16048 KB |
Output is correct |
8 |
Correct |
18 ms |
16000 KB |
Output is correct |
9 |
Correct |
16 ms |
15956 KB |
Output is correct |
10 |
Correct |
18 ms |
15972 KB |
Output is correct |
11 |
Correct |
17 ms |
16048 KB |
Output is correct |
12 |
Correct |
15 ms |
16036 KB |
Output is correct |
13 |
Correct |
15 ms |
15960 KB |
Output is correct |
14 |
Correct |
15 ms |
15956 KB |
Output is correct |
15 |
Execution timed out |
1091 ms |
16880 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |