http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3605

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

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

Author: GUAN, Yao
Contest: The 9th Zhejiang Provincial Collegiate Programming Contest

分析;

Alice和Bob在玩一个游戏,该游戏需要n个杯子和一个石头,开始时石头被罩在在某个杯子里,Alice可交换任意两个杯子,经过一系列的交换,由Bob猜石头在哪个杯子里,交换总共m步,但Bob只看到了其中的k步,问Bob猜哪个杯子的可能性最大。

第一感觉就是和组合(在n个数里取m个有多少种)很相似,再看题目因为顺序是一定的,即选了k步之后,顺序只有一种。

c[n][m]=c[n-1][m-1]+c[n-1][m];

具体编码的时候只考虑到交换的两个杯子而忘记其他杯子在选择时的值也要加上c[n-1][m-1];

 

AC代码:

 #include<cstdio>
#include<cstring>
#define MAXN 55
using namespace std; long long dp[MAXN][MAXN][MAXN]; int main()
{
int T,n,m,s,k,i,j,t,a[MAXN],b[MAXN];
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&n,&m,&k,&s);
for(i=;i<=m;i++)
scanf("%d%d",a+i,b+i);
memset(dp,,sizeof(dp));
dp[][][s]=;
for(i=;i<=m;i++)
{
dp[i][][s]=;
for(j=;j<=i&&j<=k;j++)
{
dp[i][j][b[i]]=dp[i-][j-][a[i]];
dp[i][j][a[i]]=dp[i-][j-][b[i]];
for(t=;t<=n;t++)
{
dp[i][j][t]+=dp[i-][j][t];
if(t!=a[i]&&t!=b[i])//最开始忘记考虑的一种情况
dp[i][j][t]+=dp[i-][j-][t];
}
}
}
/*for(i=1;i<=m;i++,putchar('\n'))
for(j=0;j<=k;j++,putchar('\n'))
for(t=1;t<=n;t++)
printf("%d ",dp[i][j][t]);
printf("\n");*/
for(s=,t=;t<=n;t++)
if(dp[m][k][t]>dp[m][k][s])
s=t;
printf("%d\n",s);
}
return ;
}

最新文章

  1. Nginx模块参考手册:HTTP核心模块
  2. redis 在linux下的安装
  3. Spring Boot自定义错误页面,Whitelabel Error Page处理方式
  4. 初次接触mootools
  5. 【poj2186】 Popular Cows
  6. phpcms 采集教程
  7. service mongod start start: Unknown job: mongod问题
  8. 解决位图失真-SetStretchBltMode()
  9. 利用CART算法建立分类回归树
  10. 【Android - 框架】之Retrofit+RxJava的使用
  11. [C#基础] 泛型
  12. Spring MVC如何进行JSON数据的传输与接受
  13. Ubuntu如何备份和恢复系统 - 落花往事的日志 - 网易博客
  14. 自己用的reset.css,大部分转载,加上自己常用的设置
  15. Spring Boot 数据库连接池 HikariCP
  16. JS 通过 navigator获取判断浏览器信息
  17. MyBatis源码解析(十一)——Parsing解析模块之通用标记解析器(GenericTokenParser)与标记处理器(TokenHandler)
  18. Python3 tkinter基础 Menubutton 点击按钮出现下拉菜单
  19. linux系统编程:cp的另外一种实现方式
  20. (转)unity3D 如何提取游戏资源 (反编译)+代码反编译

热门文章

  1. 【POJ2104/2761】K-th Number
  2. asp.net mvc4 HTTP Error 403.14
  3. HDU-1231 简单dp,连续子序列最大和,水
  4. 0代码隐藏GroupedTableView上边多余的间隔
  5. 【JavaService】部署Java jar为Windows后台服务
  6. LeetCode(43. Multiply Strings)
  7. c#面向对象基础 重写、虚方法、抽象类
  8. centos6.6 LVS+keepalived
  9. jquery-validation 使用
  10. 400 Bad Request(angluarJs)