Submission #863795

#TimeUsernameProblemLanguageResultExecution timeMemory
863795Trisanu_DasNewspapers (CEOI21_newspapers)C++17
100 / 100
2 ms604 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() { cin >> 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; cin >> 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); }

Compilation message (stderr)

newspapers.cpp: In function 'int main()':
newspapers.cpp:37:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |     for (int i = 1; i + 1 < path.size(); i++) {
      |                     ~~~~~~^~~~~~~~~~~~~
newspapers.cpp:41:19: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wformat=]
   41 |     printf("YES\n%d\n", 2 * ans.size());
      |                  ~^     ~~~~~~~~~~~~~~
      |                   |       |
      |                   int     std::vector<int>::size_type {aka long unsigned int}
      |                  %ld
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...