forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3605 - Find the Marble.cpp
58 lines (41 loc) · 1.16 KB
/
3605 - Find the Marble.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <cstdio>
#include <cstring>
using namespace std;
int s,a[50],b[50];
long long memo[50][51][51];
long long solve(int pos, int have, int marble){
if(have < 0) return 0;
if(pos == -1){
if(have > 0 || marble != s) return 0;
return 1;
}
long long &ret = memo[pos][have][marble];
if(ret == -1){
ret = solve(pos - 1,have,marble);
if(marble == a[pos]) ret += solve(pos - 1,have - 1,b[pos]);
else if(marble == b[pos]) ret += solve(pos - 1,have - 1,a[pos]);
else ret += solve(pos - 1,have - 1,marble);
}
return ret;
}
int main(){
int T,n,m,k;
scanf("%d",&T);
while(T--){
scanf("%d %d %d %d",&n,&m,&k,&s);
for(int i = 0;i < m;++i)
scanf("%d %d",&a[i],&b[i]);
int ans = 1;
long long aux = 0;
memset(memo,-1,sizeof memo);
for(int i = 1;i <= n;++i){
long long ret = solve(m - 1,k,i);
if(ret > aux){
aux = ret;
ans = i;
}
}
printf("%d\n",ans);
}
return 0;
}