"Bzoj" "3697" drug-picking man's Path & "3127" "USACO2013 Open" Yin and Yang

Source: Internet
Author: User

Point Division Treatment

Orz Hzwer

It's better to think of a point division. However in the program statistics here, I made two mistakes ...

1. I am more foolish to think of: through the son to update the father, that is, the number of the tree in the X-root of the path of the xxxx is how many lines ... So transferred.

However, this is really too stupid, Huang teach a man: from the father to update the son, go to a node directly update the statistics of the path, anyway we want is "through the root of the number of XX path"

So you can get it over with Dfs directly ...

2. The way of the statistical programme is also mistaken ... I've only considered the path to root as a broker, but the path through root is not the only one that is legal ... The station can be qwq at some point on the way.

In addition feel Huang Seniors record [-d,d] posture is very god ah ... The direct array is one-fold larger and then turns into [n-d,n+d] ...

The other ... Set of templates Bai--

1 //Bzoj 36972#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <iostream>6#include <algorithm>7 #defineRep (i,n) for (int i=0;i<n;++i)8 #defineF (i,j,n) for (int i=j;i<=n;++i)9 #defineD (i,j,n) for (int i=j;i>=n;--i)Ten #definePB Push_back One using namespacestd; AtypedefLong LongLL; -InlineintGetint () { -     intR=1, v=0;CharCh=GetChar (); the      for(;! IsDigit (CH); Ch=getchar ())if(ch=='-') r=-1; -      for(; isdigit (ch); Ch=getchar ()) v=v*Ten-'0'+ch; -     returnr*v; - } + Const intn=1e5+Ten; - /*******************template********************/ +  A intto[n<<1],nxt[n<<1],head[n],cnt,v[n<<1]; at voidAddintXintYintz) { -To[++cnt]=y; NXT[CNT]=HEAD[X]; head[x]=cnt; v[cnt]=Z; -To[++cnt]=x; Nxt[cnt]=head[y]; head[y]=cnt; v[cnt]=Z; - } -  - intN,rt,s[n],h[n],size,dep[n],mxdeep; inLL ans,g[n*2][2],f[n*2][2]; - intt[n<<1],dis[n]; to BOOLVis[n]; +  -InlinevoidGetroot (intXintFA) { thes[x]=1; h[x]=0; *      for(intI=head[x];i;i=Nxt[i]) $         if(TO[I]!=FA &&!)Vis[to[i]]) {Panax Notoginseng Getroot (to[i],x); -s[x]+=S[to[i]]; theh[x]=Max (H[x],s[to[i]]); +         } AH[x]=max (h[x],size-s[x]); the     if(H[x]x; + } -  $InlinevoidDfsintXintFA) { $mxdeep=Max (mxdeep,dep[x]); -     if(T[dis[x]]) g[dis[x]][1]++; -     Elseg[dis[x]][0]++; thet[dis[x]]++; -      for(intI=head[x];i;i=Nxt[i])Wuyi         if(!vis[to[i]] && to[i]!=FA) { thedep[to[i]]=dep[x]+1; -dis[to[i]]=dis[x]+V[i]; Wu DFS (to[i],x); -         } Aboutt[dis[x]]--; $ } -InlinevoidGetans (intx) { -     intmx=0; -vis[x]=1; f[n][0]=1; A      for(intI=head[x];i;i=Nxt[i]) +         if(!Vis[to[i]]) { thedis[to[i]]=n+V[i]; -dep[to[i]]=1; $mxdeep=1; theDFS (TO[I],X); mx=Max (mx,mxdeep); theans+= (f[n][0]-1) *g[n][0]; theF (j,-mxdeep,mxdeep) theans+=f[n-j][1]*g[n+j][1]+f[n-j][0]*g[n+j][1]+f[n-j][1]*g[n+j][0]; -             //the reason for f[n][0]+1 is to count X as the legal path to the start or end point (G[n][1]). inF (j,n-mxdeep,n+mxdeep) thef[j][0]+=g[j][0], thef[j][1]+=g[j][1], Aboutg[j][0]=g[j][1]=0; the         } theF (I,N-MX,N+MX) f[i][0]=f[i][1]=0; the     //Statistical answers ↑ +      for(intI=head[x];i;i=Nxt[i]) -         if(!Vis[to[i]]) { thert=0; Size=S[to[i]];Bayi Getroot (to[i],x); the Getans (RT); the         } -     //continue to divide and conquer ↑ - } the intMain () { the #ifndef Online_judge theFreopen ("3697.in","R", stdin); theFreopen ("3697.out","W", stdout); - #endif then=getint (); theF (I,2, N) { the         intX=getint (), Y=getint (), z=getint ();94         if(!z) z--; the Add (x, y, z); the     } theSize=n; h[rt=0]=n+1;98Getroot (1,0); About Getans (RT); -printf"%lld\n", ans);101     return 0;102}
View Code 3697: The path of the drug-picking man time limit:10 Sec Memory limit:128 MB
submit:252 solved:93
[Submit] [Status] [Discuss] Description

The medicine field of the drug-collecting man is a tree-like structure, with the same medicinal herbs planted on each path.
The drug-taking people with their own unique opinion of the medicinal herbs, each kind of medicinal materials were classified. Broadly divided into two categories, one is negative, and the other is positive.
Drug-taking activities are carried out daily by the people who take the medicine. He chooses the path is very fastidious, he thinks the yin and yang balance is very important, so he must go is two kinds of medicinal herbs equal number of paths. It was hard work, so he hoped that the path he had chosen would be a node for the rest station (excluding the starting and ending points), and the path to the end of the stop and Rest station was balanced with yin and yang. He wanted to know how many different paths he could choose.

Input

Line 1th contains an integer n.
Next N-1 line, each line contains three integers a_i, b_i, and t_i, indicating the type of medicinal herbs on this route.

Output

The output conforms to the number of paths required by the drug-picking person.

Sample Input7
1 2 0
3 1 1
2 4 0
5 2 0
6 3 1
5 7 1
Sample Output1HINT

For 100% of data, n≤100,000.

Source [Submit] [Status] [Discuss]

"Bzoj" "3697" drug-picking man's Path & "3127" "USACO2013 Open" Yin and Yang

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.