• Codeforecs Round #425 D Misha, Grisha and Underground (倍增LCA)


    D. Misha, Grisha and Underground
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations connected with n - 1 routes so that each route connects two stations, and it is possible to reach every station from any other.

    The boys decided to have fun and came up with a plan. Namely, in some day in the morning Misha will ride the underground from station sto station f by the shortest path, and will draw with aerosol an ugly text "Misha was here" on every station he will pass through (including sand f). After that on the same day at evening Grisha will ride from station t to station f by the shortest path and will count stations with Misha's text. After that at night the underground workers will wash the texts out, because the underground should be clean.

    The boys have already chosen three stations ab and c for each of several following days, one of them should be station s on that day, another should be station f, and the remaining should be station t. They became interested how they should choose these stations sft so that the number Grisha will count is as large as possible. They asked you for help.

    Input

    The first line contains two integers n and q (2 ≤ n ≤ 105, 1 ≤ q ≤ 105) — the number of stations and the number of days.

    The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi ≤ n). The integer pi means that there is a route between stations pi and i. It is guaranteed that it's possible to reach every station from any other.

    The next q lines contains three integers ab and c each (1 ≤ a, b, c ≤ n) — the ids of stations chosen by boys for some day. Note that some of these ids could be same.

    Output

    Print q lines. In the i-th of these lines print the maximum possible number Grisha can get counting when the stations st and f are chosen optimally from the three stations on the i-th day.

    题意:

      对于一个终点站f, 从s出发,和从t出发。经过的相同站有多少 

      有n个点,n-1条边。m次询问给出3个数 a, b, c。在a, b, c中选择一个终点两个起点,使得共同的站最大。

    题解:

      由于是一个树,所以就直接建树(点到点的距离设置为1)。暴力跑a,b, c分别为终点的情况。

      现在讨论a 为顶点的情况。

      

      那么公共的站点就是公共距离+1。

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <string>
      5 #include <algorithm>
      6 #include <cmath>
      7 #include <vector>
      8 #include <queue>
      9 #include <map>
     10 #include <stack>
     11 #include <set>
     12 using namespace std;
     13 typedef long long LL;
     14 typedef unsigned long long uLL;
     15 #define ms(a, b) memset(a, b, sizeof(a))
     16 #define pb push_back
     17 #define mp make_pair
     18 const LL INF = 0x7fffffff;
     19 const int inf = 0x3f3f3f3f;
     20 const int mod = 1e9+7;
     21 const int maxn = 100000+10;
     22 const int DEG = 20;
     23 struct node
     24 {
     25     int to, next;
     26     int w;
     27 }edge[2*maxn];
     28 int head[maxn], tol, flag[maxn];
     29 void init() {
     30     tol = 0;
     31     ms(head, -1);
     32     ms(flag, 0);
     33 }
     34 void addedge(int u, int v, int w)
     35 {
     36     edge[tol].to = v;
     37     edge[tol].next = head[u];
     38     edge[tol].w = w;
     39     head[u] = tol++;
     40 }
     41 int fa[maxn][DEG];
     42 int DD[maxn];
     43 int deg[maxn];
     44 void bfs(int root)
     45 {
     46     queue<int> que;
     47     deg[root] = 0;
     48     fa[root][0] = root;
     49     DD[root] = 0;
     50     que.push(root);
     51     while(!que.empty()){
     52         int tmp = que.front();
     53         que.pop();
     54         for(int i = 1;i<DEG;i++){
     55             fa[tmp][i] = fa[fa[tmp][i-1]][i-1];
     56         }
     57         for(int i = head[tmp];i!=-1;i=edge[i].next){
     58             int v = edge[i].to;
     59             if(v == fa[tmp][0])  continue;
     60             deg[v] = deg[tmp]+1;
     61             fa[v][0] = tmp;
     62             DD[v] = DD[tmp]+edge[i].w;
     63             que.push(v);
     64         }
     65     }
     66 }
     67 int LCA(int u, int v){
     68     int ans = 0;
     69     if(deg[u]>deg[v])   swap(u, v);
     70     int hu = deg[u], hv = deg[v];
     71     int tu = u, tv = v;
     72     for(int det = hv - hu, i=0;det;det>>=1, i++){
     73         if(det&1){
     74             tv = fa[tv][i];
     75         }
     76     }
     77     if(tu==tv){
     78         return tu;
     79     }
     80     for(int i = DEG - 1;i>=0;i--){
     81         if(fa[tu][i] == fa[tv][i])  continue;
     82         tu = fa[tu][i];
     83         tv = fa[tv][i];
     84     }
     85     return fa[tu][0];
     86 }
     87 int DIS(int u, int v)
     88 {
     89     return DD[u] + DD[v] - 2*DD[LCA(u, v)];
     90 }
     91 void solve()
     92 {
     93     int n, q;
     94     cin >> n >> q;
     95     for(int i = 2;i<=n;i++){
     96         int x;cin >> x;
     97         addedge(i, x, 1);
     98         addedge(x, i, 1);
     99         flag[x] = 1;
    100     }
    101     int root;
    102     for(int i = 1;i<=n;i++){
    103         if(!flag[i]){
    104             root = i;
    105             break;
    106         }
    107     }
    108 //    cout << root << endl;
    109     bfs(root);
    110     for(int i = 0;i<q;i++){
    111         int a, b, c;
    112         cin >> a >> b >> c;
    113         int ans = 0;
    114         ans = max(ans, (DIS(b, a)+DIS(c, a)-DIS(b, c))/2);
    115         ans = max(ans, (DIS(a, b)+DIS(c, b)-DIS(a, c))/2);
    116         ans = max(ans, (DIS(a, c)+DIS(b, c)-DIS(a, b))/2);
    117         cout << ans+1 << endl;
    118     }
    119 }
    120 int main() {
    121 #ifdef LOCAL
    122     freopen("input.txt", "r", stdin);
    123 //        freopen("output.txt", "w", stdout);
    124 #endif
    125     ios::sync_with_stdio(0);
    126     cin.tie(0);
    127     init();
    128     solve();
    129 }
    View Code
  • 相关阅读:
    (转)CSS3全局实现所有元素的内边距和边框不增加
    (转载)常用的Mysql数据库操作语句大全
    100天搞定机器学习|day40-42 Tensorflow Keras识别猫狗
    《统计学习方法》极简笔记P2:感知机数学推导
    100天搞定机器学习|day39 Tensorflow Keras手写数字识别
    100天搞定机器学习|day38 反向传播算法推导
    100天搞定机器学习|day37 无公式理解反向传播算法之精髓
    100天搞定机器学习|Day36用有趣的方式解释梯度下降算法
    100天搞定机器学习|Day35 深度学习之神经网络的结构
    《统计学习方法》极简笔记P4:朴素贝叶斯公式推导
  • 原文地址:https://www.cnblogs.com/denghaiquan/p/7242595.html
Copyright © 2020-2023  润新知