This commit is contained in:
Zengtudor 2024-10-12 19:30:59 +08:00
commit c8e8f1f0cb
2 changed files with 101 additions and 50 deletions

View File

@ -1,9 +1,6 @@
//#define NDEBUG
#include <iostream>
#include <unistd.h>
#include <vector>
#include <bitset>
#include <array>
#include <queue>
#include <algorithm>
#define NV(v)#v<<" : "<<(v)
@ -14,74 +11,62 @@
#endif
using ll = long long;
static const ll max_n = 1e4+5;
using std::endl;
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];
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;
colors[i]=1;
DEBUG(
os<<"colors["<<i<<"]="<<1<<'\n';
)
color_sum[colors[i]]++;
vis[i]=true;
q.push({i,colors[i]});
q.push(i);
set_color(i, 1);
while(!q.empty()){
const auto front = q.front();
q.pop();
for(const auto i:map[front.now]){
DEBUG(
os<<"----------------------------------\n";
)
const Status next_status{i,((front.color&1)+1)};
DEBUG(
os<<NV(front.now)<<' '<<NV(front.color)<<'\n'<<NV(next_status.now)<<' '<<NV(next_status.color)<<"\n>---<\n";
)
if(vis[next_status.now]){
DEBUG(
os<<NV(next_status.now)<<'\n'<<NV(next_status.color)<<'\n'<<NV(colors[next_status.now])<<'\n';
)
if(next_status.color==colors[front.now]){
os<<"Impossible\n";
_exit(0);
for(const auto i:next[front]){
if(vis[i]){
if(colors[i]==colors[front]){
os<<"Impossible";
flush_exit();
}
continue;
}
DEBUG(
os<<"colors["<<next_status.now<<"]="<<next_status.color<<'\n';
)
colors[next_status.now]=next_status.color;
color_sum[colors[next_status.now]]++;
vis[next_status.now]=true;
q.push(next_status);
// next_loop:;
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>>u>>v;
map[u].push_back(v);
map[v].push_back(u);
is>>a>>b;
next[a].push_back(b);
next[b].push_back(a);
}
bfs();
os<<std::min(color_sum[1],color_sum[2])<<'\n';
os<<std::min(color_sum[1],color_sum[2]);
flush_exit();
}

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';
}