제출 #1189337

#제출 시각아이디문제언어결과실행 시간메모리
1189337steveonalexNewspapers (CEOI21_newspapers)C++20
100 / 100
18 ms584 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(abs(a), abs(b));}
ll lcm(ll a, ll b){return abs(a) / gcd(a, b) * abs(b);}
 
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ull mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
 
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
    double cur = rngesus(0, MASK(60) - 1);
    cur /= MASK(60) - 1;
    return l + cur * (r - l);
}
 
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }
 
template <class T>
    void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }
 
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }
 
int n, m;

const int N = 1010;
vector<int> graph[N];
 
namespace Sub3{
    int depth[N];
    int color[N];
 
    bool find_depth(int u, int p){
        depth[u] = 1;
        int cnt = 0;
        bool ans = true;
        for(int v: graph[u]) if (v != p){
            color[v] = 1 - color[u];
            ans = ans && find_depth(v, u);
            maximize(depth[u], depth[v] + 1);
            if (depth[v] > 2) cnt++;
        }
        if (cnt >= 2) return false;
        return ans;
    }
 
    void dfs(int u, int p, vector<int> &nooky){
        vector<int> child;
        for(int v: graph[u]) 
            if (v != p) child.push_back(v);
 
        sort(ALL(child), [](int x, int y){return depth[x] > depth[y];});
 
        if (depth[u] == 1) {
            if (color[u] == 1) nooky.push_back(u);
            return;
        }
        dfs(child[0], u, nooky);
        nooky.push_back(u);
        for(int i = 1; i < (int) child.size(); ++i){
            if (depth[child[i]] >= 2){
                nooky.push_back(child[i]);
                nooky.push_back(u);
            }
        }
    }
 
    void solve(){
        bool check = true;
        int root = -1;
        for(int i = 1; i <= n; ++i){
            bool rootable = find_depth(i, 0);
            if (rootable && graph[i].size() == 1) root = i;
 
            int cnt = 0;
            for(int j: graph[i]) if (depth[j] > 2) cnt++;
 
            if (cnt > 2)
                check = false;
        }
 
        if (!check){
            cout << "NO\n";
            exit(0);
        }
        if (root == -1) assert(false);
        cout << "YES\n";
 

        vector<int> nooky[2];
        nooky[0] = nooky[1] = vector<int>(n * 5, 69);

        for(int x = 0; x <= 1; ++x){
            color[root] = x;
            find_depth(root, 0);
            for(int i = 1; i <= n; ++i) if (graph[i].size() == 1){
                if (find_depth(i, 0)) {
                    vector<int> mongey;
                    dfs(i, 0, mongey);

                    if (nooky[x].size() > mongey.size()){
                        nooky[x] = mongey;
                    }
                }
            }
        }

        nooky[0].pop_back(); nooky[1].pop_back();
        vector<int> ans(n * 5, 69);
        for(int x = 0; x <= 1; ++x) for(int y = 0; y <= 1; ++y){
            vector<int> cur = nooky[x];
            if ((int)cur.size() % 2 == (x ^ y)) cur.push_back(1);
            for(int j: nooky[y]) cur.push_back(j);

            if (cur.size() < ans.size()) ans = cur;
        }
 
        cout << ans.size() << "\n";
        printArr(ans);
 
        exit(0);
    }
}
 
void solve(){
    cin >> n >> m;
 
    if (m != n - 1) {
        cout << "NO\n";
        return;
    }

    if (n == 1){
        cout << "YES\n1\n1\n";
        return;
    }
    else if (n == 2){
        cout << "YES\n2\n1 1\n";
        return;
    }
 
    for(int i = 0; i < m; ++i){
        int u, v; cin>> u >> v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    Sub3::solve();
 
}
int main(void){
    ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
 
    clock_t start = clock();
 
    solve();
 
    cerr << "Time elapsed: " << clock() - start << "ms!\n";
 
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...