CSP-S2 2019 D1T2 parentheses solution to a problem tree

Speak in front of the

Would like to Cipian solution to a problem, to commemorate my junior high school two years \ (OI \) career and not be closed as long or short time.

Hopefully high schools to continue their studies \ (OI \) it, and sincerely hope that other \ (OIer \) Do not make my mistake similar.

The meaning of problems

Original title link

Has given a \ (n-\) nodes of the tree, each node corresponds to a bracket (left or right) required to obtain valid string brackets brackets child simple paths from the node to the root node constituted string number.
Wherein the bracket defines legal string is such that

  1. ()Parenthesis string is legal.
  2. If you Aare a legitimate brackets string, it (A)is legitimate brackets string.
  3. If A, Blegitimate brackets string, it ABis legitimate brackets string.

Wherein \ (n \ le 5 \ times 10 ^ 5 \)

answer

1. Preliminary ideas

Order \ (cnt_i \) represents the \ (I \) number of the root node to a string consisting of brackets legitimate bracket substrings, \ (F \) represents \ (I \) node of the father, it is clear that there is a the conclusion:
\ [cnt_i = cnt_f + (\ {i node number to the end of legitimate parentheses substring} text) \]
this conclusion should not need to prove ...... this is the key to solving problems, so we only need to consider in \ (I \) node is a legitimate end brackets the number of sub-strings (which is set \ (t_i \) ).

2. Statistics answer

First consider how violence statistics, it is clear from the direct \ (i \) node jump up, legitimate statistical sub-brackets to the number of strings, this is \ (O (n_2) \) is.

Then consider the legal definition given in brackets string: "If A, Blegitimate brackets string, it ABis legitimate brackets string" .

For example, consider the process of violence, if \ (i \) for the end of the brackets strings are ()()()(), we have a total of statistics \ (4 \) months, but in fact, we just need to count the far right (dfs order maximum) that legitimate parentheses, brackets and the left is equivalent to three pairs \ (f_f T_ {} \) (i.e. \ (f_f \) an amount of a legitimate end bracket string), since the leftmost string brackets ()three brackets string legitimate right of (), ()(), ()()()legal, contact the above properties, so they put together equally legitimate.

To after this step, the stack looks like most people are used to do, I'm in the examination room \ (yy \) out of an odd ♂ wonderful way here to share it.

So we set \ (re_i \) to satisfy the \ (I \) node to the node string in brackets constituted legitimate bracket string, and the maximum depth nodes, to give \ (cnt_i \) expression
\ [cnt_i = cnt_f + t_ {f_ {re_i}}
+ 1 \] bound at the previous example ()()()(), \ (cnt_f \) needless to say, \ (1 \) represented is \ (re_i \) to begin with \ (I \) is legal parentheses at the end of the string (ie, the leftmost example ()), \ ({re_i F_ t_ {}} \) is three legitimate brackets string example on the right (), , ()(), ()()()as to why these three brackets string credited answers It has been discussed above.

Combined with a look at the code:

cnt[x]+=cnt[f],fa[x]=f;
if (a[x]==1) // 只有a[x]==1(即括号为')')才有可能存在以x为结尾的合法括号串
{
    while (a[f]!=-1&&re[f]!=-1) f=fa[re[f]]; // 找到re[x]
    if (f==0||a[f]==1) re[x]=-1; // re[x]需合法
    else re[x]=f,cnt[x]+=cnt[fa[f]]-cnt[fa[fa[f]]]+1; // 统计答案。cnt[fa[f]]-cnt[fa[fa[f]]]就等于上文中的t[fa[f]]
}
else re[x]=-1;

Lessons learned

When I do this question in the examination room, in the above whileplay into if, so (Los Valley Self Test) \ (100 \ to 10 \)

In fact, this situation is not taken into account ((())()). (As for why the wrong hands can play with)

Then you happy \ (\ text {AFO} \ ) engage in cultural lesson

Here to remind you to personally lessons, must pay attention to the details! Consider the case must be fully! Do not repeat my mistakes!

I wish you all achieved satisfactory results (I can not get up)

Code

In fact, the code is very short matter, \ (qwq \) .

#include <stdio.h>

using namespace std;

template <typename T> inline void Read(T &t)
{
    int c=getchar(),f=0;
    for (;c<'0'||c>'9';c=getchar()) f=(c=='-');
    for (t=0;c>='0'&&c<='9';c=getchar()) t=(t<<3)+(t<<1)+(c^48);
    if (f) t=-t;
}

typedef long long ll;
const int N=5e5+5;

int n,tot,head[N],a[N],fa[N],re[N];
ll ans,cnt[N];
char temp[N];
 
struct Edge
{
    int to,next;
    void add(int x, int y) { to=y,next=head[x],head[x]=tot; }
} e[N<<1];
 
void dfs(int x, int f)
{
    cnt[x]+=cnt[f],fa[x]=f;
    if (a[x]==1)
    {
        while (a[f]!=-1&&re[f]!=-1) f=fa[re[f]];
        if (f==0||a[f]==1) re[x]=-1;
        else re[x]=f,cnt[x]+=cnt[fa[f]]-cnt[fa[fa[f]]]+1;
    }
    else re[x]=-1;
    for (int i=head[x];i;i=e[i].next)
    {
        int v=e[i].to;
        dfs(v,x);
    }
}   

signed main()
{
    Read(n);
    scanf("%s",temp);
    for (int i=1;i<=n;i++) 
        a[i]=(temp[i-1]=='('?-1:1);
    for (int i=2,f;i<=n;i++) Read(f),e[++tot].add(f,i);

    re[0]=-1;
    dfs(1,0);
    
    for (ll i=1;i<=n;i++) ans^=(cnt[i]*i);
    printf("%lld\n",ans);
    
    return 0;
}

Epilogue

The author is a high probability \ (\ text {AFO} \ ) , but still want to give this solution to a problem or did not work out this problem to make people bring some help, I suppose I \ (OI \) careerSupernaturalIt

Engage in cultural studies exam preparation.

Guess you like

Origin www.cnblogs.com/asd369-blog/p/CSP-S2-2019-brackets.html