Miracle_InDream @ 2024-08-09 10:11:54
#include<bits/stdc++.h>
using namespace std;
int f[400005];
int n,m;
int ans=0;
int find(int x)
{
if(x==f[x])
{
return x;
}
return f[x]=find(f[x]);
}
void hb(int x,int y)
{
int xx=find(x);
int yy=find(y);
if(xx!=yy)
{
f[xx]=yy;
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
f[i]=i;
}
for(int i=1;i<=m;i++)
{
int x,y;
cin>>x>>y;
hb(x,y);
}
int k;
cin>>k;
for(int i=1;i<=k;i++)
{
int ans=0;
int x;
cin>>x;
f[x+1]=x+1;
for(int j=1;j<=n;j++)
{
if(f[j]==j)
{
ans++;
}
}
cout<<ans<<endl;
}
return 0;
}
by Miracle_InDream @ 2024-08-09 10:15:10
emmm……忘了题目下标是从
#include<bits/stdc++.h>
using namespace std;
int f[400005];
int n,m;
int ans=0;
int find(int x)
{
if(x==f[x])
{
return x;
}
return f[x]=find(f[x]);
}
void hb(int x,int y)
{
int xx=find(x);
int yy=find(y);
if(xx!=yy)
{
f[xx]=yy;
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
f[i]=i;
}
for(int i=1;i<=m;i++)
{
int x,y;
cin>>x>>y;
hb(x+1,y+1);
}
int k;
cin>>k;
for(int i=1;i<=k;i++)
{
int ans=0;
int x;
cin>>x;
f[x+1]=x+1;
for(int j=1;j<=n;j++)
{
if(f[j]==j)
{
ans++;
}
}
cout<<ans<<endl;
}
return 0;
}
by fzy_qwq @ 2024-08-09 10:20:08
@shooting__star ctj
by Miracle_InDream @ 2024-08-09 10:20:44
@fzy_qwq 什么东西
by fzy_qwq @ 2024-08-09 10:23:19
@shooting__star 抄题解
by Miracle_InDream @ 2024-08-09 10:26:16
@fzy_qwq 我的意思是!我!不!干!
by yhylivedream @ 2024-08-09 10:27:00
@shooting__star 自己调啊,样例没过你不能调试吗你不能输出变量吗?
by qwertim @ 2024-08-09 10:29:38
这显然会T吧
by ragwort @ 2024-08-09 10:33:07
@shooting__star 显然样例就是用来给你调的
by dyyzy @ 2024-08-09 10:51:24
@shooting__star 哥你并查集的撤销写挂了。建议看一眼题解
by Miracle_InDream @ 2024-08-09 10:52:28
@dyyzy ……6