hdu 2545(并查集求节点到根节点的距离)
来源:互联网 发布:声学模拟软件 编辑:程序博客网 时间:2024/06/06 02:01
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2545
思路:dist[u]表示节点u到根节点的距离,然后在查找的时候更新即可,最后判断dist[u],dist[v]的大小即可。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 using namespace std; 7 #define MAXN 101000 8 9 int n,m;10 int parent[MAXN];11 int dist[MAXN];12 13 int Find(int x)14 {15 if(x==parent[x]){16 return parent[x];17 }18 int tmp=parent[x];19 parent[x]=Find(parent[x]);20 dist[x]+=dist[tmp];//更新到根节点的距离21 return parent[x];22 }23 24 int main()25 {26 int u,v;27 while(~scanf("%d%d",&n,&m)){28 if(n==0&&m==0)break;29 for(int i=1;i<=n;i++){30 parent[i]=i;31 dist[i]=0;32 }33 for(int i=1;i<n;i++){34 scanf("%d%d",&u,&v);35 parent[v]=u;36 dist[v]=1;37 }38 while(m--){39 scanf("%d%d",&u,&v);40 Find(u);41 Find(v);42 if(dist[u]<=dist[v]){43 puts("lxh");44 }else 45 puts("pfz");46 }47 }48 return 0;49 }
0 0
- hdu 2545(并查集求节点到根节点的距离)
- HDU:4707 Pet(并查集+某元素到根节点的距离计算)
- hdu 2196(求树上每个节点到树上其他节点的最远距离)
- 求树上每个节点到其他节点的最远距离
- HDU 2196 Computer(求树上每个节点到其它点的最远距离)
- HDU 2196 Computer(求树上每个节点到其它点的最远距离 树dp)
- 数据结构——距离根节点为k的节点
- BNU Corporative Network(并查集求点到根节点的距离)@
- 找出所有从根节点到叶子节点路径和等于n的路径并输出
- 找出所有从根节点到叶子节点路径和等于n的路径并输出
- leetcode:Minimum Depth of Binary Tree(树的根节点到叶子节点的最小距离)【面试算法题】
- (DS 《算法竞赛入门经典》)LA 3027 Corporative Network(查询某一个节点到根节点之间的距离)
- HDU 2196 树形DP + 节点到达的最长距离
- ZOJ 3789 Gears (并查集 合并+删除+离根节点的距离)
- 网易——求二叉树最大叶子节点到最小叶子节点的距离
- 搜狐研究院 求二叉树最大叶子节点到最小叶子节点的距离
- 求树中节点的最大距离
- 节点的网络拓扑距离
- vim 相关
- uva 11380(最大流+拆点)
- 乎下的早也下得冷,这种天气人们
- uva 10246(最短路变形)
- uva 10972(边双连通分量)
- hdu 2545(并查集求节点到根节点的距离)
- hdu 4751(dfs染色)
- loj 1251(2-sat + 输出一组可行解)
- loj 1407(2-sat + 枚举 + 输出一组可行解 )
- loj 1026( tarjan + 输出割边 )
- loj 1063(求割点个数)
- loj 1300( 边双联通 + 判奇圈 )
- loj 1308(点双连通分量应用)
- loj 1108(spfa判负环)