#include <bits/stdc++.h>
#define For(i, a, b) for(int i = a; i <= b; i++)
#define Forr(i, a, b) for(int i = a; i >= b; i--)
#define F first
#define S second
#define sz(x) ((int)x.size())
#define all(x) x.begin(), x.end()
#define eb emplace_back
#define int LL
using namespace std;
using LL = long long;
using pii = pair<int, int>;
const int MAXN = 1010;
struct DSU {
int p[MAXN];
void init() {
memset(p, 0, sizeof(p));
}
int find(int n) {
if(!p[n]) return n;
return p[n] = find(p[n]);
}
void uni(int a, int b) {
a = find(a); b = find(b);
if(a != b) p[b] = a;
}
} dsu;
string s[MAXN];
int pos[MAXN];
int res[MAXN];
int pad[MAXN][MAXN];
set<int> st[MAXN];
vector<int> v[MAXN];
const int B = 5;
const int M = 1000000007;
int pw[MAXN];
int owo(int i, int j) {
if(pad[i][j] != -1) return pad[i][j];
if(i > j) return pad[i][j] = 1;
if(res[i] != res[j]) return pad[i][j] = 0;
return pad[i][j] = owo(i + 1, j - 1);
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
// nya ><
int n; cin >> n;
assert(n <= 1000);
For(i, 1, n) {
char ch; cin >> ch;
s[i] = "";
s[i].push_back(ch);
pos[i] = 0;
}
dsu.init();
vector<pii> op;
For(i, 1, n - 1) {
int a, b; cin >> a >> b;
a = dsu.find(a); b = dsu.find(b);
op.eb(a, b);
pos[b] += sz(s[a]);
s[a] += s[b];
dsu.uni(a, b);
}
Forr(i, n - 2, 0) {
auto p = op[i];
pos[p.S] += pos[p.F];
}
int rt = op.back().F;
pw[0] = 1;
For(i, 1, n) {
res[i] = s[rt][i - 1] - '0';
pos[i]++;
v[i] = vector<int>(1, pos[i]);
pw[i] = pw[i - 1] * B % M;
}
For(i, 1, n) st[i].insert(res[pos[i]]);
// For(i, 1, n) cout << res[i];
// cout << "\n";
memset(pad, -1, sizeof(pad));
For(i, 1, n) For(j, 1, n) {
owo(i, j);
}
For(i, 1, n) {
res[i] = (res[i - 1] * B + res[i] + 1) % M;
}
auto hsh = [&](int i, int j) {
return (res[j] - res[i - 1] * pw[j - i + 1] % M + M) % M;
};
for(auto &p:op) {
int a, b;
tie(a, b) = p;
for(auto &i:v[a]) for(auto &j:v[b]) {
if(pad[i][j]) st[a].insert(hsh(i, j));
}
for(auto &i:st[b]) st[a].insert(i);
v[a].insert(v[a].end(), all(v[b]));
// for(auto &i:st[a]) cout << i << " ";
// cout << "\n";
cout << sz(st[a]) << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8404 KB |
Output is correct |
2 |
Correct |
4 ms |
8492 KB |
Output is correct |
3 |
Correct |
3 ms |
8404 KB |
Output is correct |
4 |
Correct |
4 ms |
8404 KB |
Output is correct |
5 |
Correct |
4 ms |
8404 KB |
Output is correct |
6 |
Correct |
4 ms |
8532 KB |
Output is correct |
7 |
Correct |
4 ms |
8532 KB |
Output is correct |
8 |
Correct |
3 ms |
8404 KB |
Output is correct |
9 |
Correct |
4 ms |
8404 KB |
Output is correct |
10 |
Correct |
6 ms |
8660 KB |
Output is correct |
11 |
Correct |
4 ms |
8532 KB |
Output is correct |
12 |
Correct |
3 ms |
8404 KB |
Output is correct |
13 |
Correct |
4 ms |
8404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8404 KB |
Output is correct |
2 |
Correct |
4 ms |
8492 KB |
Output is correct |
3 |
Correct |
3 ms |
8404 KB |
Output is correct |
4 |
Correct |
4 ms |
8404 KB |
Output is correct |
5 |
Correct |
4 ms |
8404 KB |
Output is correct |
6 |
Correct |
4 ms |
8532 KB |
Output is correct |
7 |
Correct |
4 ms |
8532 KB |
Output is correct |
8 |
Correct |
3 ms |
8404 KB |
Output is correct |
9 |
Correct |
4 ms |
8404 KB |
Output is correct |
10 |
Correct |
6 ms |
8660 KB |
Output is correct |
11 |
Correct |
4 ms |
8532 KB |
Output is correct |
12 |
Correct |
3 ms |
8404 KB |
Output is correct |
13 |
Correct |
4 ms |
8404 KB |
Output is correct |
14 |
Correct |
3 ms |
8404 KB |
Output is correct |
15 |
Correct |
35 ms |
11748 KB |
Output is correct |
16 |
Correct |
13 ms |
9700 KB |
Output is correct |
17 |
Correct |
32 ms |
9916 KB |
Output is correct |
18 |
Correct |
15 ms |
10108 KB |
Output is correct |
19 |
Correct |
57 ms |
21092 KB |
Output is correct |
20 |
Correct |
21 ms |
14564 KB |
Output is correct |
21 |
Correct |
29 ms |
8564 KB |
Output is correct |
22 |
Correct |
13 ms |
8508 KB |
Output is correct |
23 |
Correct |
80 ms |
31052 KB |
Output is correct |
24 |
Correct |
25 ms |
17304 KB |
Output is correct |
25 |
Correct |
26 ms |
8680 KB |
Output is correct |
26 |
Correct |
11 ms |
8668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
724 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8404 KB |
Output is correct |
2 |
Correct |
4 ms |
8492 KB |
Output is correct |
3 |
Correct |
3 ms |
8404 KB |
Output is correct |
4 |
Correct |
4 ms |
8404 KB |
Output is correct |
5 |
Correct |
4 ms |
8404 KB |
Output is correct |
6 |
Correct |
4 ms |
8532 KB |
Output is correct |
7 |
Correct |
4 ms |
8532 KB |
Output is correct |
8 |
Correct |
3 ms |
8404 KB |
Output is correct |
9 |
Correct |
4 ms |
8404 KB |
Output is correct |
10 |
Correct |
6 ms |
8660 KB |
Output is correct |
11 |
Correct |
4 ms |
8532 KB |
Output is correct |
12 |
Correct |
3 ms |
8404 KB |
Output is correct |
13 |
Correct |
4 ms |
8404 KB |
Output is correct |
14 |
Correct |
3 ms |
8404 KB |
Output is correct |
15 |
Correct |
35 ms |
11748 KB |
Output is correct |
16 |
Correct |
13 ms |
9700 KB |
Output is correct |
17 |
Correct |
32 ms |
9916 KB |
Output is correct |
18 |
Correct |
15 ms |
10108 KB |
Output is correct |
19 |
Correct |
57 ms |
21092 KB |
Output is correct |
20 |
Correct |
21 ms |
14564 KB |
Output is correct |
21 |
Correct |
29 ms |
8564 KB |
Output is correct |
22 |
Correct |
13 ms |
8508 KB |
Output is correct |
23 |
Correct |
80 ms |
31052 KB |
Output is correct |
24 |
Correct |
25 ms |
17304 KB |
Output is correct |
25 |
Correct |
26 ms |
8680 KB |
Output is correct |
26 |
Correct |
11 ms |
8668 KB |
Output is correct |
27 |
Runtime error |
1 ms |
724 KB |
Execution killed with signal 6 |
28 |
Halted |
0 ms |
0 KB |
- |