60pts求助 WA4个点 使用unordered_map
查看原帖
60pts求助 WA4个点 使用unordered_map
244597
kabout楼主2023/6/16 12:37
#include<bits/stdc++.h>
using namespace std;
#define TLE ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
unordered_map<string,int>mp;//代表的是每个点的度数
unordered_map<string,string>fa;//福清节点
int od,cnt,sum;//奇数节点,已经合并了多少次,一共有多少个颜色
string Find(string x)
{
	if(fa[x]!=x)return fa[x]=Find(fa[x]);
	return x;
}
void mer(string x,string y)//这是并查集的合并
{
	string fx=Find(x),fy=Find(y);
	if(fx!=fy){
		cnt++;
		fa[fx]=fy;
	}
}
int main()
{
 	TLE;
 	string c1,c2;
 	while(cin>>c1>>c2)
 	{
 		if(mp[c1]==0)sum++,fa[c1]=c1;
 		if(mp[c2]==0)sum++,fa[c2]=c2;
 		mp[c1]++,mp[c2]++;
 		if(mp[c1]%2==1)od++;
 		else od--;
 		if(mp[c2]%2==1)od++;
 		else od--;
 		mer(c1,c2);//
	}
	if((cnt==sum-1&&(od<=2))||(sum==0))cout<<"Possible"<<endl;
	else cout<<"Impossible"<<endl;
 	return 0;
}
2023/6/16 12:37
加载中...