Count on the path
Count on the path
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
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.
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.
For each queries, a single number denotes the value.
1 2
1 3
1 4
2 3
5 2
1 2
1 3
2 4
2 5
1 2
7 6
3
1
#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的更多相关文章
- HDU4916 Count on the path(树dp??)
这道题的题意其实有点略晦涩,定义f(a,b)为 minimum of vertices not on the path between vertices a and b. 其实它加一个minimum ...
- HDU 4916 Count on the path
意甲冠军: 考虑到一棵树,m询价 不要求回答每一次询价u和v通过在两个节点形成的最低等级点路径 思路: 一開始以为是LCA- 只是T了好几次- 后来发现不用LCA也可做 考虑每一个询问u和v ...
- hdu4916 Count on the path
调了好久.... •把树视为以1为根的有向树,然后将1删除 •原树变为一个森林,并且任一棵树的根节点均为原树中1的子节点 •只需要考虑最小编号前3小的三棵树 •记f[x][y]为去掉x和y两棵树后的最 ...
- [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 ...
- [Swift]LeetCode71. 简化路径 | Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- php 生成word的三种方式
原文地址 http://www.jb51.net/article/97253.htm 最近工作遇到关于生成word的问题 现在总结一下生成word的三种方法. btw:好像只要是标题带PHP的貌似点击 ...
- WPF 自定义雷达图
自定义雷达图表如下: Git下载地址:https://github.com/Kybs0/RadarChartControl 1.创建UserControl,名为“RadarChartControl” ...
- WPF Tookit Chart
如何使用Chart 实例: Binding数据源中是一个KeyValuePair对象.可以是Dictionary. <charting:Chart x:Name="chtSumma ...
- Proj.4 API 中文参考
ProjAPI https://github.com/OSGeo/proj.4/wiki/ProjAPI Tom Kralidis在2015年5月27日编辑此页·修订4 简介 执行pj_init()选 ...
随机推荐
- 转-CSS优先级(权重)解析
1.多个选择器可能会选择同一个元素,有3个规则,从上到下重要性降低: !important的用户样式 !important的作者样式 作者样式 用户样式 浏览器定义的样式 2. CSS规范为不同类型的 ...
- ckeditor上传图片的注意点
1.要在 ckeditor的 config.js 文件中加上 CKEDITOR.editorConfig = function( config ) { config.filebrowserImage ...
- 后台验证url是不是有效的链接
/** * 判断链接是否有效 * 输入链接 * 返回true或者false */ public static boolean isValid(String strLink){ URL url=null ...
- 老oj1965:polygon半平面交
题目链接:http://192.168.2.240:8080/JudgeOnline/showproblem?problem_id=1965 polygon半平面交 Time Limit:1000MS ...
- hdu_2825_Wireless Password(AC自动机+状压DP)
题目链接:hdu_2825_Wireless Password 题意: 给你m个串,问长度为n至少含k个串的字符串有多少个 题解: 设dp[i][j][k]表示考虑到长度为i,第j个自动机的节点,含有 ...
- CABasicAnimation 几种停止的回调
一.编写一个简单的动画,使一个UIview从屏幕的左上角移动到左下角,间隔时间3S // // ViewController.m // CAAnimationTest // // Created by ...
- 对一个表中所有列数据模糊查询adoquery
如何用adoquery对一个表中所有列进行模糊查询: procedure TForm3.Button4Click(Sender: TObject); var ASql,AKey: string; I: ...
- 获取 Windows 任务栏 Rect
获取当前Windows系统的任务栏尺寸 1: RECT rect; 2: HWND hwndTaskbar = FindWindow(TEXT("Shell_TrayWnd"), ...
- java.util.Iterator
public interface Iterator<E>: 对 collection 进行迭代的迭代器. 方法摘要: boolean hasNext() 如果仍有元素可以迭代,则返回 tr ...
- 比较全的 C# 操作 Word的代码
using System;using System.Collections.Generic;using System.Text;using Microsoft.Office.Interop.Word; ...