提交时间:2021-05-16 17:57:57

运行 ID: 19556

#include<bits/stdc++.h> using namespace std; int n; char Head[51]; char Tail[51]; int Flag[51]; int Answer,Count; int IsFull(int y){ for(int i=1;i<=n;i++){ if(Tail[y]==Head[i]&&Flag[i]==0) return 0; } return 1; } int Solitaire(int x){ if(IsFull(x)){ if(Count>Answer) Answer=Count; memset(Flag,0,n); return 0; } for(int i=1;i<=n;i++){ if(Head[i]==Tail[x]&&Flag[i]==0){ Flag[x]++; Count++; Solitaire(i); } } } int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>Head[i]; cin>>Tail[i]; } Solitaire(1); cout<<Answer+1; return 0; }