acwing 837 连通块中点的数量 2022/03/06
#include<iostream>using namespace std ;const int N = 1e5 + 10 ;int p[N] ,si[N] ;int n , m ;int find(int x){if(p[x] != x) p[x] = find(p[x]);return p[x] ;}int main(){cin >> n >> m ;for
·
#include<iostream>
using namespace std ;
const int N = 1e5 + 10 ;
int p[N] ,si[N] ;
int n , m ;
int find(int x)
{
if(p[x] != x) p[x] = find(p[x]);
return p[x] ;
}
int main()
{
cin >> n >> m ;
for(int i = 1 ; i <= n ; i++)
{
p[i]= i ;
si[i] = 1 ;
}
while(m--)
{
char op[5] ;
int a ,b ;
scanf("%s" , op);
if(op[0] == 'C')
{
cin >> a >> b ;
if(find(a) == find(b)) continue ;
si[find(b)] = si[find(b)] + si[find(a)] ;
p[find(p[a])] = find(p[b]) ;
}
else if(op[1] == '1')
{
cin >> a >> b ;
if(find(a) == find(b))cout << "Yes"<< endl ;
else cout << "No" << endl ;
}
else
{
cin >> a ;
cout << si[find(a)] << endl ;
}
}
return 0 ;
}

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)