2370 小机房的树

 时间限制: 1 s
 空间限制: 256000 KB
 题目等级 : 钻石 Diamond
 查看运行结果
 
 
题目描述 Description

小机房有棵焕狗种的树,树上有N个节点,节点标号为0到N-1,有两只虫子名叫飘狗和大吉狗,分居在两个不同的节点上。有一天,他们想爬到一个节点上去搞基,但是作为两只虫子,他们不想花费太多精力。已知从某个节点爬到其父亲节点要花费 c 的能量(从父亲节点爬到此节点也相同),他们想找出一条花费精力最短的路,以使得搞基的时候精力旺盛,他们找到你要你设计一个程序来找到这条路,要求你告诉他们最少需要花费多少精力

输入描述 Input Description
第一行一个n,接下来n-1行每一行有三个整数u,v, c 。表示节点 u 爬到节点 v 需要花费 c 的精力。
第n+1行有一个整数m表示有m次询问。接下来m行每一行有两个整数 u ,v 表示两只虫子所在的节点
输出描述 Output Description

一共有m行,每一行一个整数,表示对于该次询问所得出的最短距离。

样例输入 Sample Input

3

1 0 1

2 0 1

3

1 0

2 0

1 2

样例输出 Sample Output

1

1

2

数据范围及提示 Data Size & Hint

1<=n<=50000, 1<=m<=75000, 0<=c<=1000

思路:

  裸lca强行ac(极端数据会超时,但是我还是ac了)

来,上代码:

#include<cstdio>
#include<algorithm> using namespace std; struct node {
int to,dis,next;
};
struct node edge[*+]; int n,m,head[],num=,dfn[],flag=; char ch; inline void read_int(int &x)
{
x=;ch=getchar();
while(ch>''||ch<'') ch=getchar();
while(ch>=''&&ch<=''){x=x*+(int)(ch-'');ch=getchar();}
} inline void edge_add(int from,int to,int dis)
{
num++;
edge[num].to=to;
edge[num].dis=dis;
edge[num].next=head[from];
head[from]=num;
} void dfs(int now)
{
dfn[now]=++flag;
for(int i=head[now];i;i=edge[i].next) if(!dfn[edge[i].to]) dfs(edge[i].to);
} int lca(int now_1,int now_2)
{
int dis_1=,dis_2=;
while(dfn[now_2]>dfn[now_1])
{
for(int i=head[now_2];i;i=edge[i].next)
{
if(dfn[edge[i].to]<dfn[now_2])
{
now_2=edge[i].to;
dis_2+=edge[i].dis;
}
}
}
while(dfn[now_1]>dfn[now_2])
{
for(int i=head[now_1];i;i=edge[i].next)
{
if(dfn[edge[i].to]<dfn[now_1])
{
now_1=edge[i].to;
dis_1+=edge[i].dis;
}
}
}
return dis_1+dis_2;
} int main()
{
read_int(n);
int from,to,dis;
for(int i=;i<n;i++)
{
read_int(from),read_int(to),read_int(dis);
edge_add(from,to,dis);
edge_add(to,from,dis);
}
read_int(m);
dfs();
for(int i=;i<=m;i++)
{
read_int(from),read_int(to);
if(dfn[from]>dfn[to]) swap(from,to);
printf("%d\n",lca(from,to));
}
return ;
}

小机房的树 codevs 2370的更多相关文章

  1. CodeVs.2370 小机房的树 ( LCA 倍增 最近公共祖先)

    CodeVs.2370 小机房的树 ( LCA 倍增 最近公共祖先) 题意分析 小机房有棵焕狗种的树,树上有N个节点,节点标号为0到N-1,有两只虫子名叫飘狗和大吉狗,分居在两个不同的节点上.有一天, ...

  2. LCA(倍增在线算法) codevs 2370 小机房的树

    codevs 2370 小机房的树 时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题目描述 Description 小机房有棵焕狗种的树,树上有N个节点, ...

  3. Codevs 2370 小机房的树

    2370 小机房的树 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为 ...

  4. codevs——2370 小机房的树

    2370 小机房的树  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 小机房有棵焕狗种的树,树上有N个 ...

  5. 【codevs2370】小机房的树 LCA 倍增

    2370 小机房的树  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为0 ...

  6. codevs2370 小机房的树 x

    2370 小机房的树  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond   题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号 ...

  7. Codevs 2370 小机房的树 LCA 树上倍增

    题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为0到N-1,有两只虫子名叫飘狗和大吉狗,分居在两个不同的节点上.有一天,他们想爬到一个节点上去搞基,但是作为两只虫子, ...

  8. 小机房的树(codevs 2370)

    题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为0到N-1,有两只虫子名叫飘狗和大吉狗,分居在两个不同的节点上.有一天,他们想爬到一个节点上去搞基,但是作为两只虫子, ...

  9. 放一道比较基础的LCA 的题目把 :CODEVS 2370 小机房的树

    题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为0到N-1,有两只虫子名叫飘狗和大吉狗,分居在两个不同的节点上.有一天,他们想爬到一个节点上去搞基,但是作为两只虫子, ...

随机推荐

  1. iOS:JSON格式字符串转字典,字典转JSON格式字符串

    在iOS开发中,和服务器交互中,经常用到字典和JSON格式字符串相互转换. 代码如下: 1.JSON格式字符串转字典 + (NSDictionary *)dictionaryWithJsonStrin ...

  2. ALV的颜色分为行的颜色、列的颜色和CELL的颜色

    ALV的颜色分为行的颜色.列的颜色和CELL的颜色.任务要求,将一定的Tabellenfeld 用黄色填充,也就是说CELL的颜色 DATA:ls_cellcolorTYPElvc_s_scol,co ...

  3. The specified file or folder name is too long

    You receive a "The specified file or folder name is too long" error message when you creat ...

  4. Android 调用已安装市场,进行软件评分的功能实现

    Uri uri = Uri.parse("market://details?id="+getPackageName()); Intent intent = new Intent(I ...

  5. Map集合概述

    java集合最后一站之Map,给自己的总结画个句号... Map用于保存具有映射关系的数据. 1.HashMap和Hashtable实现类 HashMap和Hashtable都是Map接口的典型实现类 ...

  6. Android Adapter的几个方法

    1  ListView是在什么时候设置对Adapter的数据监听的? 在setAdapter(ListAdapter adapter)中,会先取消ListView中原来的mAdapter中的数据监听( ...

  7. iOS中的UI

    • 不管你是学习android开发还是iOS开发• 都建议先学习UI,原因如下:UI是app的根基:⼀一个app应该是先有UI界⾯面,然后在UI的基础上增加实⽤用功能 UI相对简单易学:UI普遍是学 ...

  8. OC self = [super init] , 点语法 , @property

    OC self = [super init] , 点语法 , @property 构造方法为啥这么写? self = [super init]; [super init] 的结果可能有三种: 第一种: ...

  9. xdebug + wincachegrind

    ;;;;;;;php.ini;;;;;;;;;;;;;;;;;; [Xdebug]zend_extension=D:\Xampp\php\ext\php_xdebug.dll;开启自动跟踪xdebug ...

  10. Android进程管理及静态变量垃圾回收

    1.Android静态变量的生命周期 静态变量的生命周期遵守Java的设计.我们知道静态变量是在类被load的时候分配内存的,并且存在于方法 区.当类被卸载的时候,静态变量被销毁. 在PC机的客户端程 ...