4 条题解

  • 1
    @ 2024-5-19 17:21:16

    要炒窝的把注释删了

    #include <bits/stdc++.h>
    using namespace std; 
    bool a[20][20];
    int n,e,x,y;
    bool f[20];
    void dfs(int x){
    	f[x]=true;//发现走过一个点,输出一个点
    	cout<<x<<" ";
            //没走过并且能从x出发到这个点
    	for(int i=1;i<=n;i++){
    		if(!f[i]&&a[x][i]){
    			dfs(i);
    		}
    	}
    }
    int main() {
    	cin>>n>>e;
    	for(int i=1;i<=e;i++){
    		cin>>x>>y;
    		a[x][y]=1;
    		a[y][x]=1;//无向图双相建边
    	}
    	dfs(1);
    	return 0;
    }
    
    • 0
      @ 2024-2-21 21:39:56
      #include <bits/stdc++.h>
      using namespace std;
      int n,e;
      int a[20][20];
      bool f[20];
      void dfs(int x){
      	f[x]=true;
      	cout<<x<<" ";
      	for (int i=1;i<=n;i++){
      		if (!f[i]&&a[x][i])dfs(i);
      	}
      }
      int main(){
      	cin>>n>>e;
      	int x,y;
      	for (int i=1;i<=e;i++){
      		cin>>x>>y;
      		a[x][y]=1;
      		a[y][x]=1;
      	}
      	dfs(1);
      	return 0;
      }
      
      • 0
        @ 2022-4-1 17:22:46
        
        #include<bits/stdc++.h>
        using namespace std;
        int n,m,u,v;
        vector<vector<bool> >e;
        bool vis[15];
        /*
        邻接矩阵做法 
        查询是否存在某条边:O(1)。
        遍历一个点的所有出边:O(N)。
        遍历整张图:O(N*N)。
        空间复杂度:O(N*N)。
        */
        void dfs(int u)
        {
        	if(vis[u]) return ;
        	vis[u]=true;
        	cout<<u<<" ";
        	for (int v = 1; v <= n; ++v) 
        	{
            	if(e[u][v]) 
        		{
              		dfs(v);
            	}
          	}
        }
        int main() 
        {
        	ios::sync_with_stdio(false);
        	cin.tie(0);
        	cin>>n>>m;
        	e.resize(n+1,vector<bool>(n+1,false));
        	for(int i=1;i<=m;i++)
        	{
        		cin>>u>>v;
        		e[u][v]=true;//双向存边 
        		e[v][u]=true;
        	}
        	dfs(1);
          	return 0;
        }
        • -1
          @ 2022-4-19 22:44:25

          严禁抄题解,发现后取消成绩

          • 1

          信息

          ID
          1048
          时间
          1000ms
          内存
          128MiB
          难度
          4
          标签
          递交数
          29
          已通过
          17
          上传者