#include <cstdio>
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stdlib.h>
#include <cstring>
#include <string.h>
#include <string>
#include <sstream>
#include <assert.h>
#include <climits>
#include <sstream>
#include <numeric>
#include <time.h>
#include <limits.h>
#include <list>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <iomanip>
#include <complex>
#include <chrono>
#include <fstream>
#include <functional>
#include <unistd.h>
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx,avx2,bmi,bmi2,lzcnt,popcnt")
#define lowbit(x) ((x) & -(x))
#define ml(a, b) ((1ll * (a) * (b)) % M)
#define tml(a, b) (a) = ((1ll * (a) * (b)) % M)
#define ad(a, b) ((0ll + (a) + (b)) % M)
#define tad(a, b) (a) = ((0ll + (a) + (b)) % M)
#define mi(a, b) ((0ll + M + (a) - (b)) % M)
#define tmi(a, b) (a) = ((0ll + M + (a) - (b)) % M)
#define tmin(a, b) (a) = min((a), (b))
#define tmax(a, b) (a) = max((a), (b))
#define iter(a) (a).begin(), (a).end()
#define riter(a) (a).rbegin(), (a).rend()
#define init(a, b) memset((a), (b), sizeof(a))
#define cpy(a, b) memcpy((a), (b), sizeof(a))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define pb emplace_back
#define mpr make_pair
#define ls(i) ((i) << 1)
#define rs(i) ((i) << 1 | 1)
#define INF 0x3f3f3f3f
#define NIF 0xc0c0c0c0
#define eps 1e-9
#define F first
#define S second
#define AC cin.tie(0)->sync_with_stdio(0)
using namespace std;
typedef long long llt;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<llt, llt> pll;
typedef complex<double> cd;
// const int M = 998244353;
// random_device rm;
// mt19937 rg(rm());
// default_random_engine rg(rm());
// uniform_int_distribution<int> rd(INT_MIN, INT_MAX);
// uniform_real_distribution<double> rd(0, M_PI);
void db() { cerr << "\n"; }
template <class T, class... U>
void db(T a, U... b) { cerr << a << " ", db(b...); }
inline char gc()
{
const static int SZ = 1 << 16;
static char buf[SZ], *p1, *p2;
if (p1 == p2 && (p2 = buf + fread(p1 = buf, 1, SZ, stdin), p1 == p2))
return -1;
return *p1++;
}
void rd() {}
template <typename T, typename... U>
void rd(T &x, U &...y)
{
x = 0;
bool f = 0;
char c = gc();
while (!isdigit(c))
f ^= !(c ^ 45), c = gc();
while (isdigit(c))
x = (x << 1) + (x << 3) + (c ^ 48), c = gc();
f && (x = -x), rd(y...);
}
template <typename T>
void prt(T x)
{
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
prt(x / 10);
putchar((x % 10) ^ 48);
}
const int N = 1e5 + 5;
struct TR
{
vector<int> len, kp, bk[2], to[2];
deque<int> dq;
int cnt, ln, rn, sz;
TR() : cnt(2), sz(0)
{
len = {-1, 0}, ln = rn = 1; // 0
kp = bk[0] = bk[1] = to[0] = to[1] = {0, 0};
}
void add(int u, int vl)
{
len.pb(len[u] + 2);
to[0].pb(0), to[1].pb(0);
kp.pb(u ? to[vl][bk[vl][u]] : 1);
to[vl][u] = cnt++;
}
void addr(int vl)
{
dq.pb(vl);
int u;
if (len[rn] < sz && dq[sz - len[rn] - 1] == vl)
u = rn;
else
u = bk[vl][rn];
if (!to[vl][u])
{
add(u, vl);
int tp = dq[sz - len[kp.back()]];
bk[tp].pb(kp.back());
bk[tp ^ 1].pb(bk[tp ^ 1][kp.back()]);
}
rn = to[vl][u];
if (len[rn] == ++sz)
ln = rn;
}
void addl(int vl)
{
int u;
if (len[ln] < sz && dq[len[ln]] == vl)
u = ln;
else
u = bk[vl][ln];
dq.push_front(vl);
if (!to[vl][u])
{
add(u, vl);
int tp = dq[len[kp.back()]];
bk[tp].pb(kp.back());
bk[tp ^ 1].pb(bk[tp ^ 1][kp.back()]);
}
ln = to[vl][u];
if (len[ln] == ++sz)
rn = ln;
}
} tr[N];
char s[N];
int ds[N];
int f(int x)
{
return ds[x] == x ? x : ds[x] = f(ds[x]);
}
signed main()
{
int n, x, y;
scanf("%d%s", &n, s + 1);
iota(ds + 1, ds + n + 1, 1);
for (int i = 1; i <= n; i++)
tr[i].addl(s[i] - '0');
for (int i = 1; i < n; i++)
{
scanf("%d%d", &x, &y);
x = f(x), y = f(y);
if (tr[x].sz > tr[y].sz)
{
for (int p = 0; p < tr[y].sz; p++)
tr[x].addr(tr[y].dq[p]);
printf("%d\n", tr[ds[y] = x].cnt - 2);
}
else
{
for (int p = tr[x].sz - 1; p >= 0; p--)
tr[y].addl(tr[x].dq[p]);
printf("%d\n", tr[ds[x] = y].cnt - 2);
}
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:182:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
182 | scanf("%d%s", &n, s + 1);
| ~~~~~^~~~~~~~~~~~~~~~~~~
Main.cpp:188:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
188 | scanf("%d%d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
102492 KB |
Output is correct |
2 |
Correct |
81 ms |
102536 KB |
Output is correct |
3 |
Correct |
80 ms |
102484 KB |
Output is correct |
4 |
Correct |
81 ms |
102480 KB |
Output is correct |
5 |
Correct |
60 ms |
102380 KB |
Output is correct |
6 |
Correct |
89 ms |
102496 KB |
Output is correct |
7 |
Correct |
60 ms |
102484 KB |
Output is correct |
8 |
Correct |
83 ms |
102548 KB |
Output is correct |
9 |
Correct |
78 ms |
102480 KB |
Output is correct |
10 |
Correct |
81 ms |
102488 KB |
Output is correct |
11 |
Correct |
81 ms |
102480 KB |
Output is correct |
12 |
Correct |
79 ms |
102480 KB |
Output is correct |
13 |
Correct |
61 ms |
102484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
102492 KB |
Output is correct |
2 |
Correct |
81 ms |
102536 KB |
Output is correct |
3 |
Correct |
80 ms |
102484 KB |
Output is correct |
4 |
Correct |
81 ms |
102480 KB |
Output is correct |
5 |
Correct |
60 ms |
102380 KB |
Output is correct |
6 |
Correct |
89 ms |
102496 KB |
Output is correct |
7 |
Correct |
60 ms |
102484 KB |
Output is correct |
8 |
Correct |
83 ms |
102548 KB |
Output is correct |
9 |
Correct |
78 ms |
102480 KB |
Output is correct |
10 |
Correct |
81 ms |
102488 KB |
Output is correct |
11 |
Correct |
81 ms |
102480 KB |
Output is correct |
12 |
Correct |
79 ms |
102480 KB |
Output is correct |
13 |
Correct |
61 ms |
102484 KB |
Output is correct |
14 |
Correct |
68 ms |
102492 KB |
Output is correct |
15 |
Correct |
87 ms |
103252 KB |
Output is correct |
16 |
Correct |
80 ms |
102992 KB |
Output is correct |
17 |
Correct |
82 ms |
103040 KB |
Output is correct |
18 |
Correct |
93 ms |
103332 KB |
Output is correct |
19 |
Correct |
82 ms |
102996 KB |
Output is correct |
20 |
Correct |
83 ms |
103252 KB |
Output is correct |
21 |
Correct |
85 ms |
102796 KB |
Output is correct |
22 |
Correct |
93 ms |
102804 KB |
Output is correct |
23 |
Correct |
80 ms |
102992 KB |
Output is correct |
24 |
Correct |
84 ms |
102992 KB |
Output is correct |
25 |
Correct |
90 ms |
103252 KB |
Output is correct |
26 |
Correct |
107 ms |
102996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
155 ms |
152660 KB |
Output is correct |
2 |
Correct |
151 ms |
154476 KB |
Output is correct |
3 |
Correct |
192 ms |
152476 KB |
Output is correct |
4 |
Correct |
151 ms |
155168 KB |
Output is correct |
5 |
Correct |
157 ms |
154960 KB |
Output is correct |
6 |
Correct |
151 ms |
153416 KB |
Output is correct |
7 |
Correct |
159 ms |
154596 KB |
Output is correct |
8 |
Correct |
145 ms |
152628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
102492 KB |
Output is correct |
2 |
Correct |
81 ms |
102536 KB |
Output is correct |
3 |
Correct |
80 ms |
102484 KB |
Output is correct |
4 |
Correct |
81 ms |
102480 KB |
Output is correct |
5 |
Correct |
60 ms |
102380 KB |
Output is correct |
6 |
Correct |
89 ms |
102496 KB |
Output is correct |
7 |
Correct |
60 ms |
102484 KB |
Output is correct |
8 |
Correct |
83 ms |
102548 KB |
Output is correct |
9 |
Correct |
78 ms |
102480 KB |
Output is correct |
10 |
Correct |
81 ms |
102488 KB |
Output is correct |
11 |
Correct |
81 ms |
102480 KB |
Output is correct |
12 |
Correct |
79 ms |
102480 KB |
Output is correct |
13 |
Correct |
61 ms |
102484 KB |
Output is correct |
14 |
Correct |
68 ms |
102492 KB |
Output is correct |
15 |
Correct |
87 ms |
103252 KB |
Output is correct |
16 |
Correct |
80 ms |
102992 KB |
Output is correct |
17 |
Correct |
82 ms |
103040 KB |
Output is correct |
18 |
Correct |
93 ms |
103332 KB |
Output is correct |
19 |
Correct |
82 ms |
102996 KB |
Output is correct |
20 |
Correct |
83 ms |
103252 KB |
Output is correct |
21 |
Correct |
85 ms |
102796 KB |
Output is correct |
22 |
Correct |
93 ms |
102804 KB |
Output is correct |
23 |
Correct |
80 ms |
102992 KB |
Output is correct |
24 |
Correct |
84 ms |
102992 KB |
Output is correct |
25 |
Correct |
90 ms |
103252 KB |
Output is correct |
26 |
Correct |
107 ms |
102996 KB |
Output is correct |
27 |
Correct |
155 ms |
152660 KB |
Output is correct |
28 |
Correct |
151 ms |
154476 KB |
Output is correct |
29 |
Correct |
192 ms |
152476 KB |
Output is correct |
30 |
Correct |
151 ms |
155168 KB |
Output is correct |
31 |
Correct |
157 ms |
154960 KB |
Output is correct |
32 |
Correct |
151 ms |
153416 KB |
Output is correct |
33 |
Correct |
159 ms |
154596 KB |
Output is correct |
34 |
Correct |
145 ms |
152628 KB |
Output is correct |
35 |
Correct |
67 ms |
102480 KB |
Output is correct |
36 |
Correct |
218 ms |
159576 KB |
Output is correct |
37 |
Correct |
208 ms |
156152 KB |
Output is correct |
38 |
Correct |
216 ms |
160736 KB |
Output is correct |
39 |
Correct |
229 ms |
159080 KB |
Output is correct |
40 |
Correct |
199 ms |
153408 KB |
Output is correct |
41 |
Correct |
210 ms |
154996 KB |
Output is correct |
42 |
Correct |
146 ms |
152640 KB |
Output is correct |
43 |
Correct |
158 ms |
154624 KB |
Output is correct |
44 |
Correct |
164 ms |
152260 KB |
Output is correct |
45 |
Correct |
164 ms |
153936 KB |
Output is correct |
46 |
Correct |
208 ms |
175608 KB |
Output is correct |
47 |
Correct |
204 ms |
162152 KB |
Output is correct |