CodeForces Gym 100500A A. Poetry Challenge DFS

时间:2023-03-09 21:33:57
CodeForces Gym 100500A A. Poetry Challenge DFS

Problem A. Poetry Challenge

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/gym/100500/attachments

Description

Let’s check another challenge of the IBM ICPC Chill Zone, a poetry challenge. One says a poetry string that starts with an English letter and ends with an English letter, the second should say a poetry string that starts with the same letter that the previous string ended with.
Given the two poetry string sets representing the known strings for each player. Each player can use each of his strings only once. If during the player turn he can not say any string, he loses. Assuming both players play optimally well determine which player wins the game depending on the given two sets.

Input

The first line contains an integer T represent the number of the following test cases. Each test case starts with an integer n the number of strings in the first player set. Each of the next n lines contains a string of the first player set. Then read an integer m, which will be succeeded by m lines describing the strings of the second player. No string in the input will start or finish with a white space, only lowercase letters. The length of each string in the input will not exceed 10,000 letters. 1 ≤ n ≤ 9 1 ≤ m ≤ 9 1 ≤ T ≤ 10

Output

For each test case, print one line saying which player should win if they are so clever to play it perfectly and assuming that each one knows the set of the other player. Discarding quotes, print "Game_i:_player1"to denote the wining of the first player or "Game_i:_player2"to denote the win of the second player where ‘i’ represents the game number starting from 1. Replace the underscores with spaces.

Sample Input

2 3 a poetry string a poetry string starting with a a poetry string ending with a 3 generated word a word ending with b poetry 2 either one or two random string 3 another test case one greatest poetry be the winner

Sample Output

Game 1: player2 Game 2: player1

HINT

题意

从player1开始进行字母接龙游戏,接不下去的输,问最后谁赢了

题解:

转化成点与点相接,dfs.......    感谢小q神的博客

代码

  

 #include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <typeinfo>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//**************************************************************************************
vector<int > e[];
bool vis[];
bool dfs(int x)
{
for(int i=; i<e[x].size(); i++)
{
if(vis[e[x][i]])continue;
vis[e[x][i]]=;
if(!dfs(e[x][i]))
{
vis[e[x][i]]=;
return ;
}
}
return ;
}
int main()
{
int oo=;
int T;
scanf("%d",&T);
while(T--)
{
memset(vis,,sizeof(vis));
for(int i=; i<; i++)
e[i].clear();
int n=read();
char a[];
char s1[][];
char s2[][];
for(int i=; i<=n; i++)
{
gets(s1[i]); }
int m=read();
for(int i=; i<=m; i++)
gets(s2[i]);
for(int i=; i<=n; i++)
{ int l=strlen(s1[i]);
for(int j=; j<=m; j++)
{ if(s1[i][l-]==s2[j][])
e[i].push_back(j+);
}
}
for(int i=; i<=m; i++)
{ int l=strlen(s2[i]);
for(int j=; j<=n; j++)
{ if(s2[i][l-]==s1[j][])
e[i+].push_back(j);
}
}
bool flag=false;
for(int i=; i<=n; i++)
{
vis[i]=;
if(!dfs(i))
{
flag=true;
break;
}
vis[i]=;
}
if(flag)
printf("Game %d: player1\n",oo++);
else
printf("Game %d: player2\n",oo++);
}
return ;
}