HELLO WORLD--一起加油(🍺)!|

kingwzun

园龄:3年6个月粉丝:111关注:0

补题*总结题21/9/2

A - Takahashi Tour
链接:AtCoder - abc213_d
题意:
按一下规则遍历图:
每次优先遍历序号较小的结点,
如无结点可遍历则返回上一结点

思路:
DFS

#include <bits/stdc++.h>
using namespace std;
const int Int = 2e5+5;
vector<int> ma[Int];
int vi[Int];
int n;
void find_in(int y)
{
cout<<y<<' ';
vi[y]=1;
int i;
int len=ma[y].size();
for(i=0; i<len; i++)
{
int o= ma[y][i];
if(vi[ ma[y][i] ]==0)
{
find_in(o);
cout<<y<<' ';
}
}
}
int main()
{
memset(ma,0,sizeof(ma));
memset(vi,0,sizeof(vi));
cin>>n;
int i;
int a,b;
for(i=0; i<n-1; i++)
{
cin>>a>>b;
ma[a].push_back(b);
ma[b].push_back(a);
}
for(i=1; i<=n; i++)
{
sort(ma[i].begin(),ma[i].end());
}
//cout<<1;
find_in(1);
cout<<endl;
return 0;
}

本文作者:kingwzun

本文链接:https://www.cnblogs.com/kingwz/p/15223969.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   kingwzun  阅读(28)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起