Lyp
来源:互联网 发布:什么是网络安全意识 编辑:程序博客网 时间:2024/05/29 14:43
众所周知,我们的 lyp 神犇外号叫Altman,的确,在另一个平行宇宙,lyp神犇就是一个——Altman。
有一天,lyp神犇遇到了另一个平行宇宙中的他,得知了在其他的宇宙中,Altman是存在的,那么,怪兽也是存在的咯。
作为一个有名的oier,lyp神犇想要统计一下各个宇宙中怪兽的战斗力,他发现,一些怪兽在不同的宇宙都出现过(!. !,难不成怪兽不穿越,算了,不管了),每一个Altman都提供给lyp一些信息,告诉他一个怪兽比另一个怪兽的战斗力大多少(这个数可以是负数)。
Altman神犇为了给你以表现机会,将这些信息都给你,并会时不时地问你两个怪兽之间谁更强。
假设Altman们提到了怪兽都按1~n编号。
Altman们的信息的格式是
其中
Lyp的询问的格式是:
输入描述 Input Description
第
第
输出描述 Output Description
对于每一个询问,用单独一行回答。
输入样例 Sample Input
3 3
1 1 2 3
1 3 2 1
2 1 3
输出描述 Sample Output
1
数据范围 Data Size
分析 I Think
特殊的路径压缩,设
当我们需要连边
代码 Code
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int Size = 30300;int father[Size];int f[Size];int g[Size];int n,m;void read(int &);int find(int);void uni(int,int,int);void updata(int);int main(){ read(n); read(m); for(int i=1;i<=n;++i) father[i] = i; int type,x,y,a; while( m-- ){ read(type); if(type == 1){ read(x); read(y); read(a); uni(x,y,a); } else{ read(x); read(y); updata(x); updata(y); if(find(x)!=find(y) || f[x]==f[y]) printf("0\n"); else if(f[x] > f[y]) printf("%d\n",x); else printf("%d\n",y); } } return 0;}void read(int &_in){ _in = 0; int flag = 1; char ch = getchar(); while(ch!='-' && (ch>'9'||ch<'0')) ch = getchar(); if(ch == '-'){ flag = -1; ch = getchar(); } while(ch<='9' && ch>='0'){ _in = _in*10+ch-'0'; ch = getchar(); } _in *= flag;}int find(int x){ if(x != father[x]) father[x] = find(father[x]); return father[x];}void uni(int x,int y,int c){ int p = find(x); int q = find(y); updata(x); updata(y); f[p] = c+f[y]-f[x]; father[p] = g[p] = q;}void updata(int x){ while(g[g[x]]){ f[x] += f[g[x]]; g[x] = g[g[x]]; }}