Bzoj 1103 POI 2007 Metropolitan Meg Tree Array

Source: Internet
Author: User

The main idea: to give a tree, the beginning of every two points are connected by dirt road, but there will be some dirt road gradually into the road, asked each time from point 1 to the number of points K how many dirt road.


Ideas: POI is not difficult problem, in fact, each point to 1 of the number of dirt road is the depth of this point, in the dirt road into the road, the point and the subtree of all the nodes of the depth of 1, sub-tree modification is very basic, you can use the DFS sequence +fenwick, of course, if not too troublesome can also be tree chain But constants will compare cards.


CODE:


#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 250010using namespace Std;int points,asks;int head[max],total;int _next[max << 1],aim[max << 1];int Deep[max ],cnt;pair<int,int> range[max];int fenwick[max];char c[10];inline void Add (int x,int y) {_next[++total] = head[x]; Aim[total] = y;head[x] = total;} void DFS (int x,int last) {deep[x] = Deep[last] + 1;range[x].first = ++cnt;for (int i = head[x]; i; i = _next[i]) {if (Aim[i] = = last) Continue;dfs (aim[i],x);} Range[x].second = CNT;} inline void Fix (int x,int c) {for (; x; x = x&-x) fenwick[x]-= c;} inline int getsum (int x) {int re = 0;for (; x <= points; x + = x&-x) Re + = Fenwick[x];return Re;} int main () {cin >> points;for (int x,y,i = 1; i < points; ++i) {scanf ("%d%d", &x,&y); Add (x, y), add (y,x);} Deep[0] = -1;dfs (1,0), cin >> asks;for (int x,y,i = 1; I <= asks + points-1; ++i) {scanf ("%s", c); if (c[0] = = ' A ') {scanf ("%d%d", &x,&y); intopt = deep[x] > deep[y]? x:y; Fix (range[opt].first-1,1); Fix (range[opt].second,-1);} else {scanf ("%d", &x);p rintf ("%d\n", Deep[x]-getsum (Range[x].first));}} return 0;}


Bzoj 1103 POI 2007 Metropolitan Meg Tree Array

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.