zoukankan      html  css  js  c++  java
  • Network LCA修改点权

    Problem Description
    The ALPC company is now working on his own network system, which is connecting all N ALPC department. To economize on spending, the backbone network has only one router for each department, and N-1 optical fiber in total to connect all routers.
    The usual way to measure connecting speed is lag, or network latency, referring the time taken for a sent packet of data to be received at the other end.
    Now the network is on trial, and new photonic crystal fibers designed by ALPC42 is trying out, the lag on fibers can be ignored. That means, lag happened when message transport through the router. ALPC42 is trying to change routers to make the network faster, now he want to know that, which router, in any exactly time, between any pair of nodes, the K-th high latency is. He needs your help.
     
    Input
    There are only one test case in input file.
    Your program is able to get the information of N routers and N-1 fiber connections from input, and Q questions for two condition: 1. For some reason, the latency of one router changed. 2. Querying the K-th longest lag router between two routers.
    For each data case, two integers N and Q for first line. 0<=N<=80000, 0<=Q<=30000.
    Then n integers in second line refer to the latency of each router in the very beginning.
    Then N-1 lines followed, contains two integers x and y for each, telling there is a fiber connect router x and router y.
    Then q lines followed to describe questions, three numbers k, a, b for each line. If k=0, Telling the latency of router a, Ta changed to b; if k>0, asking the latency of the k-th longest lag router between a and b (include router a and b). 0<=b<100000000.
    A blank line follows after each case.
     
    Output
    For each question k>0, print a line to answer the latency time. Once there are less than k routers in the way, print "invalid request!" instead.
     
    Sample Input
    5 5 5 1 2 3 4 3 1 2 1 4 3 5 3 2 4 5 0 1 2 2 2 3 2 1 4 3 3 5
     
    Sample Output
    3 2 2 invalid request!
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  3071 3070 3072 3073 3074 
     
     
    一开始以为要选择第K大的这需要什么东西维护
    结果就是排序  (打扰了)
     

    题意:单case,一棵无根树,

    输入点数和操作数,下面一行n个值代表每个点的权。下面n-1行是树边

    操作分为

    0 x w ,表示把点x的权改为w

    k a b , 求出,从a到b的路径中,第k大的点权

    板子随便改一改就过了

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <queue>
      4 #include <cmath>
      5 #include <algorithm>
      6 #include <set>
      7 #include <iostream>
      8 #include <map>
      9 #include <stack>
     10 #include <string>
     11 #include <vector>
     12 #define  pi acos(-1.0)
     13 #define  eps 1e-6
     14 #define  fi first
     15 #define  se second
     16 #define  lson l,m,rt<<1
     17 #define  rson m+1,r,rt<<1|1
     18 #define  bug         printf("******
    ")
     19 #define  mem(a,b)    memset(a,b,sizeof(a))
     20 #define  fuck(x)     cout<<"["<<x<<"]"<<endl
     21 #define  f(a)        a*a
     22 #define  sf(n)       scanf("%d", &n)
     23 #define  sff(a,b)    scanf("%d %d", &a, &b)
     24 #define  sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
     25 #define  sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
     26 #define  pf          printf
     27 #define  FRE(i,a,b)  for(i = a; i <= b; i++)
     28 #define  FREE(i,a,b) for(i = a; i >= b; i--)
     29 #define  FRL(i,a,b)  for(i = a; i < b; i++)
     30 #define  FRLL(i,a,b) for(i = a; i > b; i--)
     31 #define  FIN         freopen("DATA.txt","r",stdin)
     32 #define  gcd(a,b)    __gcd(a,b)
     33 #define  lowbit(x)   x&-x
     34 #pragma  comment (linker,"/STACK:102400000,102400000")
     35 using namespace std;
     36 typedef long long LL;
     37 typedef unsigned long long ULL;
     38 const int maxn = 2e5 + 10;
     39 int _pow[maxn], dep[maxn], dis[maxn], vis[maxn], ver[maxn];
     40 int tot, head[maxn], dp[maxn * 2][30], k, first[maxn], path[maxn], val[maxn], fa[maxn];
     41 struct node {
     42     int u, v, w, nxt;
     43 } edge[maxn << 2];
     44 void init() {
     45     tot = 0;
     46     mem(head, -1);
     47 }
     48 void add(int u, int v, int w) {
     49     edge[tot].v = v, edge[tot].u = u;
     50     edge[tot].w = w, edge[tot].nxt = head[u];
     51     head[u] = tot++;
     52 }
     53 void dfs(int u, int DEP, int pre) {
     54     vis[u] = 1;
     55     ver[++k] = u;
     56     first[u] = k;
     57     dep[k] = DEP;
     58     fa[u] = pre;
     59     for (int i = head[u]; ~i; i = edge[i].nxt) {
     60         if (vis[edge[i].v]) continue;
     61         int v = edge[i].v, w = edge[i].w;
     62         dis[v] = dis[u] + w;
     63         dfs(v, DEP + 1, u);
     64         ver[++k] = u;
     65         dep[k] = DEP;
     66     }
     67 }
     68 void ST(int len) {
     69     int K = (int)(log((double)len) / log(2.0));
     70     for (int i = 1 ; i <= len ; i++) dp[i][0] = i;
     71     for (int j = 1 ; j <= K ; j++) {
     72         for (int i = 1 ; i + _pow[j] - 1 <= len ; i++) {
     73             int a = dp[i][j - 1], b = dp[i + _pow[j - 1]][j - 1];
     74             if (dep[a] < dep[b]) dp[i][j] = a;
     75             else dp[i][j] = b;
     76         }
     77     }
     78 }
     79 int RMQ(int x, int y) {
     80     int K = (int)(log((double)(y - x + 1)) / log(2.0));
     81     int a = dp[x][K], b = dp[y - _pow[K] + 1][K];
     82     if (dep[a] < dep[b]) return a;
     83     else return b;
     84 }
     85 int LCA(int u, int v) {
     86     int x = first[u], y = first[v];
     87     if (x > y) swap(x, y);
     88     int ret = RMQ(x, y);
     89     return ver[ret];
     90 }
     91 int cnt;
     92 void solve(int s, int t) {
     93     while(s != t) {
     94         path[cnt++] = val[s];
     95         s = fa[s];
     96         // printf("cnt=%d
    ",cnt);
     97     }
     98     path[cnt++] = val[t];
     99 }
    100 int cmp(int a, int b) {
    101     return a > b;
    102 }
    103 int main() {
    104     for (int i = 0 ; i < 40 ; i++) _pow[i] = (1 << i);
    105     int n, m;
    106     while(~sff(n, m)) {
    107         init();
    108         mem(vis, 0);
    109         mem(fa, 0);
    110         for (int i = 1 ; i <= n ; i++) sf(val[i]);
    111         for (int i = 0 ; i < n - 1 ; i++) {
    112             int u, v;
    113             sff(u, v);
    114             add(u, v, 0);
    115             add(v, u, 0);
    116         }
    117         k = 0, dis[1] = 0;
    118         dfs(1, 1, -1);
    119         ST(2 * n - 1);
    120         while(m--) {
    121             int op, u, v;
    122             sfff(op,u,v);
    123             if (op == 0) val[u] = v;
    124             else {
    125                 int lca = LCA(u, v);
    126                 //  printf("u=%d v=%d lca=%d
    ",u,v,lca);
    127                 cnt = 0;
    128                 solve(u, lca);
    129                 solve(v, lca);
    130                 cnt--;
    131               //  printf("cnt=%d
    ", cnt);
    132                 if (op > cnt) printf("invalid request!
    ");
    133                 else {
    134                     sort(path, path + cnt, cmp);
    135                     printf("%d
    ", path[op - 1]);
    136                 }
    137             }
    138         }
    139     }
    140     return  0;
    141 }
  • 相关阅读:
    我在项目内使用了设计模式后,同事直呼看不懂
    pom文件中依赖找不到的根本解决方法
    基于session的传统认证授权详解
    python中2个字典比较
    编码设计应遵循的规则
    yarn任务执行流程
    python3 中print 显示不全问题
    pandas 可视化
    python时间大小判断,相差天数秒数计算
    Impala任务程序cancle
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9448191.html
Copyright © 2011-2022 走看看