BZOJ3631: [JLOI2014] Squirrel's new home

Source: Internet
Author: User

Transmission Door

The difference optimization on the tree, very simple one problem, should belong to the sub-problem of noip2015tgd2t3.

Bzoj 3631//by cydiater//2016.10.25#include <iostream> #include <cstdlib> #include <cstdio># Include <cmath> #include <ctime> #include <cstring> #include <string> #include <algorithm > #include <queue> #include <map> #include <bitset> #include <iomanip>using namespace std;# Define ll long long#define up (i,j,n) for (int. i=j;i<=n;i++) #define DOWN (i,j,n) for (int i=j;i>=n;i--) const int maxn= 6e5+5;const int Oo=0x3f3f3f3f;inline int read () {char ch=getchar (); int x=0,f=1;while (ch> ' 9 ' | | ch< ' 0 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} int n,a[maxn],link[maxn],lable[maxn],len=0,fa[maxn][25],dep[maxn];struct edge{int Y,next;} E[maxn];namespace solution{inline void Insert (int x,int y) {e[++len].next=link[x]; Link[x]=len;e[len].y=y;} void Init () {n=read (); Up (I,1,n) a[i]=read (); up (i,1,n-1) {int x=read (), Y=read (), insert (x, y), insert (Y,X);}} void Dfs (int node,int deep,int fatheR) {fa[node][0]=father;dep[node]=deep;for (int i=link[node];i;i=e[i].next) if (e[i].y!=father) DFS (e[i].y,deep+1, node);} void Get_ancestor () {Up (i,1,20) up (node,1,n) if (fa[node][i-1]!=0) fa[node][i]=fa[fa[node][i-1]][i-1];} int LCA (int x,int y) {if (x==y) return x;if (Dep[x]<dep[y]) swap (x, y);d Own (i,20,0) if (dep[x]-(1<<i) >=dep[y]) X=fa[x][i];if (x==y) return X;down (i,20,0) if (Fa[x][i]!=0&&fa[x][i]!=fa[y][i]) {x=fa[x][i];y=fa[y][i];} return fa[x][0];} void Re_dfs (int node) {for (int i=link[node];i;i=e[i].next) if (E[i].y!=fa[node][0]) {Re_dfs (E[I].Y); lable[node]+= LABLE[E[I].Y];}} void Slove () {DFS (1,0,0); Get_ancestor (); up (i,1,n-1) {int X=a[i],y=a[i+1],lca=lca (x, y); lable[x]++;lable[y]++;lable[ fa[lca][0]]--;lable[lca]--;} Re_dfs (1); Up (I,2,n) lable[a[i]]--;} void output () {Up (i,1,n) printf ("%d\n", Lable[i]);}} int main () {//freopen ("input.in", "R", stdin), using namespace Solution;init (); slove (); output (); return 0;}

BZOJ3631: [JLOI2014] Squirrel's new home

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.