HDU 2586 How far away ?(LCA模板 近期公共祖先啊)
阿新 • • 發佈:2017-05-21
sizeof rmq round pad show mod 部分 cas sam
Input First line is a single integer T(T<=10), indicating the number of test cases.
For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.
Output For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.
Sample Input
Sample Output
Source ECJTU 2009 Spring Contest
題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2586
Input First line is a single integer T(T<=10), indicating the number of test cases.
For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.
Output For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.
Sample Input
2 3 2 1 2 10 3 1 15 1 2 2 3 2 2 1 2 100 1 2 2 1
Sample Output
10 25 100 100
Source ECJTU 2009 Spring Contest
題意:
一個村莊有 n 個房子和 n-1 條雙向路,每兩個房子之間都有一條簡單路徑。
如今有m次詢問。求兩房子之間的距離。
PS:能夠用LCA來解,首先找到u, v 兩點的lca,然後計算一下距離值就能夠了。
計算方法是。記下根結點到隨意一點的距離dis[i],
這樣ans = dis[u] + dis[v] - 2 * dis[lca(v, v)]了。
這題要用c++交。G++會爆棧!
代碼例如以下:看別人的模板(tarjan 離線)
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 40047 #define maxm 247 struct node { int to,w,next; } edge[maxn*2]; int n, m; //點數,詢問次數 int head[maxn]; int k; int fa[maxn]; //父親結點 int dis[maxn]; //到根節點距離 int vis[maxn]; //是否訪問過 int s[maxm]; //詢問起點 int e[maxm]; //詢問終點 int lca[maxm]; //LCA(s,e) 近期公共祖先 int find(int x) { if(fa[x]!=x) return fa[x]=find(fa[x]); return fa[x]; } void init() { k = 1; memset(head,0,sizeof(head)); memset(dis,0,sizeof(dis)); memset(vis,0,sizeof(vis)); } void add(int u,int v,int w) { edge[k].to = v; edge[k].w = w; edge[k].next = head[u]; head[u] = k++; } void tarjan(int u) { int i,v; fa[u] = u; vis[u] = 1; for(i = 0; i < m; i++) { if(e[i]==u && vis[s[i]]) lca[i] = find(s[i]); //若詢問的兩點中有一點已被訪問過。則兩點的LCA則為這一點的當前父節點 if(s[i]==u && vis[e[i]]) lca[i] = find(e[i]); } for(i = head[u]; i; i = edge[i].next) { v = edge[i].to; if(!vis[v]) //若沒被訪問過 { dis[v] = dis[u]+edge[i].w;//更新距離 tarjan(v); fa[v] = u;//回溯更新父節點 } } } int main() { int t; scanf("%d",&t); while(t--) { init(); scanf("%d%d",&n,&m); int u, v, w; for(int i = 0; i < n-1; i++) { scanf("%d%d%d",&u,&v,&w); add(u,v,w); add(v,u,w); } for(int i = 0; i < m; i++) { scanf("%d%d",&s[i],&e[i]); } tarjan(1); for(int i = 0; i < m; i++) { printf("%d\n",dis[s[i]]+dis[e[i]]-2*dis[lca[i]]);//兩點距離為根節點到兩點距離之和-根節點到LCA距離*2 } } return 0; }
(ST在線算法 轉)
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; //#pragma comment(linker, "/STACK:102400000,102400000") //不須要申請系統棧 const int N = 40010; const int M = 25; int dp[2*N][M]; //這個數組記得開到2*N,由於遍歷後序列長度為2*n-1 bool vis[N]; struct edge { int u,v,w,next; } e[2*N]; int tot,head[N]; inline void add(int u ,int v ,int w ,int &k) { e[k].u = u; e[k].v = v; e[k].w = w; e[k].next = head[u]; head[u] = k++; u = u^v; v = u^v; u = u^v; e[k].u = u; e[k].v = v; e[k].w = w; e[k].next = head[u]; head[u] = k++; } int ver[2*N],R[2*N],first[N],dir[N]; //ver:節點編號 R:深度 first:點編號位置 dir:距離 void dfs(int u ,int dep) { vis[u] = true; ver[++tot] = u; first[u] = tot; R[tot] = dep; for(int k=head[u]; k!=-1; k=e[k].next) if( !vis[e[k].v] ) { int v = e[k].v , w = e[k].w; dir[v] = dir[u] + w; dfs(v,dep+1); ver[++tot] = u; R[tot] = dep; } } void ST(int n) { for(int i=1; i<=n; i++) dp[i][0] = i; for(int j=1; (1<<j)<=n; j++) { for(int i=1; i+(1<<j)-1<=n; i++) { int a = dp[i][j-1] , b = dp[i+(1<<(j-1))][j-1]; dp[i][j] = R[a]<R[b]?a:b; } } } //中間部分是交叉的。 int RMQ(int l,int r) { int k=0; while((1<<(k+1))<=r-l+1) k++; int a = dp[l][k], b = dp[r-(1<<k)+1][k]; //保存的是編號 return R[a]<R[b]?a:b; } int LCA(int u ,int v) { int x = first[u] , y = first[v]; if(x > y) swap(x,y); int res = RMQ(x,y); return ver[res]; } int main() { //freopen("Input.txt","r",stdin); //freopen("Out.txt","w",stdout); int cas; scanf("%d",&cas); while(cas--) { int n,q,num = 0; scanf("%d%d",&n,&q); memset(head,-1,sizeof(head)); memset(vis,false,sizeof(vis)); for(int i=1; i<n; i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); add(u,v,w,num); } tot = 0; dir[1] = 0; dfs(1,1); /*printf("節點ver "); for(int i=1; i<=2*n-1; i++) printf("%d ",ver[i]); cout << endl; printf("深度R "); for(int i=1; i<=2*n-1; i++) printf("%d ",R[i]); cout << endl; printf("首位first "); for(int i=1; i<=n; i++) printf("%d ",first[i]); cout << endl; printf("距離dir "); for(int i=1; i<=n; i++) printf("%d ",dir[i]); cout << endl;*/ ST(2*n-1); while(q--) { int u,v; scanf("%d%d",&u,&v); int lca = LCA(u,v); printf("%d\n",dir[u] + dir[v] - 2*dir[lca]); } } return 0; }
HDU 2586 How far away ?(LCA模板 近期公共祖先啊)