This commit is contained in:
Zengtudor 2024-10-12 15:00:59 +08:00
parent ab6136dcf9
commit cdf8358f3c

View File

@ -1,3 +1,65 @@
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using ll = long long;
using std::endl;
static auto &is = std::cin;
static auto &os = std::cout;
static const ll max_n = 1e4+5;
static ll n, m, a, b;
static std::vector<ll> next[max_n];
static bool vis[max_n];
static std::queue<ll> q;
static int colors[max_n];
static ll color_sum[3];
void set_color(const ll n,const int color)noexcept{
colors[n]=color;
color_sum[color]++;
}
void flush_exit()noexcept{
os<<endl;
_Exit(0);
}
void bfs(){
for(ll i{1};i<=n;i++){
if(vis[i])continue;
q.push(i);
set_color(i, 1);
while(!q.empty()){
const auto front = q.front();
q.pop();
for(const auto i:next[front]){
if(vis[i]){
if(colors[i]==colors[front]){
os<<"Impossible";
flush_exit();
}
continue;
}
set_color(i,(colors[front]&1)+1);
vis[i]=true;
q.push(i);
}
}
}
}
int main(){
is>>n>>m;
for(ll i{0};i<m;i++){
is>>a>>b;
next[a].push_back(b);
next[b].push_back(a);
}
bfs();
os<<std::min(color_sum[1],color_sum[2]);
flush_exit();
}