답안 #752668

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
752668 2023-06-03T11:57:42 Z 이동현(#9968) Newspapers (CEOI21_newspapers) C++17
0 / 100
1 ms 212 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
#define mi(x, y) (x = min(x, y))
#define ma(x, y) (x = max(x, y))
using namespace std;

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, m;
    cin >> n >> m;
    vector<vector<int>> way(n);
    for(int i = 0; i < m; ++i){
        int x, y;
        cin >> x >> y;
        --x, --y;
        way[x].push_back(y);
        way[y].push_back(x);
    }

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

    auto getfar = [&](auto&&self, int x, int pr)->pair<int, int>{
        pair<int, int> rv = {0, x};
        for(auto&nxt:way[x]){
            if(nxt != pr){
                rv = max(rv, self(self, nxt, x));
            }
        }
        return {rv.first + 1, rv.second};
    };

    auto rv = getfar(getfar, 0, -1);
    auto rv2 = getfar(getfar, rv.second, -1);

    int x = rv.second, y = rv2.second;

    vector<int> disx(n), disy(n);
    auto dodis = [&](auto&&self, int x, int pr, vector<int>&vc)->void{
        for(auto&nxt:way[x]){
            if(nxt == pr){
                continue;
            }
            vc[nxt] = vc[x] + 1;
            self(self, nxt, x, vc);
        }
    };
    dodis(dodis, x, -1, disx);
    dodis(dodis, y, -1, disy);

    vector<int> dist(n, (int)1e9);
    queue<int> que;
    for(int i = 0; i < n; ++i){
        if(disx[i] + disy[i] == disx[y]){
            que.push(i);
            dist[i] = 0;
        }
    }

    while(!que.empty()){
        int now = que.front();
        que.pop();
        for(auto&nxt:way[now]){
            if(dist[nxt] == (int)1e9){
                dist[nxt] = dist[now] + 1;
                que.push(nxt);
            }
        }
    }

    for(int i = 0; i < n; ++i){
        if(dist[i] > 2){
            cout << "NO\n";

            return 0;
        }
    }

    cout << "YES\n";
    vector<int> nt(n);
    vector<vector<int>> br(n);
    for(int i = 0; i < n; ++i){
        if(dist[i] == 2){
            nt[way[i][0]] = 1;
        }
    }
    for(int i = 0; i < n; ++i){
        if(nt[i]){
            for(auto&nxt:way[i]){
                if(!dist[nxt]){
                    br[nxt].push_back(i);
                    break;
                }
            }
        }
    }

    vector<int> line;
    for(int i = 0; i < n; ++i){
        if(disx[i] + disy[i] == disx[y]){
            line.push_back(i);
        }
    }
    sort(line.begin(), line.end(), [&](int&x, int&y){return disx[x] < disx[y];});
    vector<int> ans;
    for(int rp = 0; rp < 2; ++rp){
        for(int i = 1; i < (int)line.size() - 1; ++i){
            ans.push_back(line[i]);
            for(int j = 0; j < (int)br[line[i]].size(); ++j){
                ans.push_back(br[line[i]][j]);
                ans.push_back(line[i]);
            }
        }
        reverse(line.begin(), line.end());
    }

    cout << (int)ans.size() << '\n';
    for(auto&i:ans){
        cout << i + 1 << ' ';
    }
    cout << '\n';

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Integer parameter [name=k] equals to 0, violates the range [1, 5]
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Integer parameter [name=k] equals to 0, violates the range [1, 5]
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Integer parameter [name=k] equals to 0, violates the range [1, 5]
2 Halted 0 ms 0 KB -