日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[swustoj 856] Huge Tree

發布時間:2025/3/14 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [swustoj 856] Huge Tree 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Huge Tree(0856)

問題描述

There are N trees in a forest. At first, each tree contains only one node as its root. And each node is marked with a number.

You're asked to do the following two operations:

A X Y, you need to link X's root to Y as a direct child. If X and Y have already been in the same tree, ignore this operation.

B X, you need to output the maximum mark in the chain from X to its root (inclusively).

輸入

The first line contains an integer T, indicating the number of followed cases. (1 <= T <= 20)

For each case, the first line contains two integers N and M, indicating the number of trees at beginning, and the number of operations follows, respectively. (1 <= N, M <= 100,000)

And the following line contains N integers, which are the marks of the N trees. (0 <= Mark <= 100,000)

And the rest lines contain the operations, in format A X Y, or B X, (0 <= X, Y < N).

輸出

For each 'B X' operation, output the maximum mark.

樣例輸入

1
5 5
5 4 2 9 1
A 1 2
A 0 4
B 4
A 1 0
B 1?

樣例輸出

1
5

簡單并查集、

#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define N 100010int n,m; int val[N]; int mx[N]; int f[N];void init() {for(int i=1;i<=n;i++){f[i]=i;mx[i]=val[i];} } int Find(int x) {if(x==f[x]) return x;int t=f[x];f[x]=Find(t);mx[x]=max(mx[x],mx[t]);return f[x]; } void UN(int x,int y) {x=Find(x);//y=Find(y);if(x==y) return;f[x]=y; } int main() {int T;scanf("%d",&T);while(T--){scanf("%d%d",&n,&m);for(int i=1;i<=n;i++) scanf("%d",&val[i]);init();while(m--){char op;int a,b;scanf(" %c",&op);if(op=='A'){scanf("%d%d",&a,&b);a++;b++;UN(a,b);}else{scanf("%d",&a);a++;Find(a);printf("%d\n",mx[a]);}}}return 0; }

?

轉載于:https://www.cnblogs.com/hate13/p/4507943.html

總結

以上是生活随笔為你收集整理的[swustoj 856] Huge Tree的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。