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