Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- int main()
- {
- int64_t nodes_count, edges_count;
- cin >> nodes_count >> edges_count;
- if (nodes_count == 1)
- {
- cout << -1 << '\n';
- return 0;
- }
- vector< vector<bool> > is_edge(1 + nodes_count, vector<bool>(1 + nodes_count, false));
- for (int64_t i = 0; i < edges_count; ++i)
- {
- int64_t node1, node2;
- cin >> node1 >> node2;
- is_edge[node1][node2] = true;
- is_edge[node2][node1] = true;
- }
- vector<bool> is_adjacent_for_first_node = is_edge[1];
- vector< bool > full_subgraph_containing_first_node(1 + nodes_count, true);
- //формирую полный подграф содержащий первую вершину
- for (int64_t node1 = 2; node1 < is_adjacent_for_first_node.size(); ++node1)
- {
- if (!is_adjacent_for_first_node[node1])
- {
- full_subgraph_containing_first_node[node1] = false;
- continue;
- }
- for (int64_t node2 = node1 + 1; node2 <= nodes_count; ++node2)
- {
- if (!is_adjacent_for_first_node[node2])
- continue;
- if (!is_edge[node1][node2])
- full_subgraph_containing_first_node[node2] = false;
- }
- }
- vector< bool > nodes(1 + nodes_count, false);
- for (int64_t i = 2; i < full_subgraph_containing_first_node.size(); ++i)
- {
- if (!full_subgraph_containing_first_node[i])
- {
- nodes[i] = true;
- }
- }
- //теперь в nodes - все вершины, которые не вошли в полный подграф, содержащий вершину 1
- //нужно проверить образуют ли эти вершины полный подграф. Если - да, то все ок, если нет - возвращаем -1.
- for (int64_t node1 = 2; node1 < nodes.size(); ++node1)
- {
- if (!nodes[node1])
- continue;
- for (int64_t node2 = node1 + 1; node2 < nodes.size(); ++node2)
- {
- if (!nodes[node2])
- continue;
- if (!is_edge[node1][node2])
- {
- cout << -1 << '\n';
- return 0;
- }
- }
- }
- //теперь у нас есть два полных подграфа.
- int64_t sum1 = 1;
- int64_t sum2 = 0;
- for (int64_t i = 2; i <= nodes_count; ++i)
- {
- if (full_subgraph_containing_first_node[i])
- sum1++;
- if (nodes[i])
- sum2++;
- }
- if (sum2 == 0)
- {
- cout << nodes_count / 2 << '\n';
- for (int64_t i = 2; i <= nodes_count; i += 2)
- {
- cout << i << ' ';
- }
- cout << '\n';
- for (int64_t i = 1; i <= nodes_count; i += 2)
- {
- cout << i << ' ';
- }
- }
- else
- {
- cout << sum1 << '\n' << 1 << ' ';
- for (int64_t i = 2; i < full_subgraph_containing_first_node.size(); ++i)
- {
- if (full_subgraph_containing_first_node[i])
- {
- cout << i << ' ';
- }
- }
- cout << '\n';
- //cout << sum2 << '\n';
- for (int64_t i = 2; i < nodes.size(); ++i)
- {
- if (nodes[i])
- {
- cout << i << ' ';
- }
- }
- }
- return 0;
- }
- /*
- test1
- 3 1
- 1 2
- answer
- 2
- 1 2
- 3
- test2
- 3 0
- answer
- -1
- test3
- 5 10
- 1 2
- 1 3
- 1 4
- 1 5
- 2 3
- 2 4
- 2 5
- 3 4
- 3 5
- 4 5
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement