답안 #977560

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
977560 2024-05-08T06:56:19 Z Circling Newspapers (CEOI21_newspapers) C++17
0 / 100
1 ms 348 KB
/*The British Royal Family and a small cadre of English Fabian Socialists, in
conjunction with the Rockefellers and the Rothchilds, are engaged in a
conspiracy to greatly reduce the population of the human race in order to head
off a Malthusian catastrophe, a catastrophe that could easily be avoided by
simply building a massive amount of nuclear power plants and a number of massive
superhighways and bridges to connect all of the world's continents. But doing
that would cut into the conspiracy's profits. So the British Royal Family
invented environmentalism and neoliberalism in order to hide the truth. And in
order to further reduce the population, the British Royal Family is also the
world's foremost drug trafficking conspiracy. And it uses its control of the IMF
to push austerity in order to kill as many people in the global south as
possible.
And also Henry Kissinger is a gay KGB agent. */
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;


int main(){
    int n, m, u, v, nlc, last, last2;
    cin >> n >> m;
    if (m != n - 1){
        cout << "NO";
        return 0;
    }
    if (n == 1){
        cout << "YES\n1\n1";
        return 0;
    }
    if (n == 2){
        cout << "YES\n2\n1 1";
        return 0;
    }
    vector<vector<int>> graph;
    graph.resize(n + 1);
    while (m--){
        cin >> u >> v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    for (int i = 1; i <= n; i++){
        nlc = 0;
        for (int j: graph[i]) if (graph[j].size() > 1) nlc++;
        if (nlc > 2){
            cout << "NO";
            return 0;
        }
    }
    vector<bool> seen;
    for (int i = 0; i <= n; i++) seen.push_back(false);
    queue<int> q;
    q.push(1);
    while (!q.empty()){
        last = q.front();
        q.pop();
        seen[last] = true;
        for (int i: graph[last]) if (!seen[i]) q.push(i);
    }
    vector<int> prev, diameter;
    for (int i = 0; i <= n; i++) prev.push_back(0);
    q.push(last);
    while (!q.empty()){
        last2 = q.front();
        q.pop();
        for (int i: graph[last2]){
            if (i != prev[last2]){
                prev[i] = last2;
                q.push(i);
            }
        }
    }
    for (int i = last2; i != last; i = prev[i]) diameter.push_back(i);
    cout << "YES\n" << 2 * diameter.size() - 2 << '\n';
    //for (int i = 1; i < diameter.size(); i++) cout << diameter[i] << ' ';
    //for (int i = diameter.size() - 1; i > 0; i--) cout << diameter[i] << ' ';
}

Compilation message

newspapers.cpp: In function 'int main()':
newspapers.cpp:78:27: warning: 'last2' may be used uninitialized in this function [-Wmaybe-uninitialized]
   78 |     for (int i = last2; i != last; i = prev[i]) diameter.push_back(i);
      |                         ~~^~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Incorrect 1 ms 348 KB Unexpected end of file - int32 expected
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Unexpected end of file - int32 expected
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Incorrect 1 ms 348 KB Unexpected end of file - int32 expected
4 Halted 0 ms 0 KB -