Submission #533957

#TimeUsernameProblemLanguageResultExecution timeMemory
533957AaronJNewspapers (CEOI21_newspapers)C++98
100 / 100
2 ms332 KiB
    #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);
        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++) {
            for (int j : G[path[i]]) if (d[j] && G[j].size() > 1) {
                ans.push_back(path[i]), ans.push_back(j);
            }
            ans.push_back(path[i]);
        }
        printf("YES\n%d\n", 2 * ans.size());
        for (int x : ans) printf("%d ", x);
        reverse(ans.begin(), ans.end());
        for (int x : ans) printf("%d ", x);
        return 0;
    }

Compilation message (stderr)

newspapers.cpp: In function 'int main()':
newspapers.cpp:42:31: 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:48:23: 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", 2 * ans.size());
      |                      ~^     ~~~~~~~~~~~~~~
      |                       |       |
      |                       int     std::vector<int>::size_type {aka long unsigned int}
      |                      %ld
newspapers.cpp:9:14: 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:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |             scanf("%d %d", &u, &v);
      |             ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...