BZOJ1787_紧急集合_KEY
LCA,对于每一个(x,y,z),两两求LCA得最优解或求出LCA不同于其他两组的那个为最优解。
code:
/**************************************************************
Problem: 1787
User: yekehe
Language: C++
Result: Accepted
Time:5212 ms
Memory:51604 kb
****************************************************************/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int read()
{
char c;while(c=getchar(),c<''||c>'');
int x=c-'';while(c=getchar(),c>=''&&c<='')x=(x<<)+(x<<)+c-'';
return x;
}
const int MAXN=;
int N,M;
int head[MAXN],to[MAXN<<],nxt[MAXN<<],cnt;
void add(int x,int y)
{
to[cnt]=y;
nxt[cnt]=head[x];
head[x]=cnt;
cnt++;
}
int D[MAXN];
int f[][MAXN];
void dfs(int now,int deep,int fa)
{
D[now]=deep;
f[][now]=fa;
for(int i=head[now];i!=-;i=nxt[i])
if(!D[to[i]])
dfs(to[i],deep+,now);
return ;
}
int Me;
int get(int x,int y)
{
if(D[x]<D[y])swap(x,y);
for(int i=;i>=;i--)
if(D[f[i][x]]>=D[y])x=f[i][x];
if(x==y)return x;
for(int i=;i>=;i--)
if(f[i][x]!=f[i][y])
x=f[i][x],y=f[i][y];
return f[][x];
}
int solve(int x,int y)
{
return D[x]+D[y]-(D[get(x,y)]<<);
}
int main(){
memset(head,-,sizeof head);
memset(nxt,-,sizeof nxt);
N=read(),M=read();
register int i,j;
for(i=;i<N;i++){
int x=read(),y=read();
add(x,y),add(y,x);
}
dfs(,,);
for(i=;i<=;i++)
for(j=;j<=N;j++)
f[i][j]=f[i-][f[i-][j]];
for(i=;i<=M;i++){
int x=read(),y=read(),z=read();
int a=get(x,y),b=get(y,z),c=get(x,z);
int dist=;
if(a==b)dist=c;else
if(b==c)dist=a;else
dist=b;
printf("%d %d\n",dist,solve(x,dist)+solve(y,dist)+solve(z,dist));
}
return ;
}
BZOJ1787_紧急集合_KEY的更多相关文章
- bzoj1787 [Ahoi2008]Meet 紧急集合
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 2272 Solved: 1029 [Submi ...
- 【BZOJ-1787&1832】Meet紧急集合&聚会 倍增LCA
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 2259 Solved: 1023[Submit] ...
- bzoj 1787 [Ahoi2008]Meet 紧急集合(1832 [AHOI2008]聚会)
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1841 Solved: 857[Submit][ ...
- BZOJ 1787: [Ahoi2008]Meet 紧急集合( 树链剖分 )
这道题用 LCA 就可以水过去 , 但是我太弱了 QAQ 倍增写LCA总是写残...于是就写了树链剖分... 其实也不难写 , 线段树也不用用到 , 自己YY一下然后搞一搞就过了...速度还挺快的好像 ...
- 1787: [Ahoi2008]Meet 紧急集合
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1482 Solved: 652[Submit][ ...
- [AHOI2008]紧急集合 / 聚会
题目描述 欢乐岛上有个非常好玩的游戏,叫做“紧急集合”.在岛上分散有N个等待点,有N-1条道路连接着它们,每一条道路都连接某两个等待点,且通过这些道路可以走遍所有的等待点,通过道路从一个点到另一个点要 ...
- [AHOI2008] 紧急集合
Description 欢乐岛上有个非常好玩的游戏,叫做"紧急集合".在岛上分散有N个等待点,有N-1条道路连接着它们,每一条道路都连接某两个等待点,且通过这些道路可以走遍所有的等 ...
- P4281 [AHOI2008]紧急集合 / 聚会
P4281 [AHOI2008]紧急集合 / 聚会 lca 题意:求3个点的lca,以及3个点与lca的距离之和. 性质:设点q1,q2,q3 两点之间的lca t1=lca(q1,q2) t2=lc ...
- 【BZOJ1787】[Ahoi2008]Meet 紧急集合 LCA
[BZOJ1787][Ahoi2008]Meet 紧急集合 Description Input Output Sample Input 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 ...
随机推荐
- Oracle EBS 应收发票取值
SELECT ct.trx_number ,ctl.description ,fnd_flex_ext.get_segs('SQLGL' ,'GL#' ,gcc.chart_of_accounts_i ...
- 记录Ubuntu14.04 LTS版本中使用Docker的过程
sudo apt-get update sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ software-p ...
- 使用WebViewJavascriptBridge与UIWebView交互
使用WebViewJavascriptBridge与UIWebView交互 https://github.com/marcuswestin/WebViewJavascriptBridge 核心的地方: ...
- 使用CoreData [2]
使用CoreData [2] 此篇讲解CoreData处理关系型数据. 1. 先创建出Student于Teacher的实体. 2. 确定关系,并修改描述 3. 创建对象,并查看一下关系(Teacher ...
- Windows 7 添加快速启动栏
1.右击任务栏空白处,选择 “工具栏” ,单击 “新建工具栏” 2.输入 以下路径: %userprofile%\AppData\Roaming\Microsoft\Internet Explorer ...
- win8中 cmd直接以管理员权限运行
一.组合键 WIN+X 二.锁定到任务栏,方便以后快速启动 三.控制面板->控制中心 四.更改用户账户控制设置--调到最低 好了,点击任务栏的命令提示符
- zabbix日常监控NFS(新加一)
有时候主机使用NFS文件挂载的方式来存储.备份.共享文件:但有时会出现断开的现象. 1.客户机现状 [root@tianxia6 ~]# df -h Filesystem Size Used Avai ...
- zabbix的日常监控-分布式监控(十)
参考博文:http://blog.51cto.com/jinlong/2051966 zabbix proxy 可以代替 zabbix server 检索客户端的数据,然后把数据汇报给 zabbix ...
- [2018HN省队集训D5T2] party
[2018HN省队集训D5T2] party 题意 给定一棵 \(n\) 个点以 \(1\) 为根的有根树, 每个点有一个 \([1,m]\) 的权值. 有 \(q\) 个查询, 每次给定一个大小为 ...
- .split("\n") 和 .strip("我是诗人的感叹")
s10='''诗人 学者 作家 # 这里面是有换行 "\n" 的, 要想变成一行, 删除strip不行,要用 split分开,这样就能变成一个列表,里面是各个字 ...