80分求助,悬赏关注
查看原帖
80分求助,悬赏关注
546681
lcbridgeAK CSP-S楼主2023/2/2 16:22

RT,WAon#8,#10

#include <bits/stdc++.h>
#define MAXN 500005
using namespace std;
unordered_map <string,int> mp;
string l,r;
int cnt,tmp,ind[MAXN],fa[MAXN],n;
int find(int x){
	if(x==fa[x])return x;
	return fa[x]=find(fa[x]);
}
int num(string s) {
   if(mp[s])return mp[s];
   else return mp[s]=++cnt;
}
void merge(int x,int y){
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)return ;
	else {
		fa[fy]=fx;
		tmp++;
	}
}
int main(){
	while(cin>>l>>r){
		int x=num(l); 
		int y=num(r);
		merge(x,y);
		ind[x]++;
		ind[y]++;
	}
	if(tmp<cnt-1){
		cout<<"Impossible";
		return 0; 
	}
	int tmp1=0,tmp2=0;
	for(int i=1;i<=cnt;i++){
        //cout<<ind[i]<<' ';
		if(ind[i]%2==1)tmp2++;
		else tmp1++;
	}
    //cout<<"\n"<<tmp1<<' '<<tmp2;
	if(tmp2==0)cout<<"Possible";
	else {
        if(tmp2==2)cout<<"Possible"; 
        else cout<<"Impossible";
    }
	return 0;
}
2023/2/2 16:22
加载中...