tj
2026-05-17 11:16:27
发布于:广东
7阅读
0回复
0点赞
吓尿了一次过(其实是在DEV调的
最标准写法
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3+9;
int fa[maxn];
vector<int >ve[maxn];
int get(int x)
{
if(fa[x] == x)
return x;
return fa[x] = get(fa[x]);
}
void merge(int x,int y)
{
fa[get(y)] = get(x);
}
int main(){
int n,m;cin >> n >> m;
for(int i = 1;i <= n;i++)
fa[i] = i;
while(m--)
{
char opt;int p,q;cin >> opt >> p >> q;
if(opt == 'E')
{
ve[p].push_back(q);
ve[q].push_back(p);
}
else
{
if(get(p) != get(q))
merge(p,q);
}
}
for(int i = 1;i <= n;i++)
{
for(int j = 1;j < ve[i].size();i++)
{
if(get(ve[i][j]) != get(ve[i][j-1]))
merge(ve[i][j],ve[i][j-1]);
}
}
int ans = 0;
for(int i = 1;i <= n;i++)
if(fa[i] == i)
ans++;
cout<<ans;
}
成功抢第一
全部评论 2
回我消息
2026-05-18 来自 浙江
0d
2026-05-17 来自 广东
0








有帮助,赞一个