Submission #518619

#TimeUsernameProblemLanguageResultExecution timeMemory
518619prvocisloNewspapers (CEOI21_newspapers)C++17
100 / 100
1 ms460 KiB
#include <algorithm> #include <iostream> #include <string> #include <vector> #include <set> #include <map> #include <queue> typedef long long ll; using namespace std; const int maxn = 1e3 + 5; vector<vector<int> > g; vector<int> d, p; int n, m; void ready() { d.assign(n, -1), p.assign(n, -1); } void dfs(int u) { for (int v : g[u]) { if (d[v] == -1) { d[v] = d[u] + 1; p[v] = u; dfs(v); } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; if (m >= n) { cout << "NO\n"; return 0; } if (n <= 2) { cout << "YES\n" << n << "\n"; for (int i = 0; i < n; i++) cout << "1 "; cout << "\n"; return 0; } g.assign(n, {}); for (int i = 0, a, b; i < n - 1; i++) { cin >> a >> b; a--, b--; g[a].push_back(b); g[b].push_back(a); } ready(); d[0] = 0; dfs(0); int e1 = max_element(d.begin(), d.end()) - d.begin(); ready(); d[e1] = 0; dfs(e1); int e2 = max_element(d.begin(), d.end()) - d.begin(); vector<int> di; for (int i = e2; i != -1; i = p[i]) di.push_back(i); ready(); for (int i : di) d[i] = 0; for (int i : di) dfs(i); if (*max_element(d.begin(), d.end()) >= 3) { cout << "NO\n"; return 0; } cout << "YES\n"; vector<int> ans; for (int i = 1; i + 1 < di.size(); i++) { ans.push_back(di[i]); for (int j : g[di[i]]) if (d[j] && g[j].size() > 1) { ans.push_back(j); ans.push_back(di[i]); } } int k = ans.size(); for (int i = 0; i < k; i++) ans.push_back(ans[k - i - 1]); cout << ans.size() << "\n"; for (int i = 0; i < ans.size(); i++) cout << ans[i] + 1 << " \n"[i == ans.size() - 1]; return 0; }

Compilation message (stderr)

newspapers.cpp: In function 'int main()':
newspapers.cpp:76:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |     for (int i = 1; i + 1 < di.size(); i++)
      |                     ~~~~~~^~~~~~~~~~~
newspapers.cpp:88:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |     for (int i = 0; i < ans.size(); i++) cout << ans[i] + 1 << " \n"[i == ans.size() - 1];
      |                     ~~^~~~~~~~~~~~
newspapers.cpp:88:72: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |     for (int i = 0; i < ans.size(); i++) cout << ans[i] + 1 << " \n"[i == ans.size() - 1];
      |                                                                      ~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...