把每种颜色看成一个点,然后合并去判联通;
若联通,判一下是不是欧拉图或欧拉路。。。
还有,我的不是正解,要吸氧才能水过去、、、QAQ
// luogu-judger-enable-o2 // luogu-judger-enable-o2 #include<cstdio> #include<iostream> #include<cstring> //#include<unordered_map> #include<map> #define R register int using namespace std; //unordered_map<string,int>mp; map<string,int> mp; inline int g() { R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-1:fix; do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix; } int n,m,cnt,num,tot; int fa[500010],mu[250010],mv[250010],r[500010]; int getf(int x) {return x==fa[x]?x:fa[x]=getf(fa[x]);} inline void merge(int u,int v) { u=getf(u),v=getf(v); if(u==v) return ; fa[u]=v; } signed main() { register char u[11],v[11]; while(~scanf("%s%s",u,v)) { R uu,vv; if(mp.find(u)==mp.end()) uu=mp[u]=++num; else uu=mp[u]; if(mp.find(v)==mp.end()) vv=mp[v]=++num; else vv=mp[v]; mu[++cnt]=uu,mv[cnt]=vv; ++r[uu],++r[vv]; } for(R i=1;i<=num;++i) fa[i]=i; for(R i=1;i<=cnt;++i) merge(mu[i],mv[i]); for(R i=1;i<=num;++i) if(getf(i)==i) ++tot; if(tot>1) {printf("Impossible "); return 0;} cnt=0; for(R i=1;i<=num;++i) cnt+=(r[i]&1); (cnt==0||cnt==2)?printf("Possible "):printf("Impossible "); }
2019 .04.18&&upd:修改不规范的map使用方法