답안 #115246

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
115246 2019-06-06T10:33:42 Z IOrtroiii Mousetrap (CEOI17_mousetrap) C++14
100 / 100
1278 ms 186108 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 1000100;

int n, t, m;
vector<int> g[N];
vector<int> vals[N];
vector<int> tour;
bool on[N];
int pref[N];

bool dfs(int u, int p, int to) {
   tour.push_back(u);
   if (u == to) return true;
   for (int v : g[u]) {
      if (v != p) {
         if (dfs(v, u, to)) {
            return true;
         }
      }
   }
   tour.pop_back();
   return false;
}

int dfs2(int u, int p) {
   vector<int> vals;
   for (int v : g[u]) {
      if (v != p) {
         vals.push_back(dfs2(v, u));
      }
   }
   sort(vals.begin(), vals.end(), greater<int>());
   if (vals.empty()) return 0;
   else if (vals.size() == 1) return 1;
   else return vals.size() + vals[1];
}

bool solve(int md) {
   int free = 0;
   int use = 0;
   for (int i = tour.size() - 1; i > 0; --i) {
      ++free;
      int u = tour[i];
      int ptr = 0;
      while (ptr < vals[u].size() && free && use + pref[i] + vals[u][ptr] > md) {
         --free; ++ptr;
      }
      if (ptr < vals[u].size() && use + pref[i] + vals[u][ptr] > md) {
         return false;
      }
      use += ptr;
   }
   if (use > md) {
      return false;
   }
   return true;
}

int main() {
   scanf("%d %d %d", &n, &t, &m);
   for (int i = 1; i < n; ++i) {
      int u, v;
      scanf("%d %d", &u, &v);
      g[u].push_back(v);
      g[v].push_back(u);
   }
   dfs(t, -1, m);
   for (int u : tour) on[u] = true;
   for (int u : tour) {
      for (int v : g[u]) {
         if (!on[v]) {
            vals[u].push_back(dfs2(v, u));
         }
      }
      sort(vals[u].begin(), vals[u].end(), greater<int>());
   }
   for (int i = 1; i < tour.size(); ++i) {
      int u = tour[i];
      pref[i] = pref[i - 1];
      for (int v : g[u]) {
         if (!on[v]) pref[i]++;
      }
   }
   int l = 0, r = n - 1;
   while (l < r) {
      int md = (l + r) >> 1;
      if (solve(md)) r = md;
      else l = md + 1;
   }
   printf("%d\n", l);
}

Compilation message

mousetrap.cpp: In function 'bool solve(int)':
mousetrap.cpp:48:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       while (ptr < vals[u].size() && free && use + pref[i] + vals[u][ptr] > md) {
              ~~~~^~~~~~~~~~~~~~~~
mousetrap.cpp:51:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       if (ptr < vals[u].size() && use + pref[i] + vals[u][ptr] > md) {
           ~~~~^~~~~~~~~~~~~~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:80:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i = 1; i < tour.size(); ++i) {
                    ~~^~~~~~~~~~~~~
mousetrap.cpp:63:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d %d %d", &n, &t, &m);
    ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:66:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%d %d", &u, &v);
       ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 47356 KB Output is correct
2 Correct 44 ms 47352 KB Output is correct
3 Correct 42 ms 47352 KB Output is correct
4 Correct 42 ms 47332 KB Output is correct
5 Correct 43 ms 47224 KB Output is correct
6 Correct 42 ms 47228 KB Output is correct
7 Correct 42 ms 47224 KB Output is correct
8 Correct 43 ms 47360 KB Output is correct
9 Correct 41 ms 47224 KB Output is correct
10 Correct 42 ms 47352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 438 ms 92140 KB Output is correct
2 Correct 401 ms 87524 KB Output is correct
3 Correct 1110 ms 93020 KB Output is correct
4 Correct 508 ms 70008 KB Output is correct
5 Correct 1149 ms 93108 KB Output is correct
6 Correct 1142 ms 93032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 47356 KB Output is correct
2 Correct 44 ms 47352 KB Output is correct
3 Correct 42 ms 47352 KB Output is correct
4 Correct 42 ms 47332 KB Output is correct
5 Correct 43 ms 47224 KB Output is correct
6 Correct 42 ms 47228 KB Output is correct
7 Correct 42 ms 47224 KB Output is correct
8 Correct 43 ms 47360 KB Output is correct
9 Correct 41 ms 47224 KB Output is correct
10 Correct 42 ms 47352 KB Output is correct
11 Correct 44 ms 47352 KB Output is correct
12 Correct 44 ms 47352 KB Output is correct
13 Correct 45 ms 47360 KB Output is correct
14 Correct 44 ms 47380 KB Output is correct
15 Correct 45 ms 47352 KB Output is correct
16 Correct 45 ms 47352 KB Output is correct
17 Correct 43 ms 47352 KB Output is correct
18 Correct 94 ms 47352 KB Output is correct
19 Correct 45 ms 47352 KB Output is correct
20 Correct 45 ms 47352 KB Output is correct
21 Correct 44 ms 47352 KB Output is correct
22 Correct 41 ms 47352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 47356 KB Output is correct
2 Correct 44 ms 47352 KB Output is correct
3 Correct 42 ms 47352 KB Output is correct
4 Correct 42 ms 47332 KB Output is correct
5 Correct 43 ms 47224 KB Output is correct
6 Correct 42 ms 47228 KB Output is correct
7 Correct 42 ms 47224 KB Output is correct
8 Correct 43 ms 47360 KB Output is correct
9 Correct 41 ms 47224 KB Output is correct
10 Correct 42 ms 47352 KB Output is correct
11 Correct 438 ms 92140 KB Output is correct
12 Correct 401 ms 87524 KB Output is correct
13 Correct 1110 ms 93020 KB Output is correct
14 Correct 508 ms 70008 KB Output is correct
15 Correct 1149 ms 93108 KB Output is correct
16 Correct 1142 ms 93032 KB Output is correct
17 Correct 44 ms 47352 KB Output is correct
18 Correct 44 ms 47352 KB Output is correct
19 Correct 45 ms 47360 KB Output is correct
20 Correct 44 ms 47380 KB Output is correct
21 Correct 45 ms 47352 KB Output is correct
22 Correct 45 ms 47352 KB Output is correct
23 Correct 43 ms 47352 KB Output is correct
24 Correct 94 ms 47352 KB Output is correct
25 Correct 45 ms 47352 KB Output is correct
26 Correct 45 ms 47352 KB Output is correct
27 Correct 44 ms 47352 KB Output is correct
28 Correct 41 ms 47352 KB Output is correct
29 Correct 43 ms 47448 KB Output is correct
30 Correct 438 ms 92120 KB Output is correct
31 Correct 443 ms 92152 KB Output is correct
32 Correct 515 ms 163804 KB Output is correct
33 Correct 476 ms 186108 KB Output is correct
34 Correct 1278 ms 93148 KB Output is correct
35 Correct 1232 ms 93048 KB Output is correct
36 Correct 1101 ms 103132 KB Output is correct
37 Correct 1129 ms 103152 KB Output is correct
38 Correct 917 ms 105076 KB Output is correct
39 Correct 912 ms 104944 KB Output is correct
40 Correct 892 ms 105176 KB Output is correct