很暴力的一道题。问我方是否有一种排列能赢对方的所有排列。枚举呗。
dfs按字典序求出我方的一个排列,再用这个排列跟敌方的所有排列比,看赢了多少局。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<cmath>
///LOOP
#define REP(i, n) for(int i = 0; i < n; i++)
#define FF(i, a, b) for(int i = a; i < b; i++)
#define FFF(i, a, b) for(int i = a; i <= b; i++)
#define FD(i, a, b) for(int i = a - 1; i >= b; i--)
#define FDD(i, a, b) for(int i = a; i >= b; i--)
///INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define RFI(n) scanf("%lf", &n)
#define RFII(n, m) scanf("%lf%lf", &n, &m)
#define RFIII(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define RFIV(n, m, k, p) scanf("%lf%lf%lf%lf", &n, &m, &k, &p)
#define RS(s) scanf("%s", s)
///OUTPUT
#define PN printf("\n")
#define PI(n) printf("%d\n", n)
#define PIS(n) printf("%d ", n)
#define PS(s) printf("%s\n", s)
#define PSS(s) printf("%s ", n)
#define PC(n) printf("Case %d: ", n)
///OTHER
#define PB(x) push_back(x)
#define CLR(a, b) memset(a, b, sizeof(a))
#define CPY(a, b) memcpy(a, b, sizeof(b))
#define display(A, n, m) {REP(i, n){REP(j, m)PIS(A[i][j]);PN;}}
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int MOD = 9901;
const int INFI = 1e9 * 2;
const LL LINFI = 1e17;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int N = 11;
const int M = 22;
const int move[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1};
const int K[7] = {1, 1, 2, 6, 24, 120, 720};
bool can[N][N], vis[N], vis2[N];
string s[N], ch;
int a[N], b[N], n, win;
void dfs2(int step)
{
if(step == n)
{
int x, y;
x = y = 0;
while(x < n && y < n)
{
if(can[b[y]][a[x]])x++;
else y++;
}
if(x < n)win++;
return;
}
REP(i, n)if(!vis2[i])
{
vis2[i] = 1;
b[step] = i;
dfs2(step + 1);
vis2[i] = 0;
}
}
bool dfs(int step)
{
if(step == n)
{
CLR(vis2, 0);
win = 0;
dfs2(0);
return win == K[n];
}
REP(i, n)if(!vis[i])
{
vis[i] = 1;
a[step] = i;
if(dfs(step + 1))return 1;
vis[i] = 0;
}
return 0;
}
int main()
{
//freopen("input.txt", "r", stdin);
int t, m;
cin >> t;
FFF(cas, 1, t)
{
cin >> n;
REP(i, n)cin >> s[i];
sort(s, s + n);
CLR(can, 0);
CLR(vis, 0);
REP(i, n)
{
RI(m);
REP(j, m)
{
cin >> ch;
REP(k, n)if(s[k] == ch)
{
can[i][k] = 1;
break;
}
}
}
cout << "Case " << cas << ": ";
if(dfs(0))
{
puts("Yes");
REP(i, n - 1)cout << s[a[i]] << " ";
cout << s[a[n - 1]] << endl;
}
else puts("No");
}
return 0;
}