社区讨论

信友队T4 10pts 求调

题目总版参与者 3已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@m3tr9elx
此快照首次捕获于
2024/11/23 13:54
去年
此快照最后确认于
2025/11/04 14:07
4 个月前
查看原帖
CPP
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int T,n,m,i,x,y,ans;
vector<int> G[N];
bool vis[N];
int p[N];
void dfs(int x)
{
	vis[x]=true;
	for (int to:G[x])
	{
		if (!vis[to]) dfs(to);
	}
}
void work()
{
	ans=0;
	scanf("%d%d",&n,&m);
    for (i=1;i<=n;i++) G[i].clear();
	for (i=1;i<=m;i++)
	{
		scanf("%d%d",&x,&y);
		G[x].push_back(y);
		G[y].push_back(x);
	}
	for (i=1;i<=n;i++) vis[i]=false;
	for (i=1;i<=n;i++)
	{
		if (!vis[i]) 
		{
			p[++ans]=i;
			dfs(i);
		}
	}
	printf("%d\n",ans-1);
    for (i=1;i<=ans-1;i++) printf("%d %d\n",p[i],p[i+1]);
}
int main()
{
	freopen("destiny.in","r",stdin);
	freopen("destiny.out","w",stdout); 
	scanf("%d",&T);
	while (T--) work();
	return 0;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...