inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9') w=ch=='-'?-1:1,ch=getchar();
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
inline void write(int x){
if(x<0){
putchar('-');
x=-x;
}
if(x>9) write(x/10);
putchar(x%10+'0');
}
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+10,mod=1e9+7,INF=0x3f3f3f3f3f3f3f3f,lim=20;
int n,tot,ans;pair<pair<int,int>,int>a[N];
namespace Fast_IO
{
static char buf[1000000],*paa=buf,*pd=buf,out[10000000];int length=0;
#define getchar() paa==pd&&(pd=(paa=buf)+fread(buf,1,1000000,stdin),paa==pd)?EOF:*paa++
inline int read()
{
int x(0),t(1);char fc(getchar());
while(!isdigit(fc)){if(fc=='-') t=-1;fc=getchar();}
while(isdigit(fc)) x=(x<<1)+(x<<3)+(fc^48),fc=getchar();
return x*t;
}
inline void flush(){fwrite(out,1,length,stdout);length=0;}
inline void put(char c){if(length==9999999) flush();out[length++]=c;}
inline void put(string s){for(char c:s) put(c);}
inline void print(int x)
{
if(x<0) put('-'),x=-x;
if(x>9) print(x/10);
put(x%10+'0');
}
inline bool chk(char c) { return !(c>='a'&&c<='z'); }
inline bool ck(char c) { return c!='\n'&&c!='\r'&&c!=-1&&c!=' '; }
inline void rd(char s[],int&n)
{
s[++n]=getchar();
while(chk(s[n])) s[n]=getchar();
while(ck(s[n])) s[++n]=getchar();
n--;
}
}
using namespace Fast_IO;
//并查集
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+5;
int fa[N], siz[N];
int n, m;
int find(int x){
if(fa[x] == x) return x; // 已经是根节点
else return fa[x] = find(fa[x]); // 继续找
}
int main(){
cin >> n >> m;
for(int i = 1; i <= n; ++i) fa[i] = i, siz[i] = 1; // 初始化
for(int i = 1; i <= m; ++i){
int op, x, y;
cin >> op >> x >> y;
if(op == 1){ // 合并操作
x = find(x), y = find(y); // 查询各自的代表元素
if(x == y) continue; // 如果已经在同一个集合,跳过
if(siz[y] < siz[x]) swap(x, y); // 改变合并顺序
if(siz[x] == siz[y]) siz[y] = siz[x]+1; // 计算新的秩:如果两个集合的秩相等,那么秩加一
// 否则秩不变
fa[x] = y; // 合并
}
else{ // 查询操作的一种变形
x = find(x), y = find(y); // 查询各自的代表元素
if(x == y) cout << "Y" << endl; // 是否在同一个集合
else cout << "N" << endl;
}
}
return 0;
}xianliyun
1361272
愿我们能在未来光明处相遇
资料、奖项与版本
资料
- 编号
- 1361272
- 抓取
奖项
- 2025 CSP-S
- 一等奖
- 2025 NOIP
- 二等奖
- 2024 CSP-J
- 一等奖
- 2024 CSP-S
- 一等奖
- 2023 CSP-J
- 一等奖
个人介绍
文章
还没有收录这位用户的文章。