Codeforces 191 C Fools and Roads (tree chain split)

Source: Internet
Author: User

Topic Link ~~>

do the problem mood: do the hdu 5044 after feeling very easy.

Problem Solving Ideas:

First, the tree chain split, the tree is divided into chains, because the final question is all asked, so~ can be linear operation. After the tree chain is split, there will be a lot of chains, but each edge has a number, equivalent to an array of linear operation, so. Add 1 if you go in the U ~ V. Then you can make sum [u] + = 1; sum [v + 1]-= 1; Here if the number of V is large.

At the end of the day, you just have to walk backwards from the back. Get all the results. The idea of a tree-chain split after that is clear.

Code:

#include <iostream> #include <sstream> #include <map> #include <cmath> #include <fstream> #include <queue> #include <vector> #include <sstream> #include <cstring> #include <cstdio > #include <stack> #include <bitset> #include <ctime> #include <string> #include <cctype  > #include <iomanip> #include <algorithm>using namespace std, #define INT __int64#define L (x) (x * 2) #define  R (x) (x * 2 + 1) const int INF = 0X3F3F3F3F; Const double ESP = 0.0000000001; const double PI = ACOs ( -1.0); const INT MOD = 1e9 + 7; const int MY = 1400 + 5; const int MX = 100000 + 5; int n, num, idx, m; int head[mx], sum[mx], ans[mx], p[mx] , Ti[mx], dep[mx], top[mx], siz[mx], son[mx], father[mx]; struct node{int u, v;} E[MX]; struct node{int V, Next;} E[MX*2]; void Addedge (int u, int v) {e[num].v = v; E[num].next = Head[u];    Head[u] = num++; E[NUM].V = u; E[num].next = Head[v]; HEAD[V] = num++;}   void Dfs_find (int u, int fa) { Dep[u] = Dep[fa] + 1;    Siz[u] = 1;    Son[u] = 0;    Father[u] = FA;        for (int i = head[u]; i =-1; i = e[i].next) {int v = E[I].V;        if (v = = FA) continue;        Dfs_find (V, u);        Siz[u] + = Siz[v];    if (Siz[son[u]] < SIZ[V]) son[u] = v;    }}void dfs_time (int u, int fa) {Ti[u] = idx++;    Top[u] = FA;    if (Son[u]) dfs_time (Son[u], top[u]);        for (int i = head[u]; i =-1; i = e[i].next) {int v = E[I].V;        if (v = = Father[u] | | v = = Son[u]) continue;    Dfs_time (V, v); }}void LCA (int u, int v) {while (Top[u]! = Top[v]) {if (Dep[top[u]] < DEP[TOP[V]) swap (U, v)        ;        SUM[TI[U]+1]-= 1;        Sum[ti[top[u]] + = 1;    U = father[top[u]];    } if (Dep[u] > Dep[v]) Swap (U, v);        if (U = v) {Sum[ti[son[u]] + = 1;    SUM[TI[V]+1]-= 1;    }}int Main () {//freopen ("Input.txt", "R", stdin);    int u, v;     while (~SCANF ("%d", &n)) {   num = 0;        memset (head, -1, sizeof (head));        memset (sum, 0, sizeof (sum));            for (int i = 1; i < n; ++i) {scanf ("%d%d", &e[i].u, &AMP;E[I].V);        Addedge (e[i].u, E[I].V);        } dep[1] = siz[0] = 0;        Dfs_find (1, 1);        IDX = 1;        Dfs_time (1, 1);        scanf ("%d", &m);           for (int i = 0; i < m; ++i) {scanf ("%d%d", &u, &v);        LCA (U, v); } for (int i = 1; I <= n; ++i)//First Edge {if (dep[e[i].u] < DEP[E[I].V]) swap (            E[I].U, E[I].V);        P[TI[E[I].U]] = i;            } for (int i = 1; I <= n; ++i)//number of edges in Split {sum[i] + = sum[i-1];        Ans[p[i]] = sum[i];        } printf ("%d", ans[1]);        for (int i = 2; i < n; ++i) printf ("%d", ans[i]);    Puts (""); } return 0;}




Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Codeforces 191 C Fools and Roads (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.