#include <bits/stdc++.h>
#define int int64_t
using namespace std;
int t;
map<tuple<int, vector<char>, bool>, int> mem;
map<tuple<int, vector<char>, bool>, bool> visited;
vector<vector<pair<int, int>>> graph; // v, id
int dp(int m, vector<char> state, bool turn) { // state 0 = clean; 1 = dirty; 2 = blocked;
if (m == t) return 0;
if (visited[{m, state, turn}]) return 1e15;
if (mem.count({m, state, turn})) {return mem[{m, state, turn}];}
visited[{m, state, turn}] = 1;
if (turn) { // mouse: turn = 1
int res = 0;
for (auto i: graph[m]) {
vector<char> stateHere = state;
if (state[i.second] != 0) continue;
stateHere[i.second] = 1;
res = max(res, dp(i.first, stateHere, !turn)+1);
}
if (res == 0) res = dp(m, state, !turn)+1;
visited[{m, state, turn}] = 0;
return mem[{m, state, turn}] = res;
} else {
int res = dp(m, state, !turn)+1;
for (int i = 0; i<state.size(); i++) {
if (state[i] == 2) continue;
if (state[i] == 0) {
state[i] = 2;
res = min(res, dp(m, state, !turn)+1);
state[i] = 0;
}
if (state[i] == 1) {
state[i] = 0;
res = min(res, dp(m, state, !turn)+1);
state[i] = 1;
}
}
visited[{m, state, turn}] = 0;
return mem[{m, state, turn}] = res;
}
}
signed main() { // they may not be all connected!!!
cin.tie(0);
ios_base::sync_with_stdio(0);
int n, m; cin >> n >> t >> m;
m--; t--;
vector<int> a(1, 0);
graph.clear();
graph.resize(n);
for (int i = 1; i<n; i++) {
int a, b; cin >> a >> b;
a--; b--;
graph[a].push_back({b, i-1});
graph[b].push_back({a, i-1});
}
vector<char> state(n-1, 0);
cout << dp(m, state, 0) << "\n";
return 0;
}
Compilation message
mousetrap.cpp: In function 'int64_t dp(int64_t, std::vector<char>, bool)':
mousetrap.cpp:43:26: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (int i = 0; i<state.size(); i++) {
| ~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
83 ms |
4388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2122 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
83 ms |
4388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
83 ms |
4388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |