Count on the path

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Problem Description
bobo has a tree, whose vertices are conveniently labeled by 1,2,…,n.

Let f(a,b) be the minimum of vertices not on the path between vertices a and b.

There are q queries (ui,vi) for the value of f(ui,vi). Help bobo answer them.

 
Input
The input consists of several tests. For each tests:

The first line contains 2 integers n,q (4≤n≤106,1≤q≤106). Each of the following (n - 1) lines contain 2 integers ai,bi denoting an edge between vertices ai and bi (1≤ai,bi≤n). Each of the following q lines contains 2 integer u′i,v′i (1≤ui,vi≤n).

The queries are encrypted in the following manner.

u1=u′1,v1=v′1.
For i≥2, ui=u′i⊕f(ui - 1,vi - 1),vi=v′i⊕f(ui-1,vi-1).

Note ⊕ denotes bitwise exclusive-or.

It is guaranteed that f(a,b) is defined for all a,b.

The task contains huge inputs. `scanf` in g++ is considered too slow
to get accepted. You may (1) submit the solution in c++; or (2) use
hand-written input utilities.

 
Output
For each tests:

For each queries, a single number denotes the value.

 
Sample Input
4 1
1 2
1 3
1 4
2 3
5 2
1 2
1 3
2 4
2 5
1 2
7 6
 
Sample Output
4
3
1
分析:参考http://www.lai18.com/content/8107004.html;
   学到的东西很多,比如求子树的第二小,在当前根下却不在其中一个儿子的子树的最小值;
   bel[i]表示i在1的哪个儿子下,fa[i]表示i的父亲,son[i]表示i的所有子树里面最小的节点,sec_son[i]对所有i的儿子j的son[j]排序后取第二小的;
   dp[i]表示不是i,却是i的父亲的儿子j下的最小son[j],dp1[i]表示从1走到i所有分支子树下的最小节点;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
const int maxn=1e6+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,q,tot,h[maxn],bel[maxn],fa[maxn],son[maxn],sec_son[maxn],dp[maxn],dp1[maxn],ans;
struct node
{
int to,nxt;
}e[maxn<<];
void add(int x,int y)
{
tot++;
e[tot].to=y;
e[tot].nxt=h[x];
h[x]=tot;
}
void dfs(int now,int pre)
{
son[now]=sec_son[now]=inf;
for(int i=h[now];i;i=e[i].nxt)
{
int to=e[i].to;
if(to!=pre)
{
fa[to]=now;
if(now!=)bel[to]=bel[now];
dfs(to,now);
if(son[now]>min(son[to],to))
{
sec_son[now]=son[now];
son[now]=min(son[to],to);
}
}
}
}
void dfs1(int now,int pre)
{
if(now!=)dp1[now]=min(dp1[fa[now]],dp[now]);
else dp1[now]=inf;
for(int i=h[now];i;i=e[i].nxt)
{
int to=e[i].to;
if(to!=pre)
{
dfs1(to,now);
}
}
}
void solve(int x,int y)
{
int now_ans=inf;
for(int i=h[];i;i=e[i].nxt)
{
int to=e[i].to;
if(to!=bel[x]&&to!=bel[y])now_ans=min(min(now_ans,son[to]),to);
}
if(x!=)now_ans=min(now_ans,son[x]);
if(y!=)now_ans=min(now_ans,son[y]);
now_ans=min(now_ans,dp1[x]);
now_ans=min(now_ans,dp1[y]);
ans=now_ans;
}
int main()
{
int i,j;
while(~scanf("%d%d",&n,&q))
{
ans=;
tot=;
memset(h,,sizeof(h));
rep(i,,n)bel[i]=i;
rep(i,,n-)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
dfs(,);
rep(i,,n)
{
if(fa[i]==)
{
dp[i]=inf;
continue;
}
if(min(son[i],i)!=son[fa[i]])dp[i]=son[fa[i]];
else dp[i]=sec_son[fa[i]];
}
dfs1(,);
while(q--)
{
int a,b;
scanf("%d%d",&a,&b);
a^=ans,b^=ans;
if(bel[a]==bel[b])ans=;
else solve(a,b);
printf("%d\n",ans);
}
}
//system("Pause");
return ;
}

