题解:【AT s8pc6 E】90-degree Rotations
赛前题解能不能攒 rp 啊?
考虑每次移动,横纵坐标只会改变一个。所以对于每个有金币的格子,将它的横纵坐标间连边。不难发现行走过程是在判断是否存在欧拉路径,于是只需要判断图上点的度数和是否连通即可,复杂度 \(\mathcal O(T \alpha(T))\),其中 \(T = nm\)。
#include<bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define ui unsigned int
#define ld long double
#define power(x) ((x)*(x))
#define eb emplace_back
#define pb pop_back
#define mp make_pair
#define fi first
#define se second
#define TT template<typename T>
#define TA template<typename T,typename ...Args>
#define dbg(x) cerr<<"In Line "<< __LINE__<<" the "<<#x<<" = "<<x<<'\n';
using namespace std;
using pii=pair<int,int>;
using pdi=pair<double,int>;
using piii=pair<pair<int,int>,int>;
bool Mbe;
namespace IO
{
inline int read()
{
int s=0,w=0; char c=getchar();
while(!isdigit(c)) w|=c=='-',c=getchar();
while(isdigit(c)) s=(s*10)+(c^48),c=getchar();
return s*(w?-1:1);
}
TT inline void read(T &s)
{
s=0; int w=0; char c=getchar();
while(!isdigit(c)) w|=c=='-',c=getchar();
while(isdigit(c)) s=(s*10)+(c^48),c=getchar();
s*=w?-1:1;
}
TA inline void read(T &x,Args &...args) {read(x),read(args...);}
TT inline void write(T x,char ch=' ')
{
if(x<0) x=-x,putchar('-');
static char stk[30]; int top=0;
do stk[top++]=x%10+'0',x/=10; while(x);
while(top) putchar(stk[--top]);
if(ch!='~') putchar(ch);
}
}
using namespace IO;
namespace MTool
{
static const int Mod=998244353;
TT inline void Swp(T &a,T &b) {static T t=a;a=b;b=t;}
TT inline void cmax(T &a,T b) {a=max(a,b);}
TA inline void cmax(T &a,T b,Args ...args) {a=max({a,b,args...});}
TT inline void cmin(T &a,T b) {a=min(a,b);}
TA inline void cmin(T &a,T b,Args ...args) {a=min({a,b,args...});}
TT inline T Cadd(T a,T b) {return a+b>=Mod?a+b-Mod:a+b;}
TT inline T Cdel(T a,T b) {return a-b<0?a-b+Mod:a-b;}
TT inline T Cmul(T a,T b) {return a*b%Mod;}
TT inline T Cmod(T a) {return (a%Mod+Mod)%Mod;}
TT inline void Madd(T &a,T b) {a=a+b>=Mod?a+b-Mod:a+b;}
TT inline void Mdel(T &a,T b) {a=a-b<0?a-b+Mod:a-b;}
TT inline void Mmul(T &a,T b) {a=a*b%Mod;}
TT inline void Mmod(T &a) {a=(a%Mod+Mod)%Mod;}
TA inline T Cadd(T a,T b,Args ...args) {return Cadd(Cadd(a,b),args...);}
TA inline T Cdel(T a,T b,Args ...args) {return Cdel(Cdel(a,b),args...);}
TA inline T Cmul(T a,T b,Args ...args) {return Cmul(Cmul(a,b),args...);}
TA inline void Madd(T &a,T b,Args ...args) {Madd(a,Cadd(b,args...));}
TA inline void Mdel(T &a,T b,Args ...args) {Mdel(a,Cadd(b,args...));}
TA inline void Mmul(T &a,T b,Args ...args) {Mmul(a,Cmul(b,args...));}
TT inline T qpow(T a,T b) {int res=1; while(b) {if(b&1) Mmul(res,a); Mmul(a,a); b>>=1;} return res;}
TT inline T qmul(T a,T b) {int res=0; while(b) {if(b&1) Madd(res,a); Madd(a,a); b>>=1;} return res;}
TT inline T spow(T a,T b) {int res=1; while(b) {if(b&1) res=qmul(res,a); a=qmul(a,a); b>>=1;} return res;}
TT inline void exgcd(T A,T B,T &X,T &Y) {if(!B) return X=1,Y=0,void(); exgcd(B,A%B,Y,X),Y-=X*(A/B);}
TT inline T Ginv(T x) {T A=0,B=0; exgcd(x,Mod,A,B); return Cmod(A);}
}
using namespace MTool;
inline void file()
{
freopen(".in","r",stdin);
freopen(".out","w",stdout);
}
namespace LgxTpre
{
constexpr int MAX=100000;
constexpr int inf=2147483647;
constexpr int INF=4557430888798830399;
int n,m,st,all,fl;
int deg[MAX],tag[MAX];
char s[101][101];
namespace DSU
{
int N,fa[MAX],siz[MAX];
inline void init() {for(int i=1;i<=N;++i) fa[i]=i,siz[i]=1;}
int find(int x) {return x==fa[x]?x:fa[x]=find(fa[x]);}
inline void merge(int x,int y) {x=find(x),y=find(y); if(x!=y) siz[x]>siz[y]?(fa[y]=x,siz[x]+=siz[y]):(fa[x]=y,siz[y]+=siz[x]);}
}
using namespace DSU;
inline void mian()
{
read(n,m),N=n+m,init();
for(int i=1;i<=n;++i) scanf("%s",s[i]+1);
for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) if(s[i][j]=='o') merge(i,j+n),++deg[i],++deg[j+n],tag[i]=1,tag[j+n]=1,st=i;
for(int i=1;i<=N;++i) if(tag[i]) fl|=find(i)!=find(st),all+=deg[i]&1;
if(fl) return puts("Impossible"),void();
if(all==0||all==2) return puts("Possible"),void();
puts("Impossible");
}
}
bool Med;
signed main()
{
// file();
int Tbe=clock();
LgxTpre::mian();
int Ted=clock();
fprintf(stderr,"\nMemory: %.3lf MB",abs(&Mbe-&Med)/1024.0/1024.0);
fprintf(stderr,"\nTime: %.3lf ms",1e3*(Ted-Tbe)/CLOCKS_PER_SEC);
return (0-0);
}