Description

Mirada生活的城市中有N个小镇,一开始小镇之间没有任何道路连接。随着经济发展,小镇之间陆续建起了一些双向的道路,但是由于经济不太发达,在建设过程中,会保证对于任意两个小镇,最多有一条路径能互相到达。有的时候Miranda会从某个小镇开始进行徒步旅行,每次出发前,她都想选择一个她能到达的最远的小镇作为终点,并且她在行走过程中是不会走回头路的,为了估算这次旅行的时间,她会需要你告诉她这次旅行需要的时间会是多少呢?可以假设通过每条道路都需要单位时间,并且Miranda不会在小镇停留。
 

Input

第一行输入一个整数type,表示数据类型。
第二行两个整数N,Q。
接下来Q行,每行先读入一个整数ty,若ty=1,则接下来读入两个整数u,v,表示小镇u与小镇v建立了一条新道路。若ty=2,读入一个整数u,表示Miranda要开始一次从小镇u出发的旅行。
注意:
若type=1,表示数据进行了加密。记lstans表示最近一次Miranda旅行的时间,那么对于每次操作的u或u,v,都要异或上lstans,即u=u ^ lstans,v=v ^ lstans。
若type=0,则不需要对数据进行处理。

Output

对于每次询问,输出Miranda能到达的最远的小镇的距离是多少。注意Miranda可能只能留在开始的小镇。

 

Sample Input

0
5 10
1 4 5
2 3
2 5
2 1
1 5 3
1 1 4
2 3
2 5
1 5 2
2 1

Sample Output

0
1
0
3
2
3
 

Data Constraint

对于20%的数据,满足:N<=5000,Q<=10000
对于50%的数据,满足:N<=100000,Q<=200000
另外有20%的数据,满足:type=0
对于100%的数据,满足:N<=300000,Q<=500000,type=0或1,解密后的u,v满足1<=u,v<=N,且道路的修建会满足:每一个时刻,都不存在u,v,使得u,v之间能通过多种方式到达

Solution

操作有加边和维护联通块的最远端点,可以用LCT做

抄标程的,根本不会写啊

复杂度为O((N+Q) log N)