Count on the path的更多相关文章

  1. HDU4916 Count on the path(树dp??)

    这道题的题意其实有点略晦涩,定义f(a,b)为 minimum of vertices not on the path between vertices a and b. 其实它加一个minimum ...

  2. HDU 4916 Count on the path

    意甲冠军: 考虑到一棵树,m询价  不要求回答每一次询价u和v通过在两个节点形成的最低等级点路径 思路: 一開始以为是LCA-  只是T了好几次-  后来发现不用LCA也可做 考虑每一个询问u和v   ...

  3. hdu4916 Count on the path

    调了好久.... •把树视为以1为根的有向树,然后将1删除 •原树变为一个森林,并且任一棵树的根节点均为原树中1的子节点 •只需要考虑最小编号前3小的三棵树 •记f[x][y]为去掉x和y两棵树后的最 ...

  4. [LeetCode] Longest Univalue Path 最长相同值路径

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

  5. [Swift]LeetCode71. 简化路径 | Simplify Path

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  6. php 生成word的三种方式

    原文地址 http://www.jb51.net/article/97253.htm 最近工作遇到关于生成word的问题 现在总结一下生成word的三种方法. btw:好像只要是标题带PHP的貌似点击 ...

  7. WPF 自定义雷达图

    自定义雷达图表如下: Git下载地址:https://github.com/Kybs0/RadarChartControl 1.创建UserControl,名为“RadarChartControl” ...

  8. WPF Tookit Chart

      如何使用Chart 实例: Binding数据源中是一个KeyValuePair对象.可以是Dictionary. <charting:Chart x:Name="chtSumma ...

  9. Proj.4 API 中文参考

    ProjAPI https://github.com/OSGeo/proj.4/wiki/ProjAPI Tom Kralidis在2015年5月27日编辑此页·修订4 简介 执行pj_init()选 ...

随机推荐

  1. 触动精灵远程Log模块

    一.功能 lua log方法能够自动发现同一网段下面的log服务器 lua log方法能够主动将log发给服务器 lua 客户端进程重启服务端不存在影响 二.实现 服务器使用python编写: 启动一 ...

  2. NOIP2014-普及组复赛-第二题-比例简化

    题目描述 Description 在社交媒体上,经常会看到针对某一个观点同意与否的民意调查以及结果.例如,对某一观点表示支持的有1498 人,反对的有 902人,那么赞同与反对的比例可以简单的记为14 ...

  3. UPX3.03+UpolyX.5 Shell v1.0 汉化绿色版

    软件名称:UPX3.03+UpolyX.5 Shell v1.0 汉化绿色版软件类别:汉化软件运行环境:Windows软件语言:简体中文授权方式:免费版软件大小:635 KB软件等级:整理时间:201 ...

  4. ADO.NET 学生管理

    今天,我主要是对前面所做的学生管理系统加以完善. 通过今天的学习,我了解到了,在做程序时,一定要充分的为用户考虑.能用下拉列表让用户选择的,就不要让他们手动输入,能少输入的就少输入.在程序中,应尽可能 ...

  5. Oracle Day07 PL/SQL基础

    1.基本格式 set serveroutput on declare -- 申明部分 name ); begin -- 执行的sql语句 ; dbms_output.put_line(name); e ...

  6. oracle 备份操作流程

    Oracle 库表导出步骤 例如,要导出wcsr用户下的所有表,已知用户名/密码:wcsr/wcsr_woer 首先打开cmd.exe 其次创建备份目录,最好目录不包含空格和中文名 md d:\ora ...

  7. 用非GUI模式执行测试,jp@gc - PerfMon Metrics Collector会出现无法获取正确数据的解决办法

    用非GUI模式执行测试,jp@gc - PerfMon Metrics Collector会出现无法获取正确数据(实际显示的是Response Times Over Time),解决办法:在GUI模式 ...

  8. Android依赖注入:Google Guice on Android的使用及相关资源

    本文转自:http://blog.csdn.net/sangming/article/details/8878104 RoboGuice 使用谷歌自己的Guice库,给Android带来了简单和易用的 ...

  9. VC MFC工具栏(CToolBar)控件

    一.工具栏 工具栏控件在控件面板里没有对应的选项(图标),但有一个工具栏控件类CToolBar,所以我们如果要创建一个工具栏控件并显示在窗口里的话,只能用代码来完成,事实上任何一种控件,都可以用代码创 ...

  10. OpenCV成长之路:图像直方图的应用

    OpenCV成长之路:图像直方图的应用 2014-04-11 13:57:03 标签:opencv 图像 直方图 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否 ...