0分求调
查看原帖
0分求调
921450
Szy0720楼主2024/7/24 10:45
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
int n,m,col[N],white,black;
vector<int>g[N];
bool bfs(int s){
	queue<int>que;
	que.push(s);
	white++;
	col[s]=0;
	while(que.size()){
		int x=que.front();
		que.pop();
		for(int y:g[x]){
			if(col[y]==-1){
				col[y]=!col[x];
				if(col[y]==0){
					white++;
				}
				else{
					black++;
				}
			}
			else if(col[x]==col[y]){
				return false;
			}
		}
	}
	return true;
} 
int main(){
	cin>>n>>m;
	while(m--){
		int x,y;
		cin>>x>>y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	memset(col,-1,sizeof(col));
	int ans=0;
	for(int i=1;i<=n;i++){
		if(col[i]==-1){
		    white=black=0;
			if(bfs(i)){
				ans+=min(white,black);
			}
			else{
				cout<<"Impossible\n";
				return 0;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}

2024/7/24 10:45
加载中...