HDU2586 How far away ?(LCA求最近公共祖先)

How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19650    Accepted Submission(s): 7704


Problem Description
There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can't visit a place twice) between every two houses. Yout task is to answer all these curious people.
 

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
 
 
23 21 2 103 1 151 22 32 21 2 1001 22 1
 

Sample Output
 
 
1025100100
 
用dis[i]表示节点i到根节点的距离,则a,b两点间距离等于dis[a]+dis[b]-2*dis[lca(a,b)];
LCA算法:
 常见的求最近公共祖先的算法是倍增算法。 
 首先对于每个结点先进行 DFS 预处理出它的深度,再记录下它们往父 亲方向走 2^0,2^1,2^2,··· ,2^k 步所到达的结点。在这里 2^k 大于整棵树的最 大深度。 
 预处理完后,需要查询两个点 u 和 v 的 LCA 的时候,先将 u 和 v 中深 度较大的一个利用先前处理出的数组走到和另一个结点相同的深度,这的所 需要的操作次数不会超过 log2|depth(u)−depth(v)|。
 接下来从 k 开始往下枚举,如果 u 和 v 如果往上走 2^i 后不相同,那么 就将它们一起往上走这么多步。 
 结束后如果 u 和 v 仍然不相等,再往上走一步。最后的顶点就是它们的 LCA。
#include <bits/stdc++.h>
using namespace std;
const int N = 40000 + 10;
struct Node{
    int to,w;
};
int n,m;
int f[N],deep[N],dis[N],p[N][20];
//p[i][j]记录i节点往上走2^j步到达的节点,f[i]记录i的父节点,
//deep[i]记录i的深度,dis[i]记录i到根节点的距离
vector <Node> node[N];

void dfs(int u,int pre){//首先对于每个结点先进行 DFS 预处理出它的深度
    for (int i = 0;i < node[u].size();i++){
        int v = node[u][i].to;
        if (v != pre){
            dis[v] = dis[u] + node[u][i].w;
            deep[v] = deep[u] + 1;
            f[v] = u;
            dfs(v,u);
        }
    }
}

void init(){//记录下它们往父 亲方向走 2^0,2^1,2^2,··· ,2^k 步所到达的结点
    for (int i = 1;i <= n;i++) p[i][0] = f[i];//向上走2^0到达父亲节点
    for (int j = 1;(1 << j) <= n;j++){
        for (int i = 1;i <= n;i++){
            if (p[i][j-1] != -1){
                p[i][j] = p[p[i][j-1]][j-1];//i走2^j到达的节点等于i走2^(j-1)到达的节点再走2^(j-1)
            }
        }
    }
}

