diameter and multiplication of URAL 1752 tree 2 Trees

Source: Internet
Author: User

Assume that the two endpoints of the tree's diameter are p0,p1. If a point q satisfies the requirement for a single query (v,k), then Q must be on the path from V to P0 or V to P1.

The rest is to look for p in the tree. Multiply it.

#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < cstdio> #include <queue> #include <cmath> #include <stack> #include <map> #define LL long long# Define ULL unsigned long long#define INF 0x3f3f3f3fusing namespace std;const int maxn = 20010;const int MAXM = 15;struct N {int v,next;}    Edge[2*maxn];int head[maxn];int top;void Link (int u,int v) {edge[top].v = v;    Edge[top].next = Head[u]; Head[u] = top++;}    int pa[2][maxn][maxm];int dep[2][maxn];void INITDEPFA (int s,int lay,int pre = -1,int d = 0) {Dep[lay][s] = D;    Pa[lay][s][0] = pre; for (int p = head[s];p! =-1; p = edge[p].next) {if (edge[p].v! = Pre) INITDEPFA (edge[p].v,lay,s,d+1)    ;    }}void initpa (int n,int lay) {int i,j; for (j = 1;j < Maxm; ++j) {for (i = 1;i <= n; ++i) {if (pa[lay][i][j-1]! =-1  ) Pa[lay][i][j] = pa[lay][pa[lay][i][j-1]][j-1];              else Pa[lay][i][j] =-1; }}}int querykthpoint (int u,int k,int lay) {for (int i = maxm-1;i >= 0 && k;-i) {if (K >= (    1<<i)) U = pa[lay][u][i],k-= (1<<i); } return u;}    Queue<int> q;bool mark[maxn];int BFS (int s) {memset (mark,false,sizeof (Mark));    Mark[s] = true;    Q.push (s);        while (q.empty () = = False) {s = Q.front ();        Q.pop (); for (int p = head[s];p! =-1; p = edge[p].next) if (mark[edge[p].v] = = False) Q.push (EDGE[P].V), mark[    s] = true; } return s;}    int main () {int i,u,v,n,m,a,k;        while (scanf ("%d%d", &n,&m)! = EOF) {memset (head,-1,sizeof (head));        Top = 0;            for (i = 1;i < n; ++i) {scanf ("%d%d", &u,&v);            Link (U,V);        Link (V,u);        } u = BFS (v = BFS (1));        INITDEPFA (u,0);        INITPA (n,0);        INITDEPFA (v,1); INITPA (n,1);            while (m--) {scanf ("%d%d", &a,&k);            if (Dep[0][a] >=k) printf ("%d\n", Querykthpoint (a,k,0));            else if (Dep[1][a] >= k) printf ("%d\n", Querykthpoint (a,k,1));        else printf ("0\n"); }} return 0;}

diameter and multiplication of URAL 1752 tree 2 Trees

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.