HDU 3078 - Network (LCA)【tarjan离线/DFS倍增】

当GO.db安装失败时,生物信息学新手应该知道的5个底层逻辑 本文为生物信息学新手提供了解决GO.db安装失败的5个关键底层逻辑,包括网络连接与镜像源设置、版本兼容性检查、依赖关系管理、SQLite存储机制理解以及调试技巧。特别针对Bioconductor的GO.db包安装问题,详细介绍了如何通过切换国内镜像、管理版本冲突和解决依赖问题来成功安装,帮助用户高效克服常见安装障碍。 阅读详情

Network

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1628    Accepted Submission(s): 726


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 55 1 2 3 43 12 14 35 32 4 50 1 22 2 32 1 43 3 5
 

Sample Output

 
322invalid request!
 

Source
 

找出a,b到他们lca的路径上的第k大点值。


POINT:

找出lca,模拟把每个点找到。这样理论上不能ac。因为数据弱,水过去了。


#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
#define LL long long
#define lt x<<1
#define rt x<<1|1
const int maxn = 80000+111;
const int inf = 0x3f3f3f3f;
int val[maxn];
int head[maxn],to[maxn<<1],nxt[maxn<<1],cnt=0;
int qhead[maxn],qto[maxn],qflag[maxn],qnxt[maxn],qcnt=0;
int pre[maxn],fa[maxn],vis[maxn];


void add(int u,int v)
{
    to[cnt]=v;
    nxt[cnt]=head[u];
    head[u]=cnt++;
}

void add1(int u,int v,int flag)
{
    qto[qcnt]=v;
    qflag[qcnt]=flag;
    qnxt[qcnt]=qhead[u];
    qhead[u]=qcnt++;
}


struct node
{
    int k,a,b;
    int ans;
}qu[maxn];

int find(int x)
{
    return fa[x]==x?x:fa[x]=find(fa[x]);
}

void merge(int x,int y)
{
    int xx=find(x);
    int yy=find(y);
    if(xx!=yy)
        fa[yy]=xx;
}

void tarjan(int u)
{
    vis[u]=1;
    for(int i=head[u];~i;i=nxt[i]){
        int v=to[i];
        if(vis[v]) continue;
        tarjan(v);
        merge(u,v);
        pre[v]=u;
    }
    for(int i=qhead[u];~i;i=qnxt[i]){
        int v=qto[i];
        if(vis[v]){
            qu[qflag[i]].ans=find(v);
        }
    }
}

bool cmd(int a,int b)
{
    return a>b;
}
int main()
{
    int n,q;
    scanf("%d %d",&n,&q);
    for(int i=1;i<=n;i++){
        scanf("%d",&val[i]);
        qhead[i]=head[i]=-1;
        fa[i]=i;
    }
    for(int i=1;i<n;i++){
        int u,v;scanf("%d %d",&u,&v);
        add(u,v);add(v,u);
    }
    for(int i=1;i<=q;i++){
        scanf("%d %d %d",&qu[i].k,&qu[i].a,&qu[i].b);
        if(qu[i].k>=1){
            add1(qu[i].a,qu[i].b,i);
            add1(qu[i].b,qu[i].a,i);
        }
    }
    tarjan(1);
    for(int i=1;i<=q;i++){
        vector<int>v;
        int k=qu[i].k;
        int a=qu[i].a;
        int b=qu[i].b;
        if(k==0){
            val[a]=b;
        }else{
            v.clear();
            int now = a;
            while(now!=qu[i].ans){
                v.push_back(val[now]);
                now=pre[now];
            }
            now=b;
            while(now!=qu[i].ans){
                v.push_back(val[now]);
                now=pre[now];
            }
            v.push_back(val[qu[i].ans]);
            if(v.size()<k){
                printf("invalid request!\n");
            }else{
                sort(v.begin(),v.end(),cmd);
                printf("%d\n",v[k-1]);
            }

        }
    }
//    printf("\n");

    
    return 0;
}