int lca(int a,int b){
    if (deep[a] < deep[b]) swap(a,b);
    int k = 0;
    while ((1 << k) < deep[a]) k++;
    k--;
    for (int j = k;j >= 0;j--){//先将u和v中深度较大的一个利用先前处理出的数组走到和另一个结点相同的深度
        if (deep[a] - (1 << j) >= deep[b]){
            a = p[a][j];
        }
    }
    if (a == b) return a;
    for (int j = k;j >= 0;j--){//接下来从 k 开始往下枚举
        if (p[a][j] != p[b][j]){//如果 u 和 v 如果往上走 2^i 后不相同,那么 就将它们一起往上走这么多步
            a = p[a][j]; b=p[b][j];
        }
    }
    return f[a];//结束后如果 u 和 v 仍然不相等,再往上走一步。最后的顶点就是它们的 LCA。
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        memset(f,-1,sizeof(-1));
        memset(p,-1,sizeof(-1));
        for (int i = 0;i < n-1;i++){
            int a,b,w;
            scanf("%d%d%d",&a,&b,&w);
            node[a].push_back(Node{b,w});
            node[b].push_back(Node{a,w});
        }
        deep[1] = 0; dis[1] = 0; 
        dfs(1,-1);
        init();
        for (int i = 0;i < m;i++){
            int a,b;
            scanf("%d%d",&a,&b);
            printf("%d\n",dis[a]+dis[b]-2*dis[lca(a,b)]);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园的建设目标是通过数据整合、全面共享,实现校园内教学、科研、管理、服务流程的数字化、信息化、智能化和多媒体化,以提高资源利用率和管理效率,确保校园安全。 智慧校园的建设思路包括构建统一支撑平台、建立完善管理体系、大数据辅助决策和建设校园智慧环境。通过云架构的数据中心与智慧的学习、办公环境,实现日常教学活动、资源建设情况、学业水平情况的全面统计和分析,为决策提供辅助。此外,智慧校园还涵盖了多媒体教学、智慧录播、电子图书馆、VR教室等多种教学模式,以及校园网络、智慧班牌、校园广播等教务管理功能,旨在提升教学品质和管理水平。 智慧校园的详细方案设计进一步细化了教学、教务、安防和运维等多个方面的应用。例如,在智慧教学领域,通过多媒体教学、智慧录播、电子图书馆等技术,实现教学资源的共享和教学模式的创新。在智慧教务方面,校园网络、考场监控、智慧班牌等系统为校园管理提供了便捷和高效。智慧安防系统包括视频监控、一键报警、阳光厨房等,确保校园安全。智慧运维则通过综合管理平台、设备管理、能效管理和资产管理,实现校园设施的智能化管理。 智慧校园的优势和价值体现在个性化互动的智慧教学、协同高效的校园管理、无处不在的校园学习、全面感知的校园环境和轻松便捷的校园生活等方面。通过智慧校园的建设,可以促进教育资源的均衡化,提高教育质量和管理效率,同时保障校园安全和提升师生的学习体验。 总之,智慧校园解决方案通过整合现代信息技术,如云计算、大数据、物联网和人工智能,为教育行业带来了革命性的变革。它不仅提高了教育的质量和效率,还为师生创造了一个更加安全、便捷和富有智慧的学习与生活环境。
好的,关于 HDU4992 所有原根的问题,我可以给出以下解答: 1. 首先,我们需要知道什么是原根。原根是指模 n 意义下,所有与 n 互质的数 a 的最小正整数 k 次幂对 n 取模的值覆盖了所有与 n 互质的数。简单来说,就是如果一个数 a 是模 n 意义下的原根,那么 a 的任何正整数次幂对 n 取模的值都不相同,且能覆盖所有与 n 互质的数。 2. 为了模 n 意义下的所有原根,我们需要先出与 n 互质的数的个数 phi(n)。phi(n) 可以使用欧拉函数出。 3. 然后,我们需要枚举模 n 意义下的所有数,判断它是否是原根。具体来说,对于每个 a,我们需要判断 a 的每个小于 phi(n) 的正整数次幂对 n 取模的值是否都不相同,且能覆盖所有与 n 互质的数。如果是,那么 a 就是模 n 意义下的原根。 4. 代码实现可以参考以下 Java 代码: ``` import java.util.*; public class Main { static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static int phi(int n) { int res = n; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { res = res / i * (i - 1); while (n % i == 0) { n /= i; } } } if (n > 1) { res = res / n * (n - 1); } return res; } static int pow(int a, int b, int mod) { int res = 1; while (b > 0) { if ((b & 1) != 0) { res = res * a % mod; } a = a * a % mod; b >>= 1; } return res; } static boolean check(int a, int n, int phi) { for (int i = 1, j = pow(a, i, n); i <= phi; i++, j = j * a % n) { if (j == 1) { return false; } } return true; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); int phi = phi(n); List<Integer> ans = new ArrayList<>(); for (int i = 1; i < n; i++) { if (gcd(i, n) == 1 && check(i, n, phi)) { ans.add(i); } } Collections.sort(ans); for (int x : ans) { System.out.print(x + " "); } System.out.println(); } } } ``` 其中,gcd 函数用于最大公约数,phi 函数用于欧拉函数,pow 函数用于快速幂模,check 函数用于判断一个数是否是原根。在主函数中,我们依次读入每个 n,出 phi(n),然后枚举模 n 意义下的所有数,判断它是否是原根,将所有原根存入一个 List 中,最后排序输出即可。 希望我的回答能够帮到你,如果你有任何问题,欢迎随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值