首页 > 学院 > 开发设计 > 正文

ZOJ3605-Find the Marble

2019-11-06 09:31:05
字体:
来源:转载
供稿:网友

Find the Marble

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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

33 1 1 11 23 1 0 11 23 3 2 22 33 21 2

Sample Output

213
Author: GUAN, YaoContest: The 9th Zhejiang PRovincial Collegiate Programming Contest

题意:n个容器,m次两两交换,其中k次是可以知道的,一开始珠子放在容器s里,问你交换完以后,珠子在哪个容器的概率最大

解题思路:用DP[m][k][n] 表示 m次交换,知道了其中的k次,结尾为n的方案数

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>using namespace std;const int INF=0x3f3f3f3f;#define LL long longint main(){    int t,n,m,k,s;    int a[100],b[100];    LL dp[55][55][55];//dp[n][m][k]交换n次看到m次在k里的方案数    scanf("%d",&t);    while(t--)    {        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);        for(int i=0;i<=m;i++) dp[i][0][s]=1;        for(int i=1;i<=m;i++)        {            for(int j=1;j<=k&&j<=i;j++)            {                //看到交换并且交换的杯子里有石头                dp[i][j][a[i]]+=dp[i-1][j-1][b[i]];                dp[i][j][b[i]]+=dp[i-1][j-1][a[i]];                for(int p=1;p<=n;p++)                {                    //没看到交换                    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 ma=0;        int p;        for(int i=1;i<=n;i++)        {            if(dp[m][k][i]>ma)            {                ma=dp[m][k][i];                p=i;            }        }        printf("%d/n",p);    }    return 0;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表