链接:http://wikioi.com/problem/1036/

题意不写了。

思路:很明显找到lca然后用两个点的深度相加-lca的深度就是这一步的最近步数。

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#include <queue>
#define loop(s,i,n) for(i = s;i < n;i++)
#define cl(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn = ;
int low[maxn],dfn[maxn],set[maxn],father[maxn],dfsclock,cut;
int depth[maxn]; vector<int >g[maxn];
int find(int x)
{
if(set[x] != x)
set[x] = find(set[x]); return set[x];
}
int merge(int x,int y)
{
x = find(x);
y = find(y);
if(y != x)
{
set[y] = x;
return ;
}
return ;
}
void tarjan(int u,int pre,int deep)
{
int v,i,j;
dfn[u] = low[u] = ++dfsclock;
depth[u] = deep;
loop(,i,g[u].size())
{
v = g[u][i];
if(!dfn[v])
{
tarjan(v,u,deep+);
father[v] = u;
low[u] = min(low[v],low[u]);
if(low[v] > dfn[u])
cut++;
else
merge(u,v);
}
else if(v != pre)
low[u] = min(low[u],dfn[v]);
}
}
int lca(int u,int v)
{ while(u != v)
{
while(dfn[u] >= dfn[v] && u != v)
{
if(merge(u,father[u]))
cut--;
u = father[u];
}
while(dfn[v] >= dfn[u] && u != v)
{
if(merge(v,father[v]))
cut--;
v = father[v];
}
}
return u;
}
int main()
{
int n,m;
int i,x,y;
scanf("%d",&n);
{
int u,v;
loop(,i,n+)
{
g[i].clear();
set[i] = i;
father[i] = ;
}
loop(,i,n)
{
scanf("%d %d",&u,&v);
g[u].push_back(v);
g[v].push_back(u); }
cl(dfn,);
cl(low,);
cut = dfsclock = ; int k;
scanf("%d",&k);
tarjan(,-,);
u = ;
int ans;
ans = ;
while(k--)
{
scanf("%d",&v);
int f;
f = lca(u,v);
ans += depth[u]+depth[v]-*depth[f];
u = v;
}
cout<<ans<<endl;
}
return ;
}

wikioi1036 商务旅行 挺水的lca的更多相关文章

  1. codevs 1036 商务旅行(Targin求LCA)

    传送门 Description 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从首都出发,其他各城镇之间都有道路连接,任意 ...

  2. 倍增法-lca codevs 1036 商务旅行

    codevs 1036 商务旅行  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 某首都城市的商人要经常到各城镇去做生意 ...

  3. 【codevs1036】商务旅行 LCA 倍增

    1036 商务旅行  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 某首都城市的商人要经常到各城镇去做生意,他们按自己的 ...

  4. CodeVs.1036 商务旅行 ( LCA 最近公共祖先 )

    CodeVs.1036 商务旅行 ( LCA 最近公共祖先 ) 题意分析 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从 ...

  5. codevs1036商务旅行(LCA)

    1036 商务旅行  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 某首都城市的商人要经常到各城镇去做 ...

  6. C++之路进阶——codevs1036(商务旅行)

    1036 商务旅行 题目描述 Description 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从首都出发,其他各城镇 ...

  7. 2953: [Poi2002]商务旅行

    2953: [Poi2002]商务旅行 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 8  Solved: 8[Submit][Status] Desc ...

  8. poj3728 商务旅行

    [Description]小 T 要经常进行商务旅行,他所在的国家有 N 个城镇,标号为 1,2,3,...,N,这 N 个城镇构成一棵树.每个城镇可以买入和卖出货物,同一城镇买入和卖出的价格一样,小 ...

  9. codevs——1036 商务旅行

    1036 商务旅行  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 某首都城市的商人要经常 ...

随机推荐

  1. 删除 oracle

    C:\app\Administrator\product\11.2.0\client_1\deinstall 用这个批处理文件,会把oracle全部删除,除这个目录本身以外 .另外它不删除服务,即使服 ...

  2. pandas的DataFrame用法

    用来生成DataFrame数据 1.说明: class pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=F ...

  3. flask sqlaichemy中filter和filter_by

    简单总结一下: 查询的三种方式: 要实现组合查询,要么连续调用filter:q = sess.query(IS).filter(IS.node == node).filter(IS.password ...

  4. IIS7.5站点配置

    1. 创建站点映射目录: c:\website\release 2. 创建系统用户: a.本地用户和组-->用户-->新用户-->用户名\密码:WebUSR.xxxyyy--> ...

  5. XShell已经内置rz 直接从Windows拖文件进去终端

    XShell已经内置rz 直接从Windows拖文件进去终端 http://www.jb51.net/LINUXjishu/163820.html 借助securtCRT,使用linux命令sz可以很 ...

  6. IETF国标查询

    IETF官网 https://www.ietf.org/ rfc国标官网 https://www.ietf.org/standards/rfcs/ rfc国标查询 https://www.rfc-ed ...

  7. http webservice socket的区别

    1 数据传输方式1.1 socket传输的定义和其特点    所谓socket通常也称作"套接字",实现服务器和客户端之间的物理连接,并进行数据传输,主要有udp和tcp两个协议. ...

  8. Python统计字符串中的中英文字符、数字空格,特殊字符

    # -*- coding:utf8 -*- import string from collections import namedtuple def str_count(s): '''找出字符串中的中 ...

  9. django-生成随机验证码

    Python生成随机验证码,需要使用PIL模块. 安装: pip3 install pillow 1   1 pip3 install pillow 基本使用 1.创建图片 from PIL impo ...

  10. java操作Word总结

    import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Varia ...