Submission #922128

#TimeUsernameProblemLanguageResultExecution timeMemory
922128406Newspapers (CEOI21_newspapers)C++17
100 / 100
1 ms860 KiB
#include "bits/stdc++.h" #define int long long #define FOR(i, a, b) for (int i = (a); i < (b); i++) using namespace std; const long long INF = 1ll << 60; const int N = 1000 + 5; int h[N], pr[N]; vector<int> adj[N], V[N], order; bitset<N> leaf; void dfs(int v, int p) { for (auto u: adj[v]) if (u != p) { h[u] = h[v] + 1; pr[u] = v; dfs(u, v); } } signed main() { ios::sync_with_stdio(0), cin.tie(0); int n, m; cin >> n >> m; if (m >= n) { cout << "NO\n"; return 0; } if (n <= 2) { cout << "YES\n"; cout << n << '\n'; FOR(i, 0, n) cout << 1 << ' '; return 0; } FOR(i, 1, n) { int u, v; cin >> u >> v; u--, v--; adj[u].push_back(v); adj[v].push_back(u); } FOR(i, 0, n) leaf[i] = adj[i].size() == 1; FOR(i, 0, n) if (!leaf[i]) { vector<int> t; for (auto u: adj[i]) if (!leaf[u]) t.push_back(u); adj[i] = t; } //is caterpillar? FOR(i, 0, n) { int ch = 0; for (auto u: adj[i]) ch += adj[u].size() > 1; if (ch >= 3) { cout << "NO\n"; return 0; } } int U, V; FOR(i, 0, n) if (!leaf[i]) { memset(h, -0x3f, sizeof h); h[i] = 0; dfs(i, i); U = max_element(h, h + n) - h; memset(h, -0x3f, sizeof h); h[U] = 0; dfs(U, U); V = max_element(h, h + n) - h; break; } //U, V is diam. for (int t = V; t != U; t = pr[t]) { order.push_back(t); for (auto u: adj[t]) if (!leaf[u] && adj[u].size() == 1 && u != V && u != U) order.push_back(u), order.push_back(t); } order.push_back(U); cout << "YES\n"; cout << order.size() * 2 << '\n'; for (auto u: order) cout << u + 1 << ' '; reverse(order.begin(), order.end()); for (auto u: order) cout << u + 1 << ' '; cout << '\n'; return 0; } // this fills you with ....

Compilation message (stderr)

newspapers.cpp: In function 'int main()':
newspapers.cpp:69:63: warning: 'V' may be used uninitialized in this function [-Wmaybe-uninitialized]
   69 |       for (auto u: adj[t]) if (!leaf[u] && adj[u].size() == 1 && u != V && u != U)
      |                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...