Submission #912830

#TimeUsernameProblemLanguageResultExecution timeMemory
912830juliany2Newspapers (CEOI21_newspapers)C++17
4 / 100
4 ms604 KiB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()

const int N = 1007;
int n, m;
vector<int> adj[N], cur;
int leaf[N];

void dfs(int v) {
    int t = -1;
    for (int u : adj[v]) {
        if (leaf[u])
            leaf[u] = 0;
        else {
            if (t == -1)
                t = u;
            else {
                t = -2;
                break;
            }
        }
    }

    cur.push_back(v);
    leaf[v] = 1;
    if (t > 0)
        dfs(t);
}

int main() {
    cin.tie(0)->sync_with_stdio(false);

    cin >> n >> m;

    for (int i = 1; i <= m; i++) {
        int u, v;
        cin >> u >> v;

        adj[u].push_back(v);
        adj[v].push_back(u);
    }

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

    vector<int> ans;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++)
            leaf[j] = adj[j].size() == 1;
        cur.clear();

        dfs(i);

        if (accumulate(leaf + 1, leaf + n + 1, 0) == 1) {
            for (int j = 1; j <= n; j++)
                if (leaf[j] == 1)
                    cur.push_back(j);

            if (ans.empty() || cur.size() < ans.size())
                ans = cur;
        }
    }

    if (ans.empty())
        cout << "NO" << '\n';
    else {
        cout << "YES" << '\n' << ans.size() << '\n';
        for (int x : ans)
            cout << x << ' ';
        cout << '\n';
    }

    return 0;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...