原题目见洛谷(https://www.luogu.org/problemnew/show/P1330)
这道题如果仔细想想的话其实河蟹的分布就是对这张图进行黑白染色,且相邻的两点要染成不同的颜色,如果方案可行那么染色的两种方案中,颜色少的就是所需要的,基本思路的话就是这样
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define R register
#define maxm 200000
#define maxn 20000
using namespace std;
struct edge{
int t,nexty;
}edge[maxm];
int head[maxn],cnt=0;
bool used[maxn]={0};
int col[maxn]={0},sum[2];
inline int read(){
int num=0,f=1;
char ch=getchar();
while(ch<'0' || ch>'9'){
if(ch=='-'){
f=-1;
}
ch=getchar();
}
while(ch>='0' && ch<='9'){
num=num*10+ch-'0',ch=getchar();
}
return num*f;
}
inline void out(int x){
if(x>=10){
out(x/10);
}
putchar(x%10+'0');
}
inline void add(int a,int b){
++cnt,edge[cnt].t=b,edge[cnt].nexty=head[a],head[a]=cnt;
}
inline bool dfs(int node,int color){
if(used[node]){
if(col[node]==color){
return true;
}
return false;
}
used[node]=true,++sum[col[node]=color];
bool judge=true;
for(R int i=head[node];i!=0 && judge;i=edge[i].nexty){
judge=judge && dfs(edge[i].t,1-color);
}
return judge;
}
inline int _min(int a,int b){
return a>b?b:a;
}
int main(){
int n=read(),m=read();
int a,b;
while(m--){
a=read(),b=read(),add(a,b),add(b,a);
}
int ans=0;
for(R int i=1;i<=n;++i){
if(used[i]){
continue;
}
memset(sum,0,sizeof(sum));
if(!dfs(i,0)){
printf("Impossible");
return 0;
}
ans+=_min(sum[0],sum[1]);
}
out(ans);
return 0;
}
代码跑了12ms,但是居然还有跑0ms!!!,不过还很快吧qwq