1 条题解

  • 0
    @ 2026-4-16 19:51:58

    我当时为什么没做出来???

    T1 DFS,T2 BFS 直接完事了。

    #include<bits/stdc++.h>
    using namespace std;
    const int N=2e5+10;
    vector<int>G[N];
    int v[N];
    void dfs(int x)
    {
    	v[x]=1;
    	for(int y:G[x])if(!v[y])
    	{
    		cout<<x<<' '<<y<<'\n';
    		dfs(y);
    	}
    }
    signed main()
    {
    	int n,m;cin>>n>>m;
    	for(int i=1;i<=m;i++)
    	{
    		int x,y;cin>>x>>y;
    		G[x].push_back(y);
    		G[y].push_back(x);
    	}
    	dfs(1);
    	memset(v,0,sizeof(v));
    	deque<int>q;q.push_back(1);v[1]=1;
    	while(!q.empty())
    	{
    		int x=q.front();q.pop_front();
    		for(int y:G[x])if(!v[y])
    		{
    			cout<<x<<" "<<y<<'\n';
    			v[y]=1;q.push_back(y);
    		}
    	}
    	return 0;
    }
    • 1

    信息

    ID
    9908
    时间
    2000ms
    内存
    1024MiB
    难度
    10
    标签
    递交数
    5
    已通过
    3
    上传者