当前位置: 首页 > 工具软件 > Marble > 使用案例 >

ZOJ3605:Find the Marble(普通dp)

姚骁
2023-12-01

Alice and Bob are playing a game. This game is played with several identical pots and one marble. When the game starts, Alice puts the pots in one line and puts the marble in one of the pots. After that, Bob cannot see the inside of the pots. Then Alice makes a sequence of swappings and Bob guesses which pot the marble is in. In each of the swapping, Alice chooses two different pots and swaps their positions.

Unfortunately, Alice's actions are very fast, so Bob can only catch k of m swappings and regard these k swappings as all actions Alice has performed. Now given the initial pot the marble is in, and the sequence of swappings, you are asked to calculate which pot Bob most possibly guesses. You can assume that Bob missed any of the swappings with equal possibility.

Input

There are several test cases in the input file. The first line of the input file contains an integer N (N ≈ 100), then N cases follow.

The first line of each test case contains 4 integers nmk and s(0 < s ≤ n ≤ 50, 0 ≤ k ≤ m ≤ 50), which are the number of pots, the number of swappings Alice makes, the number of swappings Bob catches and index of the initial pot the marble is in. Pots are indexed from 1 to n. Then m lines follow, each of which contains two integers ai and bi (1 ≤ aibi ≤ n), telling the two pots Alice swaps in the i-th swapping.

Outout

For each test case, output the pot that Bob most possibly guesses. If there is a tie, output the smallest one.

Sample Input

3
3 1 1 1
1 2
3 1 0 1
1 2
3 3 2 2
2 3
3 2
1 2

Sample Output

2
1
3
题意:N只杯,初始一个石头在S号杯里面(玩家知道),M次交换杯的操作,玩家只能记住其中的任意K次交换,问最终玩家会认为石头在哪个杯子里面。

思路:就是算任选K次后石头在哪个杯子出现的次数最多,dp[i][j][k]表示前i次操作,交换了j次,石头停在k号杯子的方案数,注意用longlong存。

# include <iostream>
# include <cstdio>
# include <algorithm>
# include <cstring>
# include <vector>
using namespace std;
typedef long long LL;
int a[53], b[53];
LL dp[53][53][53];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n, m, k, s;
        scanf("%d%d%d%d",&n,&m,&k,&s);
        for(int i=1; i<=m; ++i) scanf("%d%d",&a[i],&b[i]);
        memset(dp, 0, sizeof(dp));
        dp[0][0][s] = 1LL;
        for(int i=1; i<=m; ++i)
        {
            for(int j=0; j<i; ++j)
            {
                dp[i][j+1][a[i]] = dp[i-1][j][b[i]];
                dp[i][j+1][b[i]] = dp[i-1][j][a[i]];
            }
            for(int p=1; p<=n; ++p)
            {
                dp[i][0][p] = dp[i-1][0][p];
                for(int j=1; j<=i; ++j)
                {
                    dp[i][j][p] += dp[i-1][j][p];
                    if(p!=a[i] && p!=b[i]) dp[i][j][p] += dp[i-1][j-1][p];
                }
            }

        }
        LL imax = 0;
        int id;
        for(int i=1; i<=n; ++i)
        {
            if(dp[m][k][i] > imax)
            {
                imax = dp[m][k][i];
                id = i;
            }
        }
        printf("%d\n",id);
    }
    return 0;
}

 类似资料: