Bzoj 3631: [JLOI2014] Squirrel's new home (tree-chain split)

Source: Internet
Author: User

Bare Tree chain split ...

-------------------------------------------------------------------

#include <bits/stdc++.h>using namespace std;const int MAXN = 300009;struct Edge {int to;edge* Next;} E[MAXN << 1], *pit = E, *HEAD[MAXN];inline void Add (int u, int v) {pit->to = v;pit->next = Head[u];Head[u] = pit++;}#define Addedge (U, v) Add (U, v), add (V, u)#define M (L, R) (((L) + (R)) >> 1)int DEP[MAXN], SIZE[MAXN], SON[MAXN], FA[MAXN], TOP[MAXN], ID[MAXN], top, CK = 0;void Dfs (int x) {Son[x] =-(size[x] = 1);For (edge* e = head[x]; e; e = e->next) if (e->to! = Fa[x]) {Dep[e->to] = dep[x] + 1;fa[e->to] = x;DFS (e->to);Size[x] + = size[e->to];if (!~son[x] | | size[son[x]] < size[e->to])son[x] = e->to;}} void DFS (int x) {top[x] = top;id[x] = ++ck;if (~son[x]) DFS (son[x]);For (edge* e = head[x]; e; e = e->next)if (e->to! = son[x] && e->to! = fa[x]) DFS (TOP = e->to);}void init () {dfs (Dep[0] = 0);DFS (TOP = 0);}struct Node {Node *l, *r;int add;Node (): Add (0) {}} POOL[MAXN], *pt = Pool, *root;void Build (node* t, int l, int r) {if (R > L) {int m = m (l, R);Build (T->l = pt++, L, m);Build (T->r = pt++, M + 1, R);}}int L, R, N;void Modify (node* t, int l, int r) {if (l <= l && R <= R)t->add++;else {int m = m (l, R);if (l <= m) modify (T->l, L, m);if (M < R) Modify (T->r, M + 1, R);}}int query (node* t, int l, int r) {R + = t->add;if (L = = r)return R;int m = m (l, R);return L <= m? Query (T->l, L, m): query (T->r, M + 1, R);}void Change (int x, int y) {For (; top[x]! = Top[y]; x = fa[top[x]]) {if (Dep[top[x]] < dep[top[y]) swap (x, y);L = id[top[x]]; R = id[x];Modify (root, 1, N);}if (Dep[x] < dep[y]) swap (x, y);L = Id[y]; R = id[x];Modify (root, 1, N);}int PATH[MAXN];int main () {cin >> N;for (int i = 0; i < N; i++)scanf ("%d", path + i), path[i]--;for (int i = 0; i < N-1; i++) {int u, v; scanf ("%d%d", &u, &v);--u;--v ;Addedge (U, v);}init ();Build (Root = pt++, 1, N);for (int i = 1; i < N; i++)Change (Path[i-1], path[i]);for (int i = 0; i < N; i++) {L = Id[i]; R = 0;int t = query (root, 1, N);if (i! = path[0]) t--;printf ("%d\n", t);}return 0;}

-------------------------------------------------------------------

3631: [JLOI2014] Squirrel's new home time limit: ten Sec Memory Limit: MB
Submit: 855 Solved: 412
[Submit] [Status] [Discuss] DescriptionSquirrel's new home is a tree, a few days ago just renovated a new home, the new home has n rooms, and there are n-1 root branches connected, each room can reach each other, and the two rooms between the route is unique. God, he actually lived on the tree. Squirrels want to invite Winnie the Pooh to visit, and also designate a visit guide, he hoped Pooh can follow his guide order, first go to A1, then go to A2, ..., finally to an, to visit a new home. but this will cause the Pooh to repeat a lot of rooms, lazy pooh do not listen to refuse. But the squirrel told him, every walk to a room, he can take a piece of candy from the room to eat. Pooh was a greedy fellow, and he agreed immediately. now squirrels want to know that in order to ensure that Pooh has candy to eat, he needs to put at least how many sweets in each room. Because the squirrel visited the last room in the guide, an is a restaurant, the restaurant he prepared a hearty meal, so when Pooh at the end of the visit to the restaurant will not need to take the candy to eat. Inputfirst line an integer n, indicating the number of roomsthe second row of n integers, in turn, describes the A1-anNext n-1 line, two integers per line x, Y, indicates that the two rooms labeled X and Y are connected by a branch. OutputA total of n rows, the first row of output labeled I of the room need to put at least how many sweets, in order to let Pooh have candy to eat. Sample Input5
1 4 5) 3 2
1 2
2 4
2 3
4 5

Sample Output1
2
1
2
1
HINT

2<= N <=300000

Source

Bzoj 3631: [JLOI2014] Squirrel's new home (tree-chain split)

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.