HDU 3078 Network

Network


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!

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int N=8e4+5;
vector<int>G[N];
int f[N][32],d[N],w[N],n,q;
bool cmp(int a,int b)
{
    return a>b;
}
void dfs(int u,int fa,int dep)
{
    d[u]=dep;
    f[u][0]=fa;
    int len=G[u].size();
    for(int i=0;i<len;i++)
    {
        if(G[u][i]==fa)continue;
        dfs(G[u][i],u,dep+1);
    }
}
void bz()
{
    for(int j=1;j<=30;j++)
        for(int i=1;i<=n;i++)
            f[i][j]=f[f[i][j-1]][j-1];
}
int query(int u,int v)
{
    if(d[u]<d[v])swap(u,v);
    int dc=d[u]-d[v];
    for(int i=0;i<30;i++)
        if(dc&(1<<i))
            u=f[u][i];
    if(u==v)return u;
    for(int i=30;i>=0;i--)
        if(f[u][i]!=f[v][i])
            u=f[u][i],v=f[v][i];
    return f[u][0];
}
int main()
{
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)
        scanf("%d",&w[i]);
    for(int i=0;i<n-1;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(1,0,0);
    bz();
    for(int i=0;i<q;i++)
    {
        int k,a,b;
        scanf("%d%d%d",&k,&a,&b);
        if(k==0)w[a]=b;
        else
        {
            int lca=query(a,b);
            vector<int>rout;
            while(a!=lca)
            {
                rout.push_back(w[a]);
                a=f[a][0];
            }
            while(b!=lca)
            {
                rout.push_back(w[b]);
                b=f[b][0];
            }
            rout.push_back(w[lca]);
            if(rout.size()<k)
            {
                puts("invalid request!");
                continue;
            }
            sort(rout.begin(),rout.end(),cmp);
            printf("%d\n",rout.at(k-1));
        }
    }
    return 0;
}
时间: 2024-08-03 18:54:00

HDU 3078 Network的相关文章

HDU 3078 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 connec

HDU - 3078 Network(暴力+LCA)

题目大意:给出n个点的权值,m条边,2种操作 0 u num,将第u个点的权值改成num k u v,询问u到v这条路上第k大的权值点 解题思路:该点的话直接该,找第k大的话直接暴力 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define N 80010 #define M 160010 struct Edge{ int to, next, val; }

hdu 3078 Network lca

题目链接 给一棵树, m个操作, 一共两种操作, 将第x个点的权值改为y, 询问x->y路径上权值第k大的点的权值. 暴力的找..... 1 #include <iostream> 2 #include <vector> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <cmath> 7 #include <map>

HDU 2460 Network(双连通+树链剖分+线段树)

HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链剖分+线段树处理 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; #pragma comment(linke

HDU 3078:Network(LCA之tarjan)

http://acm.hdu.edu.cn/showproblem.php?pid=3078 题意:给出n个点n-1条边m个询问,每个点有个权值,询问中有k,u,v,当k = 0的情况是将u的权值修改成v,当k不为0的情况是问u和v的路径中权值第k大的点的权值是多少. 思路:比较暴力的方法,可能数据太水勉强混过去了.对于每一个询问的时候保留两个点之间的lca,还有计算出两个点之间的点的个数(询问的时候如果点的个数小于k就不用算了),然后tarjan算完之后对每个询问再暴力路径上的每个点放进vec

HDU 3078 (LCA+树链第K大)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3078 题目大意:定点修改.查询树中任意一条树链上,第K大值. 解题思路: 先用离线Tarjan把每个Query树链的LCA求出来. LCA中对连接树Dfs的时候,令p[v]=u,记录v的前驱. LCA结束后,对于每个Query: 从u开始回溯到LCA,记录值.从v开始回溯到LCA,记录值. 再加上LCA这个点的值,形成一条完整树链.特判树链长度是否小于K. 对树链中的值,从大到小排序,取第K大即可

hdu 3078(LCA的在线算法)

Network Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 847    Accepted Submission(s): 347 Problem Description The ALPC company is now working on his own network system, which is connecting all

HDU 2460 Network 傻逼Tarjan

Network Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1014    Accepted Submission(s): 206 Problem Description A network administrator manages a large network. The network consists of N comput

HDU 2460 Network(桥+LCA)

http://acm.hdu.edu.cn/showproblem.php?pid=2460 题意:给出图,求每次增加一条边后图中桥的数量. 思路: 先用tarjan算法找出图中所有的桥,如果lowv>pre[u],那么u—v就是桥,此时可以标记一下v. 之后就是利用LCA,找到两个节点的公共祖先,在这条路径上的桥就不再是桥了.(此时就相当于这些桥组成的树,可以在脑海中缩点) 1 #include<iostream> 2 #include<algorithm> 3 #incl