답안 #468710

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
468710 2021-08-29T13:04:59 Z Josia Mousetrap (CEOI17_mousetrap) C++14
0 / 100
859 ms 524292 KB
#include <bits/stdc++.h>

#define int int64_t

using namespace std;



int t; 


map<tuple<int, vector<int>, bool>, int> mem;
map<tuple<int, vector<int>, bool>, bool> visited;

vector<vector<pair<int, int>>> graph; // v, id


int dp(int m, vector<int> state, bool turn) {   // state 0 = clean; 1 = dirty; 2 = blocked;
    if (mem[{m, state, turn}]) return mem[{m, state, turn}];

    if (visited[{m, state, turn}]) return 1e15;
    visited[{m, state, turn}] = 1;


    if (m == t) return 0;

    if (turn) {   // mouse: turn = 1
        int res = 0;
        for (auto i: graph[m]) {
            vector<int> 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;

        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;
            }
        }
        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;

    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});
        graph[b].push_back({a, i});
    }

    vector<int> 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<long int>, bool)':
mousetrap.cpp:41:26: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |         for (int i = 0; i<state.size(); i++) {
      |                         ~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 332 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 859 ms 524292 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 332 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 332 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -