QOJ6562 First Last

First Last

Description

https://qoj.ac/contest/1248/problem/6562

注意到重要性质出现在首尾的字符数量 $\le 3$。

Solution

首先先压缩成 $9$ 个点。原来的博弈可以看作 $9$ 个点以及很多条有向边(包括自环)构成的有向图。

注意到:

  • 如果一个点有两个自环,那么删去这两个边,局面胜负关系不变。
  • 如果存在边 $(x,y)$ 和 $(y,x)$,那么删去这两个边,局面的胜负关系不变。

具体证明的话可以通过讨论这个点的胜负关系,然后发现另外一个人可以绕回去来搞。

把每种边(如果有的话)删去到最多一条,这样整个图非常稀疏。直接跑暴力就好了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define maxn
#define put() putchar('\n')
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
using namespace std;
void read(int &x){
int f=1;x=0;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
x*=f;
}
namespace Debug{
Tp void _debug(char* f,Ty t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,Ty x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
Tp ostream& operator<<(ostream& os,vector<Ty>& V){os<<"[";for(auto& vv:V) os<<vv<<",";os<<"]";return os;}
#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
}using namespace Debug;
#define fi first
#define se second
#define mk make_pair
const int mod=1e9+7;
int power(int x,int y=mod-2) {
int sum=1;
while (y) {
if (y&1) sum=sum*x%mod;
x=x*x%mod;y>>=1;
}
return sum;
}
int nums[4][4],to[4][4],e[4][4],id[105],cnt;
int n,ans;
bool dfs(int x) {
int flag=0,vis=0,i;
for (i=0;i<3;i++) if (e[x][i]) {
flag=1;e[x][i]--;
vis|=(dfs(i)==0);
e[x][i]++;
}
if (!flag) return 0;
else return vis;
}
signed main(void){
int i,j;string s;
read(n);
for (i=1;i<=n;i++) {
cin>>s;
int tmp1=s[0]-'a';if (!id[tmp1]) id[tmp1]=++cnt;
int tmp2=s[s.size()-1]-'a';if (!id[tmp2]) id[tmp2]=++cnt;
nums[id[tmp1]-1][id[tmp2]-1]++;
}
for (i=0;i<3;i++) for (j=0;j<3;j++) to[i][j]=nums[i][j];
for (i=0;i<3;i++) to[i][i]=(to[i][i]-1)%2+1;
for (i=0;i<3;i++) for (j=i+1;j<3;j++) {
int x=min(to[i][j],to[j][i]);
x=max(x-1,0);
to[i][j]-=x;
to[j][i]-=x;
}
for (i=0;i<3;i++) for (j=0;j<3;j++) gdb(i,j,to[i][j]);
for (i=0;i<3;i++) for (j=0;j<3;j++) {
memcpy(e,to,sizeof(e));int flag=0;
if (e[i][j]) e[i][j]--,flag=(dfs(j)==0);
else flag=dfs(i);
ans+=flag*nums[i][j];
}
printf("%d\n",ans);
return 0;
}