#include <stdio.h>
#include <string.h>
#define rg register
#define dmax(a, b) ((a) > (b) ? (a) : (b)) inline int rd()
{
rg int x = 0, c = getchar(), f = 1;
for(; c < 48 || c > 57; c = getchar()) if(c == 45) f = -1;
for(; c > 47 && c < 58; c = getchar()) x = (x << 1) + (x << 3) + c - 48;
return x * f;
} inline void dswap(rg int &x, rg int &y)
{
x ^= y ^= x ^= y;
} const int N = 500010; int on_line, n, Q, bel[N], dia[N][2]; struct node
{
bool rev;
int sz, ch[2], fa;
}T[N]; int find(rg int x) {return bel[x] == x ? x : bel[x] = find(bel[x]);} inline void update(rg int u) {if(u) T[u].sz = T[T[u].ch[0]].sz + T[T[u].ch[1]].sz + 1;} inline bool is_root(rg int u) {rg int fa = T[u].fa; return !fa || !(T[fa].ch[0] == u || T[fa].ch[1] == u);} inline void setch(rg int u, rg int v, rg int c)
{
T[u].ch[c] = v;
if(v) T[v].fa = u;
} inline void mark(rg int u)
{
if(!u)return;
T[u].rev ^= 1;
dswap(T[u].ch[0], T[u].ch[1]);
} inline void down(rg int u)
{
if(!T[u].rev)return;
mark(T[u].ch[0]);
mark(T[u].ch[1]);
T[u].rev = 0;
} inline void rotate(rg int u)
{
int fa = T[u].fa, ft = T[fa].fa, c = T[fa].ch[1] == u;
T[u].fa = ft;
if(!is_root(fa)) setch(ft, u, T[ft].ch[1] == fa);
setch(fa, T[u].ch[!c], c);
setch(u, fa, !c);
update(fa);
} inline void splay(rg int u)
{
static int stack[N];
rg int top = 0, p = u;
for(; !is_root(p); p = T[p].fa) stack[++top] = p;
stack[++top] = p;
for(; top; top--) down(stack[top]);
for(; !is_root(u); rotate(u))
{
rg int fa = T[u].fa, ft = T[fa].fa;
if(is_root(fa)) continue;
((T[ft].ch[1] == fa) ^ (T[fa].ch[1] == u)) ? rotate(u) : rotate(fa);
}
update(u);
} inline int access(rg int u)
{
int nxt = 0;
for(; u; nxt = u, u = T[u].fa) splay(u), T[u].ch[1] = nxt, update(u);
return nxt;
} inline void make_root(rg int u) {mark(access(u));} inline void link(rg int u, rg int v)
{
make_root(v);
splay(v);
T[v].fa = u;
access(v);
} inline int get_dis(rg int u, rg int v)
{
make_root(u);
access(v);
splay(v);
return T[v].sz - 1;
} inline void merge(rg int a, rg int b)
{
rg int u = find(a), v = find(b);
bel[v] = u;
link(a, b);
rg int lu = dia[u][0], lv = dia[u][1], h = get_dis(dia[u][0], dia[u][1]);
rg int h1 = get_dis(dia[v][0], dia[v][1]);
if(h1 > h) lu = dia[v][0], lv = dia[v][1], h = h1;
for(rg int i = 0; i < 2; i++)
for(rg int j = 0; j < 2; j++)
{
h1 = get_dis(dia[u][i], dia[v][j]);
if(h1 > h) lu = dia[u][i], lv = dia[v][j], h = h1;
}
dia[u][0] = lu, dia[u][1] = lv;
} int main()
{
freopen("hike.in", "r", stdin), freopen("hike.out", "w", stdout);
on_line = rd(), n = rd(), Q = rd();
for(rg int i = 1; i <= n; i++) bel[i] = dia[i][0] = dia[i][1] = i;
rg int last_ans = 0;
while(Q--)
{
rg int ty = rd();
if(ty & 1)
{
rg int u = rd(), v = rd();
if(on_line) u ^= last_ans, v ^= last_ans;
merge(u, v);
}
else{
rg int u = rd();
if(on_line) u ^= last_ans;
rg int p = find(u);
printf("%d\n", last_ans = dmax(get_dis(u, dia[p][0]), get_dis(u, dia[p][1])));
}
}
fclose(stdin), fclose(stdout);
return 0;
}

  

