BFS判连通性70pts#5#6#9WA
查看原帖
BFS判连通性70pts#5#6#9WA
1183074
xzy_AK_IOI楼主2024/7/25 10:37
#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
typedef unsigned long long ull;
const int N=250000+10;
vector <int> e[N];
int degree[N];
bool con[N];
string a[N];
int n;
const int base=131;
map <string,int> mp;
void bfs(){
	queue <int> q;
	q.push(1);
	con[1]=1;
	while (!q.empty()){
		int x=q.front();
		q.pop();
		for (auto j:e[x]){
			if (!con[j]){
				con[j]=1;
				q.push(j);
			}
		}
	}
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	string left,right;
	while (cin>>left>>right){
		n++;
		a[n]=left;
		mp[right]=n;
	}
	for (int i=1;i<=n;i++){
		int u=mp[a[i]];
		if (u){
			e[u].push_back(i);
			e[i].push_back(u);
			degree[u]++;
			degree[i]++;
		}
	}
	/*cout<<n<<'\n';
	for (int i=1;i<=n;i++){
		cout<<i<<" ";
		for (auto j:e[i]){
			cout<<j<<" ";
		}
		cout<<'\n';
	}*/
    if (!n) {cout<<"Possible";return 0;}
	bfs();
	for (int i=2;i<=n;i++){
		if (!con[i]){
			cout<<"Impossible";
			return 0;
		}
	}
	int cnt=0;
	for (int i=1;i<=n;i++){
		if (degree[i]%2){
			cnt++;
		}
	}
	if (cnt<=2){
		cout<<"Possible";
	}
	else{
		cout<<"Impossible";
	}
	return 0;
}
2024/7/25 10:37
加载中...