1 条题解

  • 2
    @ 2023-7-16 16:22:40
    #include <bits/stdc++.h>
    #define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    #define re register
    using namespace std;
    typedef long long ll;
    int n;
    bool mp[1010][1010], vis1[1010], vis2[1010];
    void dfs(int now)
    {
    	cout<<now+1<<" ";
    	for(int i=0; i<n; i++) if(!vis1[i] && mp[now][i]) vis1[i]=true, dfs(i);
    	return;
    }
    void bfs()
    {
    	queue<int> q;
    	q.push(0);
    	vis2[0]=true;
    	while(q.size())
    	{
    		int now=q.front();
    		q.pop();
    		cout<<now+1<<" ";
    		for(int i=0; i<n; i++) if(!vis2[i] && mp[now][i]) vis2[i]=true, q.push(i);
    	}
    	return;
    }
    int main()
    {
    	IOS;
    	cin>>n;
    	for(int i=0; i<n; i++) for(int j=0; j<n; j++) cin>>mp[i][j];
    	vis1[0]=true;
    	dfs(0), cout<<endl, bfs();
    	return 0;
    }
    
    • 1

    信息

    ID
    467
    时间
    1000ms
    内存
    256MiB
    难度
    2
    标签
    递交数
    150
    已通过
    98
    上传者