SPOJ913 Query on a tree II

Time Limit: 433MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu

Description

You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. Each edge has an integer value assigned to it, representing its length.

We will ask you to perfrom some instructions of the following form:

  • DIST a b : ask for the distance between node a and node b
    or
  • KTH a b k : ask for the k-th node on the path from node a to node b

Example:
N = 6 
1 2 1 // edge connects node 1 and node 2 has cost 1 
2 4 1 
2 5 2 
1 3 1 
3 6 2 

Path from node 4 to node 6 is 4 -> 2 -> 1 -> 3 -> 6 
DIST 4 6 : answer is 5 (1 + 1 + 1 + 2 = 5) 
KTH 4 6 4 : answer is 3 (the 4-th node on the path from node 4 to node 6 is 3) 

Input

The first line of input contains an integer t, the number of test cases (t <= 25). t test cases follow.

For each test case:

  • In the first line there is an integer N (N <= 10000)
  • In the next N-1 lines, the i-th line describes the i-th edge: a line with three integers a b c denotes an edge between ab of cost c (c <= 100000)
  • The next lines contain instructions "DIST a b" or "KTH a b k"
  • The end of each test case is signified by the string "DONE".

There is one blank line between successive tests.

Output

For each "DIST" or "KTH" operation, write one integer representing its result.

Print one blank line after each test.

Example

Input:
1

6
1 2 1
2 4 1
2 5 2
1 3 1
3 6 2
DIST 4 6
KTH 4 6 4
DONE

Output:
5
3

Hint

Added by:Thanh-Vy Hua
Date:2006-08-27
Time limit:0.433s
Source limit:15000B
Memory limit:1536MB
Cluster:Cube (Intel G860)
Languages:All except: ERL JS NODEJS PERL 6 VB.net
Resource:Special thanks to Ivan Krasilnikov for his alternative solution

 

 

有两种操作,一是求两点间距离,二是求一点到另一点路径上的第k个点。

LCA妥妥的。

 

  1 /*by SilverN*/
  2 #include<algorithm>
  3 #include<iostream>
  4 #include<cstring>
  5 #include<cstdio>
  6 #include<cmath>
  7 #include<vector>
  8 using namespace std;
  9 const int mxn=10010;
 10 int read(){
 11     int x=0,f=1;char ch=getchar();
 12     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 13     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
 14     return x*f;
 15 }
 16 struct edge{
 17     int v,nxt,dis;
 18 }e[mxn<<1];
 19 int hd[mxn],mct=0;
 20 void add_edge(int u,int v,int d){
 21     e[++mct].v=v;e[mct].nxt=hd[u];e[mct].dis=d;hd[u]=mct;return;
 22 }
 23 int T,n;
 24 int fa[mxn][15];
 25 int dep[mxn];
 26 int dis[mxn];
 27 void init(){memset(hd,0,sizeof hd);memset(fa,0,sizeof fa);mct=0;}
 28 void DFS(int u,int f){
 29     dep[u]=dep[f]+1;
 30     for(int i=1;i<15;i++)fa[u][i]=fa[fa[u][i-1]][i-1];
 31     for(int i=hd[u];i;i=e[i].nxt){
 32         int v=e[i].v;
 33         if(v==f)continue;
 34         fa[v][0]=u;
 35         dis[v]=dis[u]+e[i].dis;
 36         DFS(v,u);
 37     }
 38     return;
 39 }
 40 int LCA(int x,int y){
 41     if(dep[x]<dep[y])swap(x,y);
 42     for(int i=14;i>=0;i--)
 43         if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
 44     if(x==y)return y;
 45     for(int i=14;i>=0;i--){
 46         if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
 47     }
 48     return fa[x][0];
 49 }
 50 inline int dist(int x,int y){//求距离 
 51     int tmp=LCA(x,y);
 52     return dis[x]+dis[y]-dis[tmp]*2;
 53 }
 54 inline int find(int x,int k){//上溯 
 55     for(int i=14;i>=0;i--){
 56         if(k&(1<<i))x=fa[x][i];
 57     }
 58     return x;
 59 }
 60 inline int solve(int x,int y,int k){//查询从x到y路径上第k个结点 
 61     int tmp=LCA(x,y);
 62     int mid=dep[x]-dep[tmp]+1;
 63     if(k==mid)return tmp;
 64     if(k>mid){
 65         int dd=dep[y]-dep[tmp]+1;
 66         mid=k-mid+1;
 67         k=dd-mid;
 68         return find(y,k);
 69     }
 70     else
 71         return find(x,k-1);
 72 }
 73 int main(){
 74     T=read();
 75     int i,j,x,y,d;
 76     while(T--){
 77         init();
 78         n=read();
 79         for(i=1;i<n;i++){
 80             x=read();y=read();d=read();
 81             add_edge(x,y,d);
 82             add_edge(y,x,d);
 83         }
 84         int rt=n/2+1;
 85         dis[rt]=0;
 86         DFS(rt,0);
 87         char op[10];
 88         while(scanf("%s",op) && (op[0]!='D' || op[1]!='O')){
 89             if(op[0]=='K'){
 90                 x=read();y=read();d=read();
 91                 printf("%d\n",solve(x,y,d));
 92             }
 93             if(op[0]=='D'){
 94                 x=read();y=read();
 95                 printf("%d\n",dist(x,y));
 96             }
 97         }
 98     }
 99     return 0;
100 }

 

转载于:https://www.cnblogs.com/SilverNebula/p/6098748.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
洛谷的SPOJ需要注册一个SPOJ账号并进行绑定才能进行交题。您可以按照以下步骤进行注册: 1. 打开洛谷网站(https://www.luogu.com.cn/)并登录您的洛谷账号。 2. 在网站顶部导航栏中找到“题库”选项,将鼠标悬停在上面,然后选择“SPOJ”。 3. 在SPOJ页面上,您会看到一个提示,要求您注册SPOJ账号并进行绑定。点击提示中的链接,将会跳转到SPOJ注册页面。 4. 在SPOJ注册页面上,按照要求填写您的用户名、密码和邮箱等信息,并完成注册。 5. 注册完成后,返回洛谷网站,再次进入SPOJ页面。您会看到一个输入框,要求您输入刚刚注册的SPOJ用户名。输入用户名后,点击“绑定”按钮即可完成绑定。 现在您已经成功注册并绑定了SPOJ账号,可以开始在洛谷的SPOJ题库上刷题了。祝您顺利完成编程练习!\[1\]\[2\] #### 引用[.reference_title] - *1* *3* [(洛谷入门系列,适合洛谷新用户)洛谷功能全解](https://blog.csdn.net/rrc12345/article/details/122500057)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [luogu p7492 序列](https://blog.csdn.net/zhu_yin233/article/details/122051384)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值