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

@ -23,4 +23,17 @@ struct Status{
};
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 <vector>
#include <queue>
#include <algorithm>
#define NV(v)#v<<" : "<<(v)
#ifdef NDEBUG
#define DEBUG(code)
#else
#define DEBUG(code){code}
#endif
using ll = long long;
using std::endl;
@ -22,22 +15,24 @@ 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];
static ll color_sum[3], ans;
void set_color(const ll n,const int color)noexcept{
colors[n]=color;
color_sum[color]++;
}
void flush_exit()noexcept{
os<<endl;
os<<std::flush;
_Exit(0);
}
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()){
const auto front = q.front();
q.pop();
@ -45,7 +40,7 @@ void bfs(){
for(const auto i:next[front]){
if(vis[i]){
if(colors[i]==colors[front]){
os<<"Impossible";
os<<"Impossible\n";
flush_exit();
}
continue;
@ -55,6 +50,7 @@ void bfs(){
q.push(i);
}
}
ans+=std::min(color_sum[1],color_sum[2]);
}
}
@ -67,6 +63,6 @@ int main(){
}
bfs();
os<<std::min(color_sum[1],color_sum[2]);
os<<ans<<'\n';
flush_exit();
}