#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
#define LL long long
#define lt x<<1
#define rt x<<1|1
const int maxn = 80000+5;
int head[maxn],to[maxn<<1],nxt[maxn<<1],cnt=0;
int fa[maxn][17],d[maxn];
int val[maxn],vis[maxn];
int n,Q;
void add(int x,int y)
{
    to[cnt]=y;
    nxt[cnt]=head[x];
    head[x]=cnt++;
}
void dfs(int u)
{
    vis[u]=1;
    for(int i=head[u];~i;i=nxt[i]){
        int v=to[i];
        if(vis[v]) continue;
        d[v]=d[u]+1;
        dfs(v);
        fa[v][0]=u;
    }
}
void init()
{
    for(int i=1;(1<<i)<=n;i++)
    for(int j=1;j<=n;j++){
        fa[j][i]=fa[fa[j][i-1]][i-1];
    }
}

int query(int a,int b)
{
    if(d[a]<d[b]) swap(a, b);
    int dis=d[a]-d[b];
    int c=0;
    while(dis){
        if(dis&1)
            a=fa[a][c];
        dis/=2;
        c++;
    }
    if(a==b) return a;
    else{
        for(int i=(int)log2(n);i>=0;i--){
            if(fa[a][i]!=fa[b][i])
                a=fa[a][i],b=fa[b][i];
        }
        a=fa[a][0];
        return a;
    }
}
bool cmd(int a,int b)
{
    return a>b;
}
int main()
{
    scanf("%d %d",&n,&Q);
    memset(head,-1,sizeof head);
    for(int i=1;i<=n;i++) scanf("%d",&val[i]);
    for(int i=1;i<n;i++){
        int u,v;scanf("%d %d",&u,&v);
        add(u,v);add(v,u);
    }
    d[0]=0;
    dfs(1);
    init();
    while(Q--){
        vector<int>v;
        int k,a,b;
        scanf("%d %d %d",&k,&a,&b);
        if(k==0){
            val[a]=b;
        }else{
            v.clear();
            int now = a;
            int ans=query(a, b);
//            printf("%d\n",ans);
//            int ci=0;//祖先找的不对!!!或者pre数组
            while(now!=ans){
                v.push_back(val[now]);
                now=fa[now][0];
            }
            now=b;
            while(now!=ans){
                v.push_back(val[now]);
                now=fa[now][0];
            }
            v.push_back(val[ans]);
            if(v.size()<k){
                printf("invalid request!\n");
            }else{
                sort(v.begin(),v.end(),cmd);
                printf("%d\n",v[k-1]);
            }

        }
    }

    return 0;
}


