제출 #1118130

#제출 시각아이디문제언어결과실행 시간메모리
1118130vjudge1Mousetrap (CEOI17_mousetrap)C++17
100 / 100
667 ms186948 KiB
#include <bits/stdc++.h> using namespace std; #define TASK "" #define REP(i, n) for(int i = 1; i <= n; i++) #define FOR(i, a, b) for(auto i = a; i <= b; i++) #define FORD(i, a, b) for(auto i = a; i >= b; i--) template<class T> bool maximize(T& a, T b) { if(a < b) return a = b, 1; return 0; } template<class T> bool minimize(T& a, T b) { if(a > b) return a = b, 1; return 0; } #define sz(a) (int)(a.size()) using ii = pair<int, int>; #define fi first #define se second mt19937 rng(time(0)); const int N = (int)1e6 + 7; vector<int> adj[N]; vector<ii> save; bool inPath[N]; int n, t, m; int dp[N], dep[N]; void Read() { cin >> n >> t >> m; REP(i, n - 1) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } } void DFS(int u, int p = 0) { vector<int> cost; for(int v : adj[u]) { if(v == p) continue; if(p) dp[v] = dp[u] + sz(adj[u]) - 2; DFS(v, u); cost.push_back(dp[v]); if(inPath[v]) inPath[u] = 1; } sort(cost.begin(), cost.end(), greater<int>()); if(sz(cost) > 1) dp[u] = cost[1]; if(sz(adj[u]) > 1) dp[u]++; } void Run(int u, int p = -1) { for(int v : adj[u]) { if(v == p) continue; dep[v] = dep[u] + 1; if(inPath[u] && !inPath[v]) save.push_back({dep[v], dp[v] + (u == m)}); else Run(v, u); } } bool Check(int x) { int need = 0; for(auto s : save) { if(need > s.fi && need + s.se > x) return 0; if(need + s.se > x) need++; } return need <= x; } void Solve() { inPath[m] = 1; dep[m] = -1; DFS(t); Run(m); int l = -1, r = 1e9 + 7; sort(save.begin(), save.end()); while(l + 1 < r) { int m = l + r >> 1; if(Check(m)) r = m; else l = m; } cout << r << endl; } signed main(void) { ios_base::sync_with_stdio(false); cin.tie(nullptr); if(fopen("TASK.INP", "r")) freopen("TASK.INP", "r", stdin); if(fopen(TASK".INP", "r")) { freopen(TASK".INP", "r", stdin); freopen(TASK".OUT", "w", stdout); } Read(); Solve(); return 0; } /* 10 1 4 1 2 2 3 2 4 3 9 3 5 4 7 4 6 6 8 7 10 */

컴파일 시 표준 에러 (stderr) 메시지

mousetrap.cpp: In function 'void Solve()':
mousetrap.cpp:96:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   96 |         int m = l + r >> 1;
      |                 ~~^~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:110:39: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  110 |     if(fopen("TASK.INP", "r")) freopen("TASK.INP", "r", stdin);
      |                                ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:114:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  114 |         freopen(TASK".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...