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 was played with several identical pots and one marble. When the game starts, Alice puts the pots on 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 are in. In each of the swapping, Alice chooses, different pots and swaps their positions.
Unfortunately, Alice's actions is very fast, so Bob can be only catch K of m Swappings and regard these k swappings As all actions Alice has performed. Now given the initial pot the marble was in, and the sequence of swappings, you be asked to calculate which pot Bob most p Ossibly guesses. You can assume this Bob missed any of the swappings with equal possibility.
Input
There is several test cases in the input file. The first line of the input file contains a integer n (n ≈100), then n cases follow.
The first line of all test case contains 4 integers n, m, K and s(0 < s ≤ n ≤ 50, 0≤ k ≤ m ≤50), which is the number of pots, the number of swappings Alice makes, th E number of swappings Bob catches and index of the initial pot the marble is in. Pots is indexed from 1 to N. Then M. lines follow, each of which contains, integers ai and bi (1≤ ai, bi ≤ n), telling the swaps in the-th swapping of the pots Alice.
Outout
For each test case, the output of the pot that Bob is 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, Yao
Contest: The 9th Zhejiang Provincial Collegiate Programming Contest
Analysis
Alice and Bob are playing a game that requires n cups and a stone, and at first the stone is in a cup, Alice can exchange any two cups, and after a series of exchanges, Bob guesses in which cup the stones are exchanged for a total of M steps, but Bob sees only the K-Steps, Ask Bob to guess which cup is the most likely.
The first feeling is that the combination (in the n number of M has how many) very similar, and then see the topic because the order is certain, that is, after the K-step, the order is only one.
C[N][M]=C[N-1][M-1]+C[N-1][M];
The specific code when only consider the exchange of two cups and forget the other cups in the choice of value also add c[n-1][m-1];
AC Code:
1#include <cstdio>2#include <cstring>3 #defineMAXN 554 using namespacestd;5 6 Long LongDP[MAXN][MAXN][MAXN];7 8 intMain ()9 {Ten intT,N,M,S,K,I,J,T,A[MAXN],B[MAXN]; Onescanf"%d",&T); A while(t--) - { -scanf"%d%d%d%d",&n,&m,&k,&s); the for(i=1; i<=m;i++) -scanf"%d%d", a+i,b+i); -Memset (DP,0,sizeof(DP)); -dp[0][0][s]=1; + for(i=1; i<=m;i++) - { +dp[i][0][s]=1; A for(j=1; j<=i&&j<=k;j++) at { -dp[i][j][b[i]]=dp[i-1][j-1][a[i]]; -dp[i][j][a[i]]=dp[i-1][j-1][b[i]]; - for(t=1; t<=n;t++) - { -dp[i][j][t]+=dp[i-1][j][t]; in if(T!=a[i]&&t!=b[i])//One of the things that I forgot to consider at first -dp[i][j][t]+=dp[i-1][j-1][t]; to } + } - } the /*For (I=1;i<=m;i++,putchar (' \ n ')) * For (J=0;j<=k;j++,putchar (' \ n ')) $ For (t=1;t<=n;t++)Panax Notoginseng printf ("%d", dp[i][j][t]); - printf ("\ n");*/ the for(s=1, t=2; t<=n;t++) + if(dp[m][k][t]>Dp[m][k][s]) As=T; theprintf"%d\n", s); + } - return 0; $}
View Code
Zjuoj 3605 Find the Marble