生信人填坑记 orthofinder的安装和基础使用 其中常用的有两个结果,第一个是Orthogroups文件夹下的Orthogroups.GeneCount.tsv,这个文件里保存着每个家族在每个物种里的成员数量。(写到这儿饭点到了,吃饭要紧…在国内使用conda的同学我相信很多人都会遇到-|- -|- -|- -|- -|- -|- -|- -|- 一直转圈。软件的是由很简单,将你要跑的物种的蛋白质文件放在work/data目录下,文件名都用物种缩写,后缀统一就行。当然,很多软件都会自带一个这样的文件,conda装不了的时候可以去软件官网溜达溜达。 阅读详情

相关推荐

手把手排查IOMMU映射故障:从内核日志到硬件寄存器调试全指南

本文详细解析了IOMMU映射故障的排查方法,从内核日志分析到硬件寄存器调试,提供了一套完整的诊断流程。涵盖AMD和Intel平台的特定调试技巧,包括ATS协商失败、DMA传输错误等常见问题的解决方案,并介绍了高级调试工具链的搭建与性能优化策略。

weixin_42666036的博客 357

hdu-3078 Network (树上倍增LCA+暴力)

原题链接 题意: 给出点的个数n和操作数Q,给出n个点的权值,给出n-1条边的连接方式,要及时回答Q个询问,询问包括三个数字k,x,y,如果k=0,那么就将x点的权值改为y,如果k!=0,就求出x到y的最短路径上经过所有点(含本身)的第k大的权值。 思路: 利用在线LCA树上倍增算法,求x,y的LCA然后分别从x,y出发往上走记录到lca经过的所有点的权值,然后sort排序输出就行算法用时171m...

Weng Shihao's blog 243

单细胞测序流程(八)单细胞的marker基因转化和​GO富集分析

系列文章目录 单细胞测序流程(一)简介与数据下载 单细胞测序流程(二)数据整理 单细胞测序流程(三)质控和数据过滤——Seurat包分析,小提琴图和基因离差散点图 单细胞测序流程(四)主成分分析——PCA 单细胞测序流程(五)t-sne聚类分析和寻找marker基因 单细胞测序流程(六)单细胞的细胞类型的注释 单细胞测序流程(七)单细胞的细胞类型轨迹分析———————————————— 本期主讲内容——单细胞的maeker基因转化和​富集分析 marker基因转化是为了将基因的id和基.

qq_45478665的博客 1万+

HDU - 3078 - Network (倍增LCA)

题目链接:https://cn.vjudge.net/problem/HDU-3078 题意:给定一棵树,树上每个点都有权值,输入三个数有两种操作: k a b,k == 0时把a点的权值改为b,k > 0时求a到b路径上第k大的权值 ,将点的权值改为。 ,求点到点路径上的第大权值。 思路:时直接修改就行了,时,考虑到点到点路径上的第大权值,我们可以把点...

sugarbliss 457

矩阵LED和矩阵按键

先说矩阵LED,如图: 下面以行几、列几的表达方法说明 三极管可以当做开关使用 由图可知,如果想让一个LED亮,那么它对应的行和列的三极管都要导通。如果这个矩阵每次只亮一个LED,那么正常写程序没问题。 如果我们想让LED1和LED6亮,其他灯不亮,则需要开启行1、行2、列1、列2,随之带来的问题就是LED2和LED5也会跟着亮起来。 解决方法类似于扫描,因为单片机运行速度很快,可以在很短的时间内开关数次而肉眼看不到。所以我们可以每次只亮一列(排)灯,亮单位时间后关掉同时开启另外一列(排),如此四次一循环

qq_46615634的博客 5794

别只聊天了!手把手教你为Ollama+Open WebUI搭建本地知识库(基于BGE-M3和Qwen)

本文详细介绍了如何利用Ollama、Open WebUI和BGE-M3构建本地知识库,实现智能问答和文档检索。通过环境准备、文档处理、向量化实战和应用案例,帮助用户突破基础聊天场景,打造高效的个人知识管理体系。特别适合技术文档管理和学术研究场景。

weixin_42523720的博客 479

HDU-3078-Network(LCA+暴力排序)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3078 题目大意:给出n个节点,n-1条边,q次询问。每个节点都有自己的值。然后是n-1条边。之后是q次查询,有两种状态,k==0,将a的值赋为b,k>0查询a~b最短路上面第k大的值。 思路:正解不会,看着测试数据非常水,就LCA,暴力排序,O(q*n*logn):24*1e8*longn的...

永远鲜红の幼月的博客 357

从零开始掌握Python机器学习:七步教程 基础篇

前言 「开始」往往是最难的,尤其是当选择太多的时候,一个人往往很难下定决定做出选择。本教程的目的是帮助几乎没有 Python 机器学习背景的新手成长为知识渊博的实践者,而且这个过程中仅需要使用免费的材料和资源即可。 第一步:基本 Python 技能 如果我们打算利用 Python 来执行机器学习,那么对 Python 有一些基本的了解就是至关重要的。 首先,你需要安装 Python。因为我们后...

hcgx2018的博客 1613

HDU 3078 Network (LCA)

题意:有n个点,n-1条双向边,每个点有一个值val,现在有q次询问(op,a,b),if(op==0)将a点的值改成b,否则,求a-->b 间所有点的第k大的值,包括a,b点。 解: 先求出a,b点的LCA,然后分别从a,b点询问到lca,记录下路径中所有的点,排序即可,修改操作直接修改。         由于tarjan是离线的算法,题中有修改操作,tarjan 不适合使用//RMQ求L

swust_lian的博客 422

LeetCode 热题 100 回顾

力扣最受刷题发烧友欢迎的 100 道题 LeetCode 热题 100 : 题目回顾、解题思路、复杂度分析、代码实现、验证算法

曾经“等你生日那天”都遥远得像未来,如今却可欢愉的挥手说“下个十年见” 9万+

五步通关!宝塔Linux部署ASP.NET Core的魔法秘籍大公开!

摘要: 本文提供ASP.NET Core应用五步部署攻略,涵盖从环境配置到线上运行的完整流程: 1️⃣ 环境准备:安装宝塔面板、ASP.NET Core运行时及开放端口; 2️⃣ 应用发布:通过dotnet publish生成独立部署包并上传服务器; 3️⃣ Nginx配置:设置反向代理将域名请求转发至应用端口; 4️⃣ 进程守护:使用Supervisor确保应用持续运行; 5️⃣ HTTPS加密:通过宝塔快速申请SSL证书并解决验证路径冲突。 关键点: 强调权限设置(755)、端口开放及反向代理细节; 提

java专栏 969

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,记录值...

weixin_34311757的博客 154

室内温度控制仿真(Simulink+PLC)

本篇博客将会带领大家解读Simulink自带的仿真模型(Thermal Model of a House),之后再讨论PLC控制系统控制环境温度的一些经验方法

RXXW_Dor的博客 3462

hdu3078 建层次树+在线LCA算法+排序

题意:n个点,n-1条边构成无向树,每个节点有权,Q次询问,每次或问从a->b的最短路中,权第k大的值,/或者更新节点a的权, 思路:在线LCA,先dfs生成树0,标记出层数和fa[](每个节点的父亲节点)。在对每次询问,走一遍一次公共祖先路上 的权,保持,快排。n*logn*q #include<iostream> //187MS #include&l...

weixin_33830216的博客 148

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

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 d...

weixin_30387339的博客 191

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

LCA在线(修改节点权值)hdu3078

Online Judge Online Exercise Online Teaching Online Contests Exercise Author F.A.Q Hand In Hand Online Acmers Forum | Discuss Statistical Charts Problem Archive Realtime Judge Statu

龙崎的专栏 1387

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

HDU3078(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 conn

weixin_43808717的博客 254

HDU3078 Network(tarjan离线处理+排序,树链第k大)

题意 给定一棵树,每个节点都有一个权值。 有m个操作: 0 a b表示将a节点的权值改为b k a b表示(k不为0)输出路径a–&gt;b上节点(包括a、b)的权值第k大。 解题 用tarjan算法离线求出所有查询的lca。然后通过前缀点pre来暴力统计路径上的点,将点放入数组s中,排序一次。比较数组大小sum与k的关系,如果k&gt;sum,无解;否则输出第k大(注意不是第k小...

EnjoyingAC的博客 403

HDU 3078 Network

大意略。 思路:通过LCA/RMQ可以找到LCA(u,v),通过记录预处理前驱fa的值,可以把u->v之间的所有顶点存下来,然后排一次序找到第K大值即可,如果size 给出LCA/RMQ代码。 void DFS(int u, int dep) { vis[u] = 1; dfn[top] = dep; euler[top] = u; pos[u] = top; ++t

Wall_F的专栏 1653

Network HDU - 3078 (LCA)题解

Network HDU - 3078 (LCA)题解

Sega_hsj 's Blog 549

HDU 3078 Network LCA + 排序

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3078 题意:给定一棵树,树上每个点都有权值,有两种操作: k a b,k == 0时把a点的权值改为b,k > 0时求a到b路径上第k大的权值 思路:k == 0时直接改权值即可,求k大值时求出a到b的LCA,然后分别从a和b出发走到LCA,记录下路径上的权值,排序输出第k大即可 总结:数据很菜,这

弱智就要多努力 772

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 16001

静静地码 1336

hdu3078

求路径中权重最小的点 #include #include #include #include #include using namespace std; typedef long long ll; #define N 80010 int vis[N],mark[N],first[N],ver[N*2]; int val[N]; int dis[N]; int deep[N*2]; i

wuhulala的休息室 1316

HDU3078

//LCA+排序 //用的是倍增的在线算法。。。。 #include #include #include #include #include using namespace std; const int maxn=80000+10; int val[maxn]; int temp[maxn]; struct node { int from; int to; int

woshizhazha的专栏 840

|hdu 3078|LCA|Network

hdu 3078LCA,先深度深的提上来和浅的同一深度,然后继续一起提,直到相等,期间把这些路径上的点都记录下来,排序后选择即可#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #define ms(i, j) memset(i, j, sizeof i) using namespace std;const

博客停更,请到"再见,优快云"文章中找新博客地址 545

POJ 3078

Q Time Limit:1000MS Memory Limit:65536K Total Submissions:1832 Accepted:1259 Description You've got a queue. And you just got to mess with it. Given a queue of items and a s...

一只菜鸟夹 397
上一篇: HDU 4547 - CD操作 (LCA)
下一篇: HDU 4409 - Family Name List (LCA)
Mr_Treeeee
博客等级 码龄10年 38粉丝 551原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值