AtCoder-agc071_c.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. struct Edge {
  4. int to;
  5. int id;
  6. };
  7. int main() {
  8. ios::sync_with_stdio(false);
  9. cin.tie(nullptr);
  10. int tests;
  11. cin >> tests;
  12. while (tests--) {
  13. int n, m;
  14. cin >> n >> m;
  15. vector<pair<int, int>> ends(m);
  16. vector<vector<Edge>> graph(n);
  17. for (int id = 0; id < m; ++id) {
  18. int u, v;
  19. cin >> u >> v;
  20. --u;
  21. --v;
  22. ends[id] = {u, v};
  23. graph[u].push_back({v, id});
  24. graph[v].push_back({u, id});
  25. }
  26. bool bipartite = true;
  27. vector<int> color(n, -1);
  28. queue<int> bfs;
  29. color[0] = 0;
  30. bfs.push(0);
  31. while (!bfs.empty()) {
  32. int u = bfs.front();
  33. bfs.pop();
  34. for (Edge e : graph[u]) {
  35. if (color[e.to] == -1) {
  36. color[e.to] = color[u] ^ 1;
  37. bfs.push(e.to);
  38. } else if (color[e.to] == color[u]) {
  39. bipartite = false;
  40. }
  41. }
  42. }
  43. vector<int> dfn(n, 0), low(n, 0), edge_stack;
  44. vector<vector<int>> groups(n);
  45. vector<int> parent_vertex(n, -1), parent_edge(n, -1), cursor(n, 0);
  46. vector<int> incident_count(n, 0), touched;
  47. int timer = 1;
  48. dfn[0] = low[0] = 1;
  49. vector<int> dfs_stack = {0};
  50. while (!dfs_stack.empty()) {
  51. int u = dfs_stack.back();
  52. if (cursor[u] < static_cast<int>(graph[u].size())) {
  53. Edge e = graph[u][cursor[u]++];
  54. if (e.id == parent_edge[u]) continue;
  55. if (!dfn[e.to]) {
  56. parent_vertex[e.to] = u;
  57. parent_edge[e.to] = e.id;
  58. edge_stack.push_back(e.id);
  59. dfn[e.to] = low[e.to] = ++timer;
  60. dfs_stack.push_back(e.to);
  61. } else if (dfn[e.to] < dfn[u]) {
  62. edge_stack.push_back(e.id);
  63. low[u] = min(low[u], dfn[e.to]);
  64. }
  65. continue;
  66. }
  67. dfs_stack.pop_back();
  68. int parent = parent_vertex[u];
  69. if (parent == -1) continue;
  70. low[parent] = min(low[parent], low[u]);
  71. if (low[u] < dfn[parent]) continue;
  72. touched.clear();
  73. while (true) {
  74. int id = edge_stack.back();
  75. edge_stack.pop_back();
  76. for (int v : {ends[id].first, ends[id].second}) {
  77. if (incident_count[v] == 0) touched.push_back(v);
  78. ++incident_count[v];
  79. }
  80. if (id == parent_edge[u]) break;
  81. }
  82. for (int v : touched) {
  83. groups[v].push_back(incident_count[v]);
  84. incident_count[v] = 0;
  85. }
  86. }
  87. bool every_degree_is_formable = true;
  88. for (auto &sizes : groups) {
  89. sort(sizes.begin(), sizes.end());
  90. int covered = 0;
  91. for (int size : sizes) {
  92. if (size > covered + 1) {
  93. every_degree_is_formable = false;
  94. break;
  95. }
  96. covered += size;
  97. }
  98. }
  99. cout << (!(bipartite && every_degree_is_formable) ? "Yes\n" : "No\n");
  100. }
  101. return 0;
  102. }