#include <bits/stdc++.h>
using namespace std;
// BFS-based 2-coloring of the graph
bool canBeBipartite(int start, const vector<vector<int>>& graph,
vector<int>& color)
{
queue<int> q;
q.push(start);
color[start] = 1; // Assign color 1 (black) to the starting node
while (!q.empty())
{
int node = q.front();
q.pop();
for (int neighbor : graph[node])
{
if (color[neighbor] == 0)
{
// If the neighbor has not been colored, color it with the opposite color
color[neighbor] = -color[node];
q.push(neighbor);
}
else if (color[neighbor] == color[node])
{
// If the neighbor has the same color as the current node, it's a conflict
return false;
}
}
}
return true; // No conflicts found, the graph can be colored as a bipartite graph
}
int minControlsToDisconnect(int n, int m, const vector<pair<int, int>>& edges)
{
vector<vector<int>> graph(n +
1); // Use 1-based indexing for convenience with input
for (const auto& edge : edges)
{
graph[edge.first].push_back(edge.second);
graph[edge.second].push_back(edge.first);
}
vector<int> color(n + 1,
0); // 0 means not colored, 1 means black, -1 means white
for (int i = 1; i <= n; ++i)
{
if (color[i] == 0 && !canBeBipartite(i, graph, color))
{
// If we find a node that cannot be colored without conflicts,
// it means the graph is not bipartite and we cannot disconnect all edges by controlling some nodes
return -1;
}
}
// Count the number of nodes colored with color 1 (black)
int controls = count(color.begin() + 1, color.end(), 1);
// Since the graph is bipartite, controlling all nodes of one color will disconnect all edges
// (assuming we have a valid bipartite coloring, which we checked above)
return min(controls, n -
controls); // Return the smaller set of nodes to control
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<pair<int, int>> edges(m);
for (int i = 0; i < m; ++i)
{
cin >> edges[i].first >> edges[i].second;
}
int result = minControlsToDisconnect(n, m, edges);
if(result < 0)
{
cout << "Impossible";
}
else
{
cout << result;
}
return 0;
}
救命,测试点②③④⑤⑦全部答案错误了
稻花香里说丰年,听取 Wrong Answer 声一片(