qwq

AGC013B 题解

注意到只要随便 dfs,如果没有可以走的点,说明这个端点满足要求。

因为有两个端点,所以从同一个点开始搜两次,拼在一起就行了。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N = 1e5 + 5;

vector<int> e[N];
int n, m;

bool vis[N];
void dfs(int x, int fa, vector<int> &ans)
{
    vis[x] = 1;
    for(int i : e[x])
    {
        if(vis[i]) continue;
        ans.push_back(i);
        return dfs(i, x, ans);
    }
}

signed main()
{
    ios::sync_with_stdio(0);cin.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= m; i ++)
    {
        int x, y; cin >> x >> y;
        e[x].push_back(y);
        e[y].push_back(x);
    }
    vector<int> ans1, ans2;
    dfs(1, 0, ans1);
    dfs(1, 0, ans2);
    reverse(ans1.begin(), ans1.end());
    cout << ans1.size() + ans2.size() + 1 << "\n";
    for(int i = 0; i < ans1.size(); i ++) cout << ans1[i] << " ";
    cout << 1 << " ";
    for(int i = 0; i < ans2.size(); i ++) cout << ans2[i] << " ";

    return 0;
}

作者:adam01

出处:https://www.cnblogs.com/adam01/p/18340352

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   adam01  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示
more_horiz
keyboard_arrow_up light_mode palette
选择主题