Submission #800024

# Submission time Handle Problem Language Result Execution time Memory
800024 2023-08-01T09:27:15 Z Johann Newspapers (CEOI21_newspapers) C++14
0 / 100
0 ms 212 KB
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

int N, M;
vvi adj;
vvi adj2;
bool possible;
vi cnt[2];
vvi CNT[2];
vi ans;
vi bw;
vi BW[2];
bool usable;

void dfsBW(int v, int p)
{
    bw[v] = !bw[p];
    BW[bw[v]].push_back(v);
    for (int u : adj[v])
        if (u != p)
            dfsBW(u, v);
}

void dfs(int v, int p)
{
    cnt[0][v] = 1;
    cnt[1][v] = 0;
    for (int u : adj[v])
        if (u != p)
        {
            dfs(u, v);
            cnt[0][v] += cnt[1][u];
            cnt[1][v] += cnt[0][u];
        }
}

void dfsAns(int v, int p)
{
    ans.push_back(v); // safe besuchen

    vi todo;
    for (int u : adj2[v])
        if (u != p)
            todo.push_back(u);

    dfs(v, v);

    if (sz(todo) <= 1)
    {
        for (int u : adj[v])
            if (u != p && (todo.empty() || u != todo[0]) && cnt[1][u] > 0)
            {
                assert(cnt[0][u] == 1);
                ans.push_back(u);
                ans.push_back(v);
            }
        if (sz(todo) == 1)
        {
            dfsAns(todo[0], v);
        }
    }
    else
    {
        // must be 2
        usable = false;
        // assert(false);
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> N >> M;
    if (M > N - 1)
    {
        cout << "NO\n";
        return 0;
    }

    adj.resize(N);
    for (int i = 0, a, b; i < M; ++i)
    {
        cin >> a >> b;
        --a, --b;
        adj[a].push_back(b), adj[b].push_back(a);
    }

    bw.assign(N, 0);
    dfsBW(0, 0);

    adj2.resize(N);
    possible = true;
    cnt[0].resize(N), cnt[1].resize(N);
    for (int v = 0; v < N; ++v)
    {
        dfs(v, v);
        for (int u : adj[v])
            if (cnt[0][u] >= 2)
                adj2[v].push_back(u);
        if (sz(adj2[v]) > 2)
            possible = false;
    }

    if (possible)
    {
        cout << "YES\n";

        vi finalAns;
        int color = 0;
        if (sz(BW[!color]) < sz(BW[color]))
            color = !color;
        if (BW[color].empty())
        {
            ans.push_back(0);
        }
        else
        {
            for (int v = 0; v < N; ++v)
            {
                if (sz(adj2[v]) <= 1)
                {
                    usable = true;
                    ans.clear();
                    dfsAns(v, v);
                    if (usable && (finalAns.empty() || sz(finalAns) > sz(ans)))
                        swap(finalAns, ans);
                }
            }

            int n = sz(finalAns);
            for (int i = n - 1; i >= 0; --i)
                finalAns.push_back(finalAns[i]);
        }

        cout << sz(finalAns) << '\n';
        for (int x : finalAns)
            cout << x + 1 << " ";
        cout << '\n';
    }
    else
        cout << "NO\n";

    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Integer parameter [name=k] equals to 0, violates the range [1, 5]
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Integer parameter [name=k] equals to 0, violates the range [1, 5]
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Integer parameter [name=k] equals to 0, violates the range [1, 5]
2 Halted 0 ms 0 KB -