HDU 6203 ping Ping ping [LCA, greedy, DFS sequence, BIT (tree-like array)]

Source: Internet
Author: User

Topic Link: "http://acm.hdu.edu.cn/showproblem.php?pid=6203"

Test instructions: Give a tree, if (A, B) the path has a bad point, then (A, B) is not connected, give some of the wrong point pair, and then determine the minimum number of bad points.

The following: The LCA of each point pair is then sorted according to the depth of the LCA. Starting with the deepest point pair of the LCA, if a or B point is already a bit marked, then continue, no, the sub-tree of the LCA (b) is added 1 per vertex.

#include <bits/stdc++.h>using namespace Std;const int maxn = 2e5 + 5;int N, Q;int C[MAXN], IN[MAXN], OUT[MAXN], cnt;s    truct edge{int id, next; Edge (int id = 0, int next = 0): ID (ID), next (next) {}} E[MAXN << 1];    bidirectional Edge int HEAD[MAXN], tot;void Initedge () {memset (c, 0, sizeof (c));    CNT = 0;    for (int i = 0; I <= N; i++) head[i] = 1; tot = 0;}    void Addedge (int u, int v) {E[tot] = Edge (V, Head[u]); Head[u] = tot++;}    int SIZ[MAXN], TOP[MAXN], DEP[MAXN], Fa[maxn];int clk;//requires initialization of void DFS1 (int u, int p) {In[u] = ++cnt;    Dep[u] = dep[p] + 1;    Fa[u] = p, siz[u] = 1;        for (int k = Head[u]; K! =-1; k = e[k].next) {int id = e[k].id;        if (id = = p) continue;        DFS1 (ID, u);    Siz[u] + = Siz[id]; } Out[u] = cnt;}    void DFS2 (int u, int p) {int son =-1;        for (int k = Head[u]; K! =-1; k = e[k].next)//Find heavy son {int id = e[k].id;        if (id = = p) continue;    if (son = =-1 | | siz[id] > Siz[son]) son = ID; } if (sOn! =-1) {Top[son] = Top[u];    DFS2 (son, u);        } for (int k = Head[u]; K! =-1; k = e[k].next) {int id = e[k].id;        if (id = = P | | id = = son) continue;        TOP[ID] = ID;    DFS2 (ID, u);        }}int LCA (int x, int y) {while (top[x]! = Top[y]) {if (Dep[top[x]] < Dep[top[y]]) swap (x, y);    x = fa[top[x]];    } if (Dep[x] > Dep[y]) swap (x, y); return x;}    struct node{int u, V, LCA;    BOOL operator < (const node &t) const {return DEP[LCA] > DEP[T.LCA]; }} qry[50050];int low_bit (int x) {return x & x;}        void update (int x, int val) {while (x <= N) {c[x] + = val;    x + = Low_bit (x);    }}int get_sum (int x) {int ret = 0;        while (x > 0) {ret + = c[x];    X-= Low_bit (x); } return ret;}        int main () {while (~SCANF ("%d", &n)) {n++;        Initedge ();            for (int i = 1; I <= N-1; i++) {int u, v; scanf ("%d%d ", &u, &v);            u++, v++;        Addedge (U, v), Addedge (V, u);        } DFS1 (1, 0), top[1] = 1,DFS2 (1, 0);        scanf ("%d", &q);            for (int i = 1; I <= Q; i++) {scanf ("%d%d", &qry[i].u, &AMP;QRY[I].V);            qry[i].u++, qry[i].v++;        QRY[I].LCA = LCA (qry[i].u, QRY[I].V);        } sort (qry + 1, qry + 1 + Q);        int ans = 0;            for (int i = 1; I <= Q; i++) {int u = QRY[I].LCA;            int ret = Get_sum (in[qry[i].u]) + get_sum (IN[QRY[I].V]);            if (ret) continue;            ans++;            Update (In[u], 1);        Update (Out[u] + 1,-1);    } printf ("%d\n", ans); } return 0;}

  

HDU 6203 ping Ping ping [LCA, greedy, DFS sequence, BIT (tree-like 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.