1
0

AtCoder-agc032_c.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. ios::sync_with_stdio(false);
  5. cin.tie(nullptr);
  6. int n, m;
  7. cin >> n >> m;
  8. vector<vector<int>> graph(n);
  9. vector<int> degree(n, 0);
  10. for (int i = 0; i < m; ++i) {
  11. int u, v;
  12. cin >> u >> v;
  13. --u;
  14. --v;
  15. graph[u].push_back(v);
  16. graph[v].push_back(u);
  17. ++degree[u];
  18. ++degree[v];
  19. }
  20. for (int d : degree) {
  21. if (d & 1) {
  22. cout << "No\n";
  23. return 0;
  24. }
  25. }
  26. for (int d : degree) {
  27. if (d >= 6) {
  28. cout << "Yes\n";
  29. return 0;
  30. }
  31. }
  32. vector<int> four;
  33. for (int v = 0; v < n; ++v) {
  34. if (degree[v] == 4) four.push_back(v);
  35. }
  36. if (four.size() >= 3) {
  37. cout << "Yes\n";
  38. return 0;
  39. }
  40. if (four.size() <= 1) {
  41. cout << "No\n";
  42. return 0;
  43. }
  44. const int start = four[0], forbidden = four[1];
  45. vector<char> visited(n, false);
  46. stack<pair<int, int>> work;
  47. for (int to : graph[start]) {
  48. if (to != forbidden) work.push({to, start});
  49. }
  50. visited[start] = true;
  51. bool found_cycle_avoiding_other = false;
  52. while (!work.empty()) {
  53. auto [v, parent] = work.top();
  54. work.pop();
  55. if (v == start) {
  56. found_cycle_avoiding_other = true;
  57. break;
  58. }
  59. if (v == forbidden || visited[v]) continue;
  60. visited[v] = true;
  61. for (int to : graph[v]) {
  62. if (to != parent) work.push({to, v});
  63. }
  64. }
  65. cout << (found_cycle_avoiding_other ? "Yes\n" : "No\n");
  66. return 0;
  67. }