답안 #531554

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
531554 2022-03-01T04:02:38 Z eecs Newspapers (CEOI21_newspapers) C++17
0 / 100
1 ms 320 KB
#include <bits/stdc++.h>
using namespace std;

const int maxn = 1010;
int n, m, d[maxn], pre[maxn];
vector<int> path, ans, G[maxn];

int main() {
    scanf("%d %d", &n, &m);
    if (m >= n) puts("NO"), exit(0);
    while (m--) {
        int u, v;
        scanf("%d %d", &u, &v);
        G[u].push_back(v), G[v].push_back(u);
    }
    auto bfs = [&]() {
        queue<int> Q;
        for (int i = 1; i <= n; i++) {
            if (!d[i]) Q.push(i);
        }
        while (!Q.empty()) {
            int u = Q.front(); Q.pop();
            for (int v : G[u]) if (!~d[v]) {
                Q.push(v), d[v] = d[u] + 1, pre[v] = u;
            }
        }
    };
    memset(d, -1, sizeof(d));
    d[1] = 0, bfs();
    int s = max_element(d + 1, d + n + 1) - d;
    memset(d, -1, sizeof(d));
    d[s] = 0, bfs();
    int t = max_element(d + 1, d + n + 1) - d;
    for (int i = t; i ^ s; i = pre[i]) path.push_back(i);
    path.push_back(s);
    memset(d, -1, sizeof(d));
    for (int i : path) d[i] = 0;
    bfs();
    if (*max_element(d + 1, d + n + 1) > 2) puts("NO"), exit(0);
    for (int i = 1; i + 1 < path.size(); i++) {
        ans.push_back(path[i]), ans.push_back(path[i]);
        for (int j : G[i]) if (d[j] == 1) {
            bool flag = 0;
            for (int k : G[j]) flag |= d[k] == 2;
            if (flag) ans.push_back(path[j]), ans.push_back(path[i]);
        }
    }
    printf("YES\n%d\n", ans.size());
    for (int x : ans) printf("%d ", x);
    return 0;
}

Compilation message

newspapers.cpp: In function 'int main()':
newspapers.cpp:40:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |     for (int i = 1; i + 1 < path.size(); i++) {
      |                     ~~~~~~^~~~~~~~~~~~~
newspapers.cpp:48:19: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wformat=]
   48 |     printf("YES\n%d\n", ans.size());
      |                  ~^     ~~~~~~~~~~
      |                   |             |
      |                   int           std::vector<int>::size_type {aka long unsigned int}
      |                  %ld
newspapers.cpp:9:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
newspapers.cpp:13:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |         scanf("%d %d", &u, &v);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 204 KB Integer parameter [name=k] equals to 0, violates the range [1, 5]
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 320 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 204 KB Integer parameter [name=k] equals to 0, violates the range [1, 5]
2 Halted 0 ms 0 KB -