답안 #531558

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
531558 2022-03-01T04:09:25 Z eecs Newspapers (CEOI21_newspapers) C++17
4 / 100
1 ms 332 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 - 1) puts("NO"), exit(0);
    if (n == 1) puts("YES\n1\n1"), exit(0);
    if (n == 2) puts("YES\n2\n1 1"), 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:42:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     for (int i = 1; i + 1 < path.size(); i++) {
      |                     ~~~~~~^~~~~~~~~~~~~
newspapers.cpp:50:19: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wformat=]
   50 |     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:15:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |         scanf("%d %d", &u, &v);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Partially correct 0 ms 204 KB Failed to provide a successful strategy.
4 Partially correct 0 ms 204 KB Failed to provide a successful strategy.
5 Correct 0 ms 204 KB Output is correct
6 Partially correct 1 ms 204 KB Failed to provide a successful strategy.
7 Correct 1 ms 204 KB Output is correct
8 Incorrect 1 ms 204 KB Integer 0 violates the range [1, 7]
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Partially correct 0 ms 204 KB Failed to provide a successful strategy.
5 Partially correct 1 ms 204 KB Failed to provide a successful strategy.
6 Partially correct 1 ms 204 KB Failed to provide a successful strategy.
7 Partially correct 0 ms 204 KB Failed to provide a successful strategy.
8 Partially correct 1 ms 204 KB Failed to provide a successful strategy.
9 Partially correct 0 ms 204 KB Failed to provide a successful strategy.
10 Partially correct 0 ms 204 KB Failed to provide a successful strategy.
11 Partially correct 1 ms 332 KB Failed to provide a successful strategy.
12 Partially correct 1 ms 332 KB Failed to provide a successful strategy.
13 Partially correct 1 ms 332 KB Failed to provide a successful strategy.
14 Partially correct 1 ms 332 KB Failed to provide a successful strategy.
15 Partially correct 1 ms 332 KB Failed to provide a successful strategy.
16 Partially correct 1 ms 332 KB Failed to provide a successful strategy.
17 Partially correct 1 ms 332 KB Failed to provide a successful strategy.
18 Partially correct 1 ms 332 KB Failed to provide a successful strategy.
19 Partially correct 1 ms 332 KB Failed to provide a successful strategy.
20 Partially correct 1 ms 332 KB Failed to provide a successful strategy.
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Partially correct 0 ms 204 KB Failed to provide a successful strategy.
4 Partially correct 0 ms 204 KB Failed to provide a successful strategy.
5 Correct 0 ms 204 KB Output is correct
6 Partially correct 1 ms 204 KB Failed to provide a successful strategy.
7 Correct 1 ms 204 KB Output is correct
8 Incorrect 1 ms 204 KB Integer 0 violates the range [1, 7]
9 Halted 0 ms 0 KB -