This commit is contained in:
Zengtudor 2024-10-13 12:14:36 +08:00
parent ef679ab3c8
commit 0de7fbaabb
2 changed files with 20 additions and 11 deletions

View File

@ -24,3 +24,16 @@ struct Status{
std::priority_queue<Status> q; std::priority_queue<Status> q;
``` ```
### [P1330](https://www.luogu.com.cn/problem/P1330)
#### BFS时注意初始化一开始的去重数组
```cpp
void bfs(){
for(ll i{1};i<=n;i++){
color_sum[1]=color_sum[2]=0;
if(vis[i])continue;
q.push(i);
set_color(i, 1);
vis[i]=true; // 注意初始化错误
while(!q.empty()){
```

View File

@ -1,14 +1,7 @@
//#define NDEBUG
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <queue> #include <queue>
#include <algorithm> #include <algorithm>
#define NV(v)#v<<" : "<<(v)
#ifdef NDEBUG
#define DEBUG(code)
#else
#define DEBUG(code){code}
#endif
using ll = long long; using ll = long long;
using std::endl; using std::endl;
@ -22,22 +15,24 @@ static std::vector<ll> next[max_n];
static bool vis[max_n]; static bool vis[max_n];
static std::queue<ll> q; static std::queue<ll> q;
static int colors[max_n]; static int colors[max_n];
static ll color_sum[3]; static ll color_sum[3], ans;
void set_color(const ll n,const int color)noexcept{ void set_color(const ll n,const int color)noexcept{
colors[n]=color; colors[n]=color;
color_sum[color]++; color_sum[color]++;
} }
void flush_exit()noexcept{ void flush_exit()noexcept{
os<<endl; os<<std::flush;
_Exit(0); _Exit(0);
} }
void bfs(){ void bfs(){
for(ll i{1};i<=n;i++){ for(ll i{1};i<=n;i++){
color_sum[1]=color_sum[2]=0;
if(vis[i])continue; if(vis[i])continue;
q.push(i); q.push(i);
set_color(i, 1); set_color(i, 1);
vis[i]=true; // 注意初始化错误
while(!q.empty()){ while(!q.empty()){
const auto front = q.front(); const auto front = q.front();
q.pop(); q.pop();
@ -45,7 +40,7 @@ void bfs(){
for(const auto i:next[front]){ for(const auto i:next[front]){
if(vis[i]){ if(vis[i]){
if(colors[i]==colors[front]){ if(colors[i]==colors[front]){
os<<"Impossible"; os<<"Impossible\n";
flush_exit(); flush_exit();
} }
continue; continue;
@ -55,6 +50,7 @@ void bfs(){
q.push(i); q.push(i);
} }
} }
ans+=std::min(color_sum[1],color_sum[2]);
} }
} }
@ -67,6 +63,6 @@ int main(){
} }
bfs(); bfs();
os<<std::min(color_sum[1],color_sum[2]); os<<ans<<'\n';
flush_exit(); flush_exit();
} }