Street Fighter
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1350 Accepted Submission(s): 528
Problem Description
Street Fighter is a popular Fighting game. In the game, you could choose many roles to fight.
Each role has some models, in different model the role has different super skill. Such as Ryu, he has two model -- "Metsu Hadoken" and "Metsu Shoryuken". In "Metsu Hadoken" model, Ryu could beat Chun-Li easyly and in "Metsu Shoryuken" he could beat Ken.
Giving the information of which role in which model could beat which role in which model. Your task is choosing minimum roles in certain model to beat other roles in any model.(each role could only be chosen once)
Each role has some models, in different model the role has different super skill. Such as Ryu, he has two model -- "Metsu Hadoken" and "Metsu Shoryuken". In "Metsu Hadoken" model, Ryu could beat Chun-Li easyly and in "Metsu Shoryuken" he could beat Ken.
![](https://i-blog.csdnimg.cn/blog_migrate/33b654e7a9ea2e7ca2a1005cfe8815ef.jpeg)
Giving the information of which role in which model could beat which role in which model. Your task is choosing minimum roles in certain model to beat other roles in any model.(each role could only be chosen once)
Input
The first line is a number T(1<=T<=30), represents the number of case. The next T blocks follow each indicates a case.
The first line of each case contains a integers N(2<=N<=25), indication the number of roles. (roles numbered from 0 to N - 1).
Then N blocks follow, each block contain the information of a role.
The first of each block contains a integer M(1<=M<=2), indication the number of model of this role.(models numbered from 0 to M - 1)
Then M lines follow, each line contain a number K(1<=K<=10), then K pairs integers(role id and model id) follow, indicating the current role in this model could beat that role in such model.
The first line of each case contains a integers N(2<=N<=25), indication the number of roles. (roles numbered from 0 to N - 1).
Then N blocks follow, each block contain the information of a role.
The first of each block contains a integer M(1<=M<=2), indication the number of model of this role.(models numbered from 0 to M - 1)
Then M lines follow, each line contain a number K(1<=K<=10), then K pairs integers(role id and model id) follow, indicating the current role in this model could beat that role in such model.
Output
For each case, output the number of case and the minimum roles have to choose to reach the goal.(as shown in the sample output)
Sample Input
2 2 2 1 1 0 1 1 1 2 1 0 0 1 0 1 4 2 2 1 0 1 1 1 2 0 2 2 3 0 0 1 1 2 0 2 2 0 0 0 1 1 1 0 2 2 2 0 2 1 1 1 0
Sample Output
Case 1: 2 Case 2: 2HintIn the first sample, you must select all of roles, because one role couldn't beat the other role in any model. In the second sample, you can select role 0 with its model 0, and role 3 with its model 0, then role 1 and role 2 will to be defeated no matter which model they use.
Author
NotOnlySuccess
Source
题意:有n个人物,每个人物有一到两种模式,每种模式有他特定可以击败的一些特定模式的人物。现在要选择一些人物,同时要确定他们的模式,使得这些人能够击败剩下其他人的所有模式,一个人物只能选择一种模式。求要选择的最少的人物数。
解题思路:舞蹈链重复覆盖
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
const int maxn = 300005;
int n, m, K, id[30][3], tot, x, y;
struct node
{
int x, y;
bool operator <(const node &a)const
{
return id[x][y] < id[a.x][a.y];
}
};
vector<node>a[30][3];
struct DLX
{
int L[maxn], R[maxn], U[maxn], D[maxn];
int row[maxn], col[maxn], sum[maxn], ans[maxn];
int n, m, num, cnt;
int vis[maxn], flag[maxn];
void add(int k, int l, int r, int u, int d, int x, int y)
{
L[k] = l; R[k] = r; U[k] = u;
D[k] = d; row[k] = x; col[k] = y;
}
void reset(int n, int m)
{
this->n = n; this->m = m;
for (int i = 0; i <= m; i++)
{
add(i, i - 1, i + 1, i, i, 0, i);
sum[i] = 0;
}
L[0] = m, R[m] = 0, cnt = m + 1;
}
void insert(int x, int y)
{
int temp = cnt - 1;
if (row[temp] != x)
{
add(cnt, cnt, cnt, U[y], y, x, y);
U[D[cnt]] = cnt; D[U[cnt]] = cnt;
}
else
{
add(cnt, temp, R[temp], U[y], y, x, y);
R[L[cnt]] = cnt; L[R[cnt]] = cnt;
U[D[cnt]] = cnt; D[U[cnt]] = cnt;
}
sum[y]++, cnt++;
}
void Remove(int k)
{
for (int i = D[k]; i != k; i = D[i])
{
L[R[i]] = L[i];
R[L[i]] = R[i];
}
}
void Resume(int k)
{
for (int i = U[k]; i != k; i = U[i]) L[R[i]] = R[L[i]] = i;
}
int A()
{
int dis = 0;
for (int i = R[0]; i != 0; i = R[i]) vis[i] = 0;
for (int i = R[0]; i != 0; i = R[i])
if (!vis[i])
{
dis++, vis[i] = 1;
for (int j = D[i]; j != i; j = D[j])
for (int k = R[j]; k != j; k = R[k])
vis[col[k]] = 1;
}
return dis;
}
void Dfs(int k)
{
if (!R[0]) num = min(num, k);
else if(k + A() < num)
{
int now = R[0];
for (int i = R[0]; i != 0; i = R[i])
if (sum[now] > sum[i]) now = i;
for (int i = D[now]; i != now; i = D[i])
{
if (flag[row[i] ^ 1]) continue;
flag[row[i]] = 1;
Remove(i);
for (int j = R[i]; j != i; j = R[j]) Remove(j);
Dfs(k + 1);
for (int j = L[i]; j != i; j = L[j]) Resume(j);
Resume(i);
flag[row[i]] = 0;
}
}
}
void mul()
{
memset(flag, 0, sizeof flag);
num = 0x7FFFFFFF;
}
}dlx;
int main()
{
int t, cas = 0;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 0; j < 2; j++) a[i][j].clear();
memset(id, 0, sizeof id);
tot = 0;
for (int i = 1; i <= n; i++)
{
scanf("%d", &m);
for (int j = 0; j < m; j++)
{
id[i][j] = ++tot;
scanf("%d", &K);
for (int k = 0; k < K; k++)
{
scanf("%d%d", &x, &y);
a[i][j].push_back(node{ x + 1,y });
}
for (int k = 0; k < m; k++) a[i][j].push_back(node{ i,k });
}
}
dlx.reset(n * 2 + 2, tot);
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < 2; j++)
{
sort(a[i][j].begin(), a[i][j].end());
int Size = a[i][j].size();
for (int k = 0; k < Size; k++)
dlx.insert(i * 2 + j, id[a[i][j][k].x][a[i][j][k].y]);
}
}
dlx.mul();
dlx.Dfs(0);
printf("Case %d: %d\n", ++cas, dlx.num);
}
return 0;
}