This commit is contained in:
Zengtudor 2024-10-12 14:42:48 +08:00
parent 79d6da2e95
commit ab6136dcf9
2 changed files with 67 additions and 64 deletions

View File

@ -1,66 +1,3 @@
#include <iostream>
#include <unistd.h>
#include <vector>
#include <bitset>
#include <array>
#include <queue>
#include <algorithm>
using ll = long long;
static const ll max_n = 1e4+5;
static auto &is = std::cin;
static auto &os = std::cout;
static ll n,m,u,v;
std::vector<ll> map[max_n];
std::bitset<max_n> vis;
std::array<unsigned short,max_n> colors;
struct Status{
ll now;
int color;
};
std::queue<Status> q;
ll color_sum[3];
void bfs(){
for(ll i{1};i<=n;i++){
if(vis[i])continue;
colors[i]=1;
color_sum[colors[i]]++;
vis[i]=true;
q.push({i,colors[i]});
while(!q.empty()){
const auto front = q.front();
q.pop();
for(const auto i:map[front.now]){
const Status next_status{i,((front.color&1)+1)};
if(vis[next_status.now]){
if(next_status.color!=colors[next_status.color]){
os<<"Impossible\n";
_exit(0);
}
continue;
}
colors[next_status.now]=next_status.color;
color_sum[colors[next_status.now]]++;
vis[next_status.now]=true;
q.push(next_status);
}
}
}
return;
}
int main(){
is>>n>>m;
for(ll i{0};i<m;i++){
is>>u>>v;
map[u].push_back(v);
map[v].push_back(u);
}
bfs();
os<<std::min(color_sum[1],color_sum[2])<<'\n';
}

View File

@ -0,0 +1,66 @@
#include <iostream>
#include <unistd.h>
#include <vector>
#include <bitset>
#include <array>
#include <queue>
#include <algorithm>
using ll = long long;
static const ll max_n = 1e4+5;
static auto &is = std::cin;
static auto &os = std::cout;
static ll n,m,u,v;
std::vector<ll> map[max_n];
std::bitset<max_n> vis;
std::array<unsigned short,max_n> colors;
struct Status{
ll now;
int color;
};
std::queue<Status> q;
ll color_sum[3];
void bfs(){
for(ll i{1};i<=n;i++){
if(vis[i])continue;
colors[i]=1;
color_sum[colors[i]]++;
vis[i]=true;
q.push({i,colors[i]});
while(!q.empty()){
const auto front = q.front();
q.pop();
for(const auto i:map[front.now]){
const Status next_status{i,((front.color&1)+1)};
if(vis[next_status.now]){
if(next_status.color!=colors[next_status.color]){
os<<"Impossible\n";
_exit(0);
}
continue;
}
colors[next_status.now]=next_status.color;
color_sum[colors[next_status.now]]++;
vis[next_status.now]=true;
q.push(next_status);
}
}
}
return;
}
int main(){
is>>n>>m;
for(ll i{0};i<m;i++){
is>>u>>v;
map[u].push_back(v);
map[v].push_back(u);
}
bfs();
os<<std::min(color_sum[1],color_sum[2])<<'\n';
}