计蒜客第七章:农场看守

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/shidonghang/article/details/102702380

计蒜客习题:农场看守

题目

在这里插入图片描述

样例

在这里插入图片描述

思路

离散图,采用链表存储,每条边带有标记位,然后过一遍DFS并在每一层输出点即可。

代码

#include<iostream>
#include<vector>
using namespace std;
int n,m;
vector<pair<int,bool> > mp[50010];
void dfs(int u)
{
    for(int i=0;i<mp[u].size();i++)
    {
        pair<int,bool> &e=mp[u][i];
        if(!e.second)
        {
            e.second=true;
            dfs(e.first);
        }
    }
    printf("%d\n",u);
}
int main()
{
    scanf("%d %d",&n,&m);
    while(m--)
    {
        int x,y;
        scanf("%d %d",&x,&y);
        mp[x].push_back(pair<int,bool>(y,false));
        mp[y].push_back(pair<int,bool>(x,false));
    }
    dfs(1);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/shidonghang/article/details/102702380