NOI模拟赛(3.13)Hike (远行)的更多相关文章

  1. 小奇模拟赛9.13 by hzwer

    2015年9月13日NOIP模拟赛 by hzwer    (这是小奇=> 小奇挖矿(explo) [题目背景] 小奇要开采一些矿物,它驾驶着一台带有钻头(初始能力值w)的飞船,按既定路线依次飞 ...

  2. NOI模拟赛 Day1

    [考完试不想说话系列] 他们都会做呢QAQ 我毛线也不会呢QAQ 悲伤ING 考试问题: 1.感觉不是很清醒,有点困╯﹏╰ 2.为啥总不按照计划来!!! 3.脑洞在哪里 4.把模拟赛当作真正的比赛,紧 ...

  3. 6.28 NOI模拟赛 好题 状压dp 随机化

    算是一道比较新颖的题目 尽管好像是两年前的省选模拟赛题目.. 对于20%的分数 可以进行爆搜,对于另外20%的数据 因为k很小所以考虑上状压dp. 观察最后答案是一个连通块 从而可以发现这个连通块必然 ...

  4. NOI 模拟赛 #2

    得分非常惨惨,半个小时写的纯暴力 70 分竟然拿了 rank 1... 如果 OYJason 和 wxjor 在可能会被爆踩吧 嘤 T1 欧拉子图 给一个无向图,如果一个边集的导出子图是一个欧拉回路, ...

  5. 【2018.12.10】NOI模拟赛3

    题目 WZJ题解 大概就是全场就我写不过 $FFT$ 系列吧……自闭 T1 奶一口,下次再写不出这种 $NTT$ 裸题题目我就艹了自己 -_-||| 而且这跟我口胡的自创模拟题 $set1$ 的 $T ...

  6. NOI模拟赛Day5

    T1 有and,xor,or三种操作,每个人手中一个数,求和左边进行某一种运算的最大值,当t==2时,还需要求最大值的个数. test1 20% n<=1000 O(n^2)暴力 test2 2 ...

  7. NOI模拟赛Day4

    看到成绩的时候我的内心** woc第一题写错了呵呵呵呵呵呵呵呵 人不能太浪,会遭报应的** ------------------------------------------------------ ...

  8. NOI模拟赛Day3

    终于A题啦鼓掌~开心~ 开考看完题后,觉得第二题很好捏(傻叉上线 搞到十一点准备弃疗了然后突然发现我会做第一题 于是瞎码了码,就去准备饭票了... 好了,停止扯淡(就我一个我妹子每天不说话好难受QAQ ...

  9. NOI模拟赛Day2

    深深的感受到了自己的水 ---------------------------------------------------------------------------------------- ...

随机推荐

  1. python之yaml模块和ddt模块

    aml文件是专门用来写配置文件的语言,非常简洁和强大,远比json格式方便. 在PC中新建一个yml/yaml为为缩略名的文件,输入信息见下图 新建一个py文件处理yml文件,直接处理成字典格式 缩进 ...

  2. 封装jQuery插件实现TAB切换

    先上效果图: 直接上代码: index.html <!DOCTYPE html> <html lang="en"> <head> <met ...

  3. Codeforces Round #321 (Div. 2) A, B, C, D, E

    580A. Kefa and First Steps 题目链接: A. Kefa and First Steps 题意描述: 给出一个序列,求最长不降连续子序列多长? 解题思路: 水题,签到 代码: ...

  4. Mirror Number SPOJ - MYQ10

    Mirror Number SPOJ - MYQ10 题意:http://blog.csdn.net/hcbbt/article/details/38349367 稍微改一下http://www.cn ...

  5. [已读]跨终端web

    13年去听阿里技术嘉年华,鬼道分享了<移动优先前端产品的探索>.今年我买这本书,事实上是被高大上的目录吸引→ → 买来后发现,嘿,似曾相识啊,但还是老老实实得花一下午把书翻了一遍.翻完之后 ...

  6. UGUI_屏幕适配

    引用:http://www.xuanyusong.com/archives/3278#comments 1.可以选择的有三种: 1.Screen Space – overlay  此模式不需要UI摄像 ...

  7. P1739 表达式括号匹配

    题目描述 假设一个表达式有英文字母(小写).运算符(+,—,*,/)和左右小(圆)括号构成,以“@”作为表达式的结束符.请编写一个程序检查表达式中的左右圆括号是否匹配,若匹配,则返回“YES”:否则返 ...

  8. git分支提交管理

    随着需求的增多,为了多人协作的顺利进行,需要进行分支开发,进而带来分支管理问题.今天主要讲一下如何管理分支及提交. 为了使git更好用,下面是我的git配置文件(放在C:\Users\Administ ...

  9. PKU_campus_2018_A Wife

    思路: 题目链接http://poj.openjudge.cn/practice/C18A/ 先说一个结论,每一天要么7要么0,由此提供一种状态压缩dp的解法. 实现: #include <bi ...

  10. 2018.7.21NOIP模拟赛?解题报告

    题面 预计得分:70 + 60 + 30 = 160 实际得分:40 + 60 + 0 = 100 T1数组开小了 T2比赛结束后5min AC T3加了个记忆话搜索wa了.. T1 zbq吊打std ...