日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

【SPOJ - QTREE2】Query on a tree II(LCA,倍增)

發布時間:2023/12/10 c/c++ 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【SPOJ - QTREE2】Query on a tree II(LCA,倍增) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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).?ttest 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?a,?b?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: 16 1 2 1 2 4 1 2 5 2 1 3 1 3 6 2 DIST 4 6 KTH 4 6 4 DONEOutput: 5 3

解題報告:

?

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<queue> #include<stack> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 20000 + 5; int dep[MAX],fa[MAX][33],cost[MAX][33];int n,m; vector<int> vv[MAX]; vector<int> ww[MAX]; void dfs(int cur, int rt) {fa[cur][0] = rt;dep[cur] = dep[rt] + 1;for(int i = 1; i < 31; ++i) {fa[cur][i] = fa[fa[cur][i - 1]][i - 1];cost[cur][i] = cost[fa[cur][i - 1]][i - 1] + cost[cur][i - 1];}int sz = vv[cur].size();for (int i = 0; i < sz; ++i) {if (vv[cur][i] == rt) continue;cost[vv[cur][i]][0] = ww[cur][i];dfs(vv[cur][i], cur);} } int lca(int u,int v) {if(dep[u] < dep[v]) swap(u,v);int res = 0;int dc = dep[u]-dep[v];for(int i = 0; i<=30; i++) {if((1<<i) & dc) res += cost[u][i],u=fa[u][i];}if(u == v) return res;for(int i = 30; i>=0 && u!=v; i--) {if(fa[u][i] != fa[v][i]) {res += cost[v][i] + cost[u][i];u=fa[u][i];v=fa[v][i];}}res += cost[u][0] + cost[v][0];u=fa[u][0];return res; } int fk(int a,int b,int k) {int u=a,v=b;if(dep[u] < dep[v]) swap(u,v);int dc = dep[u]-dep[v];for(int i = 0; i<=30; i++) {if((1<<i) & dc) u=fa[u][i];}if(u!=v) {for(int i = 30; i>=0 && u!=v; i--) {if(fa[u][i] != fa[v][i]) {u=fa[u][i];v=fa[v][i];}}u=fa[u][0];//得到公共祖先}int cur = 0;int ans = 0;if(dep[a] - dep[u] >= k) {int RES = k-1;if(RES!=0) {for(int i = 30; i>=0; i--) {int now = fa[a][i];if(dep[a] - dep[now] < RES) {int tmp = dep[a] - dep[now];a = fa[a][i];RES -= tmp;}}ans = fa[a][0];} else ans = a; } else if(dep[a] - dep[u] == k-1) {ans = u;} else {int RES = k - (dep[a] - dep[u])-1;RES = (dep[b]-dep[u]-RES);if(RES != 0) {for(int i = 30; i>=0; i--) {int now = fa[b][i];if(dep[b] - dep[now] < RES) {int tmp = dep[b] - dep[now];b = fa[b][i];RES -= tmp;}}ans = fa[b][0];}else ans = b;}return ans; } int main() {int t;cin>>t;while(t--) {scanf("%d",&n);memset(dep,0,sizeof dep);memset(fa,0,sizeof fa);memset(cost,0,sizeof cost);for(int i = 1; i<=n; i++) vv[i].clear(),ww[i].clear();for(int a,b,c,i = 1; i<=n-1; i++) {scanf("%d%d%d",&a,&b,&c);vv[a].pb(b);vv[b].pb(a);ww[a].pb(c);ww[b].pb(c);}dfs(1,-1);char op[22];while(scanf("%s",op)) {int a,b,k;if(!strcmp(op,"DIST")) {scanf("%d%d",&a,&b);printf("%d\n",lca(a,b));} else if(!strcmp(op,"KTH")) {scanf("%d%d%d",&a,&b,&k);printf("%d\n",fk(a,b,k));} else break;}if(t) puts("");}return 0 ; }

?

總結

以上是生活随笔為你收集整理的【SPOJ - QTREE2】Query on a tree II(LCA,倍增)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。