제출 #1150747

#제출 시각아이디문제언어결과실행 시간메모리
1150747bilgunNewspapers (CEOI21_newspapers)C++20
0 / 100
0 ms324 KiB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1005;
vector<int> gph[MAXN];
int par[MAXN];

pair<int, int> dfs(int x, int p = -1) {
    pair<int, int> ret(0, x);
    for (int y : gph[x]) {
        if (y != p) {
            par[y] = x;
            auto res = dfs(y, x);
            res.first++;
            ret = max(ret, res);
        }
    }
    return ret;
}

int main() {
    int n, m;
    cin >> n >> m;

    if (m != n - 1) {
        cout << "NO" << endl;
        return 0;
    }

    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        gph[u].push_back(v);
        gph[v].push_back(u);
    }

    int u = dfs(1).second;
    int v = dfs(u).second;

    vector<int> path;
    for (int i = v; i != u; i = par[i]) {
        path.push_back(i);
    }
    path.push_back(u);

    cout << "YES" << endl;
    cout << 2 * path.size() << endl;

    for (int i = 0; i < path.size(); i++) {
        cout << path[i] << " ";
    }
    reverse(path.begin(), path.end());
    for (int i = 1; i < path.size(); i++) {
        cout << path[i] << " ";
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...