// I don't know ..
#include<bits/stdc++.h>
using namespace std;
const int N = 300005;
int n, M[N], SZ[N], C[N];
char S[N];
long long tot;
vector < int > Adj[N];
pair < int , int > dp[N], pd[N];
void DFSSZ(int v, int p = 0)
{
SZ[v] = 1;
for (int u : Adj[v])
if (u != p && !M[u])
DFSSZ(u, v), SZ[v] += SZ[u];
}
int Centroid(int v, int p, int _n)
{
for (int u : Adj[v])
if (!M[u] && u != p && SZ[u] * 2 > _n)
return Centroid(u, v, _n);
return v;
}
void DFSC(int v, int p)
{
dp[v] = {dp[p].first + S[v], min(dp[p].second + S[v], 0)};
pd[v] = {pd[p].first - S[v], min(pd[p].second - S[v], 0)};
for (int u : Adj[v])
if (u != p && !M[u])
DFSC(u, v);
}
void DFSADD(int v, int p, int tp)
{
if (dp[v].second >= 0)
C[dp[v].first] += tp;
for (int u : Adj[v])
if (!M[u] && u != p)
DFSADD(u, v, tp);
}
void DFSGT(int v, int p)
{
if (pd[v].second >= 0)
tot += C[pd[v].first];
for (int u : Adj[v])
if (!M[u] && u != p)
DFSGT(u, v);
}
inline void Solve(int v)
{
dp[v] = {S[v], min((int)S[v], 0)};
pd[v] = {0, 0};
for (int u : Adj[v])
if (!M[u])
DFSC(u, v);
for (int u : Adj[v])
if (!M[u])
DFSADD(u, v, 1);
tot += C[0];
if (dp[v].second >= 0)
C[dp[v].first] ++;
for (int u : Adj[v])
if (!M[u])
DFSADD(u, v, -1), DFSGT(u, v), DFSADD(u, v, 1);
for (int u : Adj[v])
if (!M[u])
DFSADD(u, v, -1);
if (dp[v].second >= 0)
C[dp[v].first] --;
}
inline void Decompose()
{
queue < int > qu;
qu.push(1);
while (qu.size())
{
int v = qu.front();
DFSSZ(v);
v = Centroid(v, 0, SZ[v]);
qu.pop(); M[v] = 1;
Solve(v);
for (int u : Adj[v])
if (!M[u]) qu.push(u);
}
}
int main()
{
scanf("%d%s", &n, S + 1);
for (int i = 1; i <= n; i ++)
S[i] = (S[i] == '(' ? 1 : -1);
for (int i = 1; i < n; i ++)
{
int v, u;
scanf("%d%d", &v, &u);
Adj[v].push_back(u);
Adj[u].push_back(v);
}
Decompose();
return !printf("%lld\n", tot);
}
Compilation message
zagrade.cpp: In function 'int main()':
zagrade.cpp:87:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%s", &n, S + 1);
~~~~~^~~~~~~~~~~~~~~~~~~
zagrade.cpp:93:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &v, &u);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
7424 KB |
Output is correct |
2 |
Correct |
11 ms |
7424 KB |
Output is correct |
3 |
Correct |
10 ms |
7424 KB |
Output is correct |
4 |
Correct |
10 ms |
7424 KB |
Output is correct |
5 |
Correct |
11 ms |
7424 KB |
Output is correct |
6 |
Correct |
10 ms |
7424 KB |
Output is correct |
7 |
Correct |
10 ms |
7424 KB |
Output is correct |
8 |
Correct |
10 ms |
7424 KB |
Output is correct |
9 |
Correct |
10 ms |
7424 KB |
Output is correct |
10 |
Correct |
10 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
565 ms |
47736 KB |
Output is correct |
2 |
Correct |
537 ms |
47736 KB |
Output is correct |
3 |
Correct |
553 ms |
47736 KB |
Output is correct |
4 |
Correct |
570 ms |
47736 KB |
Output is correct |
5 |
Correct |
551 ms |
47736 KB |
Output is correct |
6 |
Correct |
588 ms |
47876 KB |
Output is correct |
7 |
Correct |
531 ms |
47608 KB |
Output is correct |
8 |
Correct |
567 ms |
47780 KB |
Output is correct |
9 |
Correct |
534 ms |
47608 KB |
Output is correct |
10 |
Correct |
540 ms |
48248 KB |
Output is correct |
11 |
Correct |
559 ms |
47608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
7424 KB |
Output is correct |
2 |
Correct |
11 ms |
7424 KB |
Output is correct |
3 |
Correct |
10 ms |
7424 KB |
Output is correct |
4 |
Correct |
10 ms |
7424 KB |
Output is correct |
5 |
Correct |
11 ms |
7424 KB |
Output is correct |
6 |
Correct |
10 ms |
7424 KB |
Output is correct |
7 |
Correct |
10 ms |
7424 KB |
Output is correct |
8 |
Correct |
10 ms |
7424 KB |
Output is correct |
9 |
Correct |
10 ms |
7424 KB |
Output is correct |
10 |
Correct |
10 ms |
7424 KB |
Output is correct |
11 |
Correct |
565 ms |
47736 KB |
Output is correct |
12 |
Correct |
537 ms |
47736 KB |
Output is correct |
13 |
Correct |
553 ms |
47736 KB |
Output is correct |
14 |
Correct |
570 ms |
47736 KB |
Output is correct |
15 |
Correct |
551 ms |
47736 KB |
Output is correct |
16 |
Correct |
588 ms |
47876 KB |
Output is correct |
17 |
Correct |
531 ms |
47608 KB |
Output is correct |
18 |
Correct |
567 ms |
47780 KB |
Output is correct |
19 |
Correct |
534 ms |
47608 KB |
Output is correct |
20 |
Correct |
540 ms |
48248 KB |
Output is correct |
21 |
Correct |
559 ms |
47608 KB |
Output is correct |
22 |
Correct |
1817 ms |
28868 KB |
Output is correct |
23 |
Correct |
1660 ms |
28792 KB |
Output is correct |
24 |
Correct |
1603 ms |
28920 KB |
Output is correct |
25 |
Correct |
1633 ms |
28796 KB |
Output is correct |
26 |
Correct |
691 ms |
35192 KB |
Output is correct |
27 |
Correct |
717 ms |
32376 KB |
Output is correct |
28 |
Correct |
765 ms |
31096 KB |
Output is correct |
29 |
Correct |
513 ms |
48248 KB |
Output is correct |
30 |
Correct |
525 ms |
48248 KB |
Output is correct |
31 |
Correct |
125 ms |
29544 KB |
Output is correct |
32 |
Correct |
523 ms |
47736 KB |
Output is correct |