poj 1986 Distance Queries
好像是模板题 当作练习题 不错; 要求任意两点之间的距离。可以假设一个根节点,然后所有点到根节点的距离,然后求出任意两点多公共祖先; 距离就变成了
dis[u]+dis[v] - 2*dis[ lca(u,v) ] 非常好的题目
- #include<iostream>
- #include<stdio.h>
- #include<cstring>
- #include<algorithm>
- #include<cmath>
- using namespace std;
- struct date{
- int v,val,next;
- }edge[];
- int N,M,total,head[],dis[];
- void add_edge( int u,int v,int val ){
- edge[total].v = v;
- edge[total].val = val;
- edge[total].next = head[u];
- head[u] = total++;
- }
- bool vis[]; int num,sta[],tab[],dep[],dp[][];
- void LCA( int son,int deep,int w ){
- dep[num] = deep; tab[num] = son; sta[son] = num++; dis[son] = w; vis[son] = true;
- for( int i = head[son]; i != -; i = edge[i].next ){
- int v = edge[i].v; //cout<<edge[i].val<<endl;
- if( !vis[v] )
- { LCA( v,deep+,w+edge[i].val ); dep[num] = deep; tab[num] = son; num++; }
- }
- //dep[num] = deep; tab[num] = son; num++;
- }
- int work( int n1,int n2 ){
- if( dep[n1] < dep[n2] )return n1;
- return n2;
- }
- void RMQ( ){
- for( int i = ; i <= num; i++ )dp[i][] = i;
- for( int i = ; (<<i) <= num; i++ )
- for( int j = ; j - + (<<i) <= num; j++ )
- dp[j][i] = work( dp[j][i-],dp[j+(<<(i-))][i-] );
- }
- int query( int L,int R )
- {
- int k = ;
- while( (<<(k+)) <= (R-L+) )k++;
- return tab[work( dp[L][k],dp[R-(<<k)+][k] )];
- }
- int main( )
- {
- int u,v,w; char str[];
- while( scanf("%d%d",&N,&M) != EOF )
- {
- memset( head,-,sizeof(head) ); total = ;
- for( int i = ; i <= M; i++ )
- {
- scanf("%d%d%d%s",&u,&v,&w,&str);
- add_edge( u,v,w );
- add_edge( v,u,w );
- }
- memset( vis,,sizeof(vis) );
- num = ; LCA( ,, ); num--; RMQ( );
- //for( int i = 1; i <= num; i++ )cout<<i<<" "<<dep[i]<<" "<<tab[i]<<endl;
- int Q; scanf("%d",&Q);
- while( Q-- ){
- scanf("%d%d",&u,&v);
- //cout<<u<<" "<<v<<" "<<dis[u]<<" "<<dis[v]<<endl;
- if( sta[u] > sta[v] )swap( u,v );
- int t = query(sta[u],sta[v]);//cout<<endl<<" "<<t<<endl;
- //cout<<t<<" "<<dis[1]<<" "<<u<<" "<<v<<" "<<dis[u]<<" "<<dis[v]<<endl;
- cout<<dis[u]+dis[v]-*dis[t]<<endl;
- }
- }
- return ;
- }
poj 1986 Distance Queries的更多相关文章
- POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)
POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...
- POJ.1986 Distance Queries ( LCA 倍增 )
POJ.1986 Distance Queries ( LCA 倍增 ) 题意分析 给出一个N个点,M条边的信息(u,v,w),表示树上u-v有一条边,边权为w,接下来有k个询问,每个询问为(a,b) ...
- POJ 1986 Distance Queries LCA两点距离树
标题来源:POJ 1986 Distance Queries 意甲冠军:给你一棵树 q第二次查询 每次你问两个点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + d ...
- POJ 1986 Distance Queries 【输入YY && LCA(Tarjan离线)】
任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total ...
- POJ 1986 Distance Queries(Tarjan离线法求LCA)
Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 12846 Accepted: 4552 ...
- poj 1986 Distance Queries LCA
题目链接:http://poj.org/problem?id=1986 Farmer John's cows refused to run in his marathon since he chose ...
- POJ 1986 - Distance Queries - [LCA模板题][Tarjan-LCA算法]
题目链接:http://poj.org/problem?id=1986 Description Farmer John's cows refused to run in his marathon si ...
- poj 1986 Distance Queries(LCA)
Description Farmer John's cows refused to run in his marathon since he chose a path much too long fo ...
- POJ 1986 Distance Queries(LCA Tarjan法)
Distance Queries [题目链接]Distance Queries [题目类型]LCA Tarjan法 &题意: 输入n和m,表示n个点m条边,下面m行是边的信息,两端点和权,后面 ...
- poj 1986 Distance Queries 带权lca 模版题
Distance Queries Description Farmer John's cows refused to run in his marathon since he chose a pa ...
随机推荐
- springMVC视频教程
http://edu.51cto.com/index.php?do=lession&id=42165
- SWD接口:探索&泄密&延伸
http://bbs.21ic.com/icview-871133-1-1.html 文买了个JLINKV9,以为神器,拿到手发现根本不是,完全没必要替换V8,想自己做个另类的调试器,当然想只是想而已 ...
- 如何用虚拟机装SQL Server
更新(2016.3.14) 看来之前还是写的太简了,来问的同学有点多. 所以还是详细一些吧. 大概步骤 装好Virtual Box这个软件,免费的 下载window server 2003系统镜像,然 ...
- HtmlHelper
HtmlHelper类 public class HtmlHelper<TModel> : HtmlHelper类 因为HtmlHelper<TModel>继承自HtmlHel ...
- 一个n位的数,去掉其中的k位,问怎样去使得留下来的(n-k)位数按原来的前后顺序组成的数最小
例如 8314925去掉4个数,留下125最小,注意有前后顺序要求,要是没有顺序当然是123. 解决方案 贪心算法,在每次被访问的位置保证有最优解. 思路一 分析:求一共n位,求其中的m位组成的数最小 ...
- Oracle在linux下的开机自启动(详细)转
linux下系统开机oracle自启动(方法一) ---加载为服务自启动.停止一.dbstart 及 dbshut 1. 修改Oracle系统配置文件:/etc/oratab,只有这样,Ora ...
- Data Base MongoDB 插入时间不正确的问题
关于mongodb插入时间不正确的问题 mongodb插入时间: 把本地时间转换为utc时间: 也就是比本地时间少8个小时: 读取的时候又会转换本地时间: 所有一般不需处理:
- MYSQL语句中SELECT语句及其子句的执行顺序
SELECT语句的执行的逻辑查询处理步骤:(8)SELECT (9)DISTINCT(11)<TOP_specification> <select_list>(1)FROM & ...
- html5 canvas移动设备渲染测试
最近项目闲着没什么事,又想起了canvas, 针对移动端设备默认浏览器,做了点渲染方面效率的测试,手头设备不多(有一些低端机型和pc chrome做对比),现将测试数据分享给大家吧,本想和css3 a ...
- cygwin的rebaseall失败
rebaseall: only ash or dash processes are allowed during rebasing Exit all Cygwin processes and stop ...