2843: 极地旅行社
Description
Input
Output
对于每个bridge命令与excursion命令,输出一行,为题目描述所示。
Sample Input
4 2 4 5 6
10
excursion 1 1
excursion 1 2
bridge 1 2
excursion 1 2
bridge 3 4
bridge 3 5
excursion 4 5
bridge 1 3
excursion 2 4
excursion 2 5
Sample Output
impossible
yes
6
yes
yes
15
yes
15
16
1 #include<iostream> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<cstdio> 6 #include<algorithm> 7 #include<string> 8 #include<map> 9 #include<queue> 10 #include<vector> 11 #include<set> 12 #define inf 1000000000 13 #define maxn 30000+5 14 #define maxm 10000+5 15 #define eps 1e-10 16 #define ll long long 17 #define for0(i,n) for(int i=0;i<=(n);i++) 18 #define for1(i,n) for(int i=1;i<=(n);i++) 19 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 20 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 21 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) 22 using namespace std; 23 int read(){ 24 int x=0,f=1;char ch=getchar(); 25 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 26 while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} 27 return x*f; 28 } 29 int n,m,c[maxn][2],fa[maxn],v[maxn],sum[maxn],sta[maxn],f[maxn]; 30 bool rev[maxn]; 31 bool isroot(int x){ 32 return c[fa[x]][0]!=x&&c[fa[x]][1]!=x; 33 } 34 void pushup(int x){ 35 sum[x]=sum[c[x][1]]+sum[c[x][0]]+v[x]; 36 } 37 void rever(int x){ 38 rev[x]^=1; 39 swap(c[x][1],c[x][0]); 40 } 41 void pushdown(int x){ 42 if(!rev[x])return ; 43 rever(c[x][1]);rever(c[x][0]); 44 rev[x]=0; 45 } 46 void rotate(int x){ 47 int y=fa[x],z=fa[y],l=c[y][1]==x,r=l^1; 48 if(!isroot(y))c[z][c[z][1]==y]=x; 49 fa[x]=z;fa[y]=x;fa[c[x][r]]=y; 50 c[y][l]=c[x][r];c[x][r]=y; 51 pushup(y);pushup(x); 52 } 53 void splay(int x){ 54 int top=0;sta[++top]=x; 55 for(int y=x;!isroot(y);y=fa[y])sta[++top]=fa[y]; 56 for(;top;)pushdown(sta[top--]); 57 while(!isroot(x)){ 58 int y=fa[x],z=fa[y]; 59 if(!isroot(y)){ 60 if(c[z][0]==y^c[y][0]==x) 61 rotate(x); 62 else rotate(y); 63 } 64 rotate(x); 65 } 66 } 67 void access(int x){ 68 for(int y=0;x;x=fa[x]){ 69 splay(x);c[x][1]=y;pushup(x);y=x; 70 } 71 } 72 void makeroot(int x){ 73 access(x);splay(x);rever(x); 74 } 75 void split(int x,int y){ 76 makeroot(x);access(y);splay(y); 77 } 78 int findroot(int x){ 79 return f[x]==x?x:f[x]=findroot(f[x]); 80 } 81 void link(int x,int y){ 82 if(findroot(x)==findroot(y)){ 83 printf("no\n"); 84 return ; 85 } 86 makeroot(x);fa[x]=y;splay(x);f[findroot(x)]=findroot(y); 87 printf("yes\n"); 88 } 89 void query(int x,int y){ 90 if(findroot(x)==findroot(y)){ 91 split(x,y),printf("%d\n",sum[y]); 92 return ; 93 } 94 printf("impossible\n"); 95 } 96 int main(){ 97 //freopen("input.txt","r",stdin); 98 //freopen("output.txt","w",stdout); 99 n=read(); 100 for1(i,n)v[i]=sum[i]=read(),f[i]=i; 101 m=read(); 102 while(m--){ 103 char ch=getchar(); 104 while(ch!='e'&&ch!='b'&&ch!='p')ch=getchar(); 105 int x=read(),y=read(); 106 if(ch=='e')query(x,y); 107 else if(ch=='b')link(x,y); 108 else makeroot(x),v[x]=y,pushup(x); 109 } 110 return 0; 111 }
View Code
转载于:https://www.cnblogs.com/htwx/articles/5659382.html
2843: 极地旅行社相关推荐
- bzoj 2843: 极地旅行社
Time Limit: 10 Sec Memory Limit: 256 MB Submit: 1077 Solved: 645 [Submit][Status][Discuss] Descrip ...
- BZOJ 2843 极地旅行社
[题解] 这道题其实有个树状数组维护dfs序的做法,不过懒得想那么多直接写了Link-Cut Tree 1 #include<cstdio> 2 #include<algorithm ...
- bzoj2843极地旅行社题解
bzoj2843极地旅行社题解 题目大意 有n座小岛,当中每一个岛都有若干帝企鹅. 一開始岛与岛之间互不相连.有m个操作.各自是在两个岛之间修一座双向桥,若两岛已连通则不修并输出no,若不连通就输出y ...
- bzoj2843极地旅行社
bzoj2843极地旅行社 题意: 一些点,每个点有一个权值.有三种操作:点与点连边,单点修改权值,求两点之间路径上点的权值和(需要判输入是否合法) 题解: 以前一直想不通为什么神犇们的模板中LCT在 ...
- BZOJ2843 - 极地旅行社
Portal Description 给出\(n(n\leq3\times10^4)\)个独立的带权顶点,对其进行\(m(m\leq10^5)\)次操作: 连接两个不连通的节点.如果它们已经联通,输出 ...
- 【BZOJ 2843】极地旅行社
复习一下Link Cut Tree的模板. #include<cstdio> #include<cstring> #include<algorithm> #defi ...
- BZOJ2843:极地旅行社
Description 不久之前,Mirko建立了一个旅行社,名叫"极地之梦".这家旅行社在北极附近购买了N座冰岛,并且提供观光服务. 当地最受欢迎的当然是帝企鹅了,这些小家伙经常 ...
- 极地旅行社[LCT]
题目描述 不久之前,Mirko建立了一个旅行社,名叫"极地之梦".这家旅行社在北极附近购买了N座冰岛,并且提供观光服务. 当地最受欢迎的当然是帝企鹅了,这些小家伙经常成群结队的游走 ...
- [COCI 2009] OTOCI / 极地旅行社
题目描述 不久之前,Mirko 建立了一个旅行社,名叫"极地之梦".这家旅行社在北极附近购买了 nn 座冰岛,并且提供观光服务. 当地最受欢迎的当然是帝企鹅了,这些小家伙经常成群结 ...
最新文章
- WildFly评估之WildFly的模块化系统
- oracle数据库如何写翻页_ORACLE数据库分页查询/翻页 最佳实践
- ECCV 2018 | OR-CNN行人检测:为‘遮挡’而生
- python打印数组中期望元素的位置
- Java 复习笔记 线程Thread
- js的异常处理 try catch
- perl语言得到的txt文档只有表头_编程语言排行榜第一Python,为何频繁遭受开发者的嫌弃!...
- [毕设分享]基于华为eNSP的校园网设计和仿真模拟
- 人人都是产品经理 2.0
- 忙碌了一整天,终于弄好了免费电脑版的KTV点歌系统,以后可以K歌了!
- js-beautify-html配置,HTML-Beautify.js学习
- 律师向公安部举报称微软黑屏是最大黑客行为
- Xcode各版本和IOS模拟器个版本下载
- 小米拒绝权限_小米手机 MIUI 12 测试新隐私保护:可设置每个应用是否允许访问相册和信息存储目录...
- H5版仿制微信跳一跳小游戏,网页版仿微信跳一跳小游戏源码,实现了跳一跳的基本核心功能
- python代码学习
- 从零开始:小程序开发环境搭建详解
- Java Core-第1章 Java程序入门
- Java实现xml文件的xsd校验(schema校验)
- linux怎么撤销关机命令,Linux怎么关机
热门文章
- 如何打败理发店装逼洗剪吹小哥
- 6张图让你搞懂浏览器渲染网页过程
- OpenFeign微服务调用传参为空的问题
- 数据库导入到hive中出现Can't open file: './mxw20_g107/log_action.frm' (errno: 24 - Too many open files)
- 2019海南大学计算机研究生分数线,2021海南大学研究生分数线一览表(含2019 2020历年复试分数线)...
- ROHM | 开发出可简化视频传输路径的、 用于车载多屏显示器的串行/解串器“BU18xx82-M”
- Windows 7 瘦身大全 系统减肥 轻松 瘦身 减肥 3个G
- 这几个好用的文本翻译工具推荐给你
- 送一台2020最新款笔记本电脑
- 切线和倒数_法线与切线的斜率关系