ãã£ã¡ãé¢ç½ãåé¡ã ã£ãï¼
åé¡æ¦è¦
é ç¹ 0 ãæ ¹ã¨ããé ç¹æ° ã®æ ¹ä»ãæ¨ãä¸ãããã¾ããé ç¹ 0 以å¤ã®é ç¹ ã«ã¯æ°å¤ ãæ¸ããã¦ãã¾ããä»ãé ç¹ 0 ã«ã³ããç½®ãã¦ããã¾ãã
é«æ©ããã¨éæ¨ããã次ã®åä½ã交äºã«ç¹°ãè¿ãã¾ãã
- éæ¨ããã¯ã好ããªé ç¹ãé¸ãã§ããã®é ç¹ã®å¤ã 0 ã«ãã
- é«æ©ããã¯ãã³ããåé ç¹ã®ããããã«ç§»åãããã移åã§ããªãã¨ãã¯ãã®å ´ã§ã²ã¼ã ãçµäºãã
é«æ©ããã¯ä»»æã®ã¿ã¤ãã³ã°ã§ã²ã¼ã ãçµäºã§ãã¾ããããã¦ã²ã¼ã çµäºããæã®ã³ãã®ç½®ãããé ç¹ã®æ°å¤ããã²ã¼ã ã®ã¹ã³ã¢ã¨ãªãã¾ãã
é«æ©ããã¯ã¹ã³ã¢ã®æ大åãç®æããéæ¨ããã¯ã¹ã³ã¢ã®æå°åãç®æãã¾ããåæ¹ãæåãå°½ãããã¨ãã®ã²ã¼ã ã®ã¹ã³ã¢ãæ±ãã¦ãã ããã
å¶ç´
èãããã¨
ã¾ãçã£å ã«äºåæ¢ç´¢ãããã¨æã£ããããããã°ãç½é»ã²ã¼ã ã«ãªããã¤ã¾ããåé ç¹ãç½ã¾ãã¯é»ã«å¡ããã¦ãã¦ã
- é«æ©ããã¯é»é ç¹ã«çå°ãããã¨ãç®æã
- éæ¨ããã¯é»é ç¹ãç½ãå¡ãã¤ã¶ããã¨ã§ãé«æ©ãããé»é ç¹ã«çå°ããã®ãé»æ¢ãããã¨ãç®æã
ã¨ããã²ã¼ã ã«ãªãããã®ããã«ãäºåæ¢ç´¢ãããã¨ã§ãã²ã¼ã ã ã£ããã°ã©ãã ã£ããç¤é¢ã ã£ãããç½é»ã«ããã±ã¼ã¹ã¯ããããæ°ãããã
ç½é»ã²ã¼ã ã®è§£ãæ¹
ã¾ãæã£ãã®ã¯ãé«æ©ãããåã¦ãå ´åãåã¤ç´åã¯ä¸å³ã®ããã«ãã³ãã®ããé ç¹ã®åé ç¹ã®ãã¡ã2 å以ä¸ãé»è²ãã¨ããç¶æ³ã«ããã
1 åã ããé»è²ã ã¨ãå ã«éæ¨ããã«ç½è²ã«ããã¦åã¦ãªããã§ã 2 åãé»è²ãªãéæ¨ããã両æ¹ãç½è²ã«ããåã«é«æ©ãããé»è²é ç¹ã«çå°ã§ããã®ã ã
ããã¦ããã«è¨ãã°ã3 åãé»è²ãªããã³ããããä¸æ©æåã«ãã£ã¦ãéã«åãã
ãã®ç¶æ³ãä¸è¬åãã¦ãåé ç¹ã®ãä½è£å¤ããæ±ãããããã ãã¤ã¾ã次ã®ãããªå¤ãè¨ç®ã§ãããã ã
dp[v]
â é ç¹ ã«ã³ããããç¶æ
ãããéæ¨ããã«ä½æå
ã«æä½ããã¦ããé«æ©ãããé»é ç¹ã«çå°ã§ããã
ãã¨ãã°ã
- é»é ç¹èªä½ã®ä½è£å¤ã¯ 1 (ãã§ã«çå°ãã¦ãããã¨ãã)
- åé ç¹ã®ãã¡ã® 2 åãé»é ç¹ã§ãããããªé ç¹ã®ä½è£å¤ã¯ 1
- åé ç¹ã®ãã¡ã® 3 åãé»é ç¹ã§ãããããªé ç¹ã®ä½è£å¤ã¯ 2
ã¨ãªãããã£ã¨ä¸è¬ã«ã次ã®ãããªæ¨ DP ã§ä½è£å¤ãæ±ããããã
dp[v]
= dp[c]
+ (é ç¹ ãé»é ç¹ ? 1 : 0)
ãã㦠dp[0]
ã 1 以ä¸ãªãé«æ©ããã®åã¡ã0 ãªãéæ¨ããã®åã¡ããã® DP ã«è¦ããè¨ç®é㯠ã¨ãªãã
ãã£ã¦äºåæ¢ç´¢æ³å ¨ä½ãåãããã¨ãå ¨ä½ã®è¨ç®é㯠ã¨ãã¦ã ã¨è©ä¾¡ã§ããã
ã³ã¼ã
#include <bits/stdc++.h> using namespace std; using Graph = vector<vector<int>>; int main() { int N; cin >> N; vector<int> A(N, 0); for (int i = 1; i < N; ++i) cin >> A[i]; Graph G(N); for (int i = 0; i < N-1; ++i) { int u, v; cin >> u >> v; --u, --v; G[u].push_back(v); G[v].push_back(u); } long long low = -1, high = 1LL<<60; while (high - low > 1) { long long x = (low + high) / 2; auto dfs = [&](auto self, int v, int p = -1) -> int { int res = 0; for (auto ch : G[v]) { if (ch == p) continue; res += self(self, ch, v); } return max(0, res - 1) + (A[v] >= x); }; if (dfs(dfs, 0) >= 1) low = x; else high = x; } cout << low << endl; }
å¥è§£
äºåæ¢ç´¢ã§ã¯ãªãæ¹æ³ã§ ã§æ±ããããæ¹æ³ãããããã£ãã® DP ã§è¨ãã°ã
dp[v][x]
â æ°å¤ã 以ä¸ã§ããé ç¹ãé»é ç¹ã§ããã¨ããå ´åã®ãé ç¹ ã®ä½è£å¤
ã¨ãããé
å dp[v]
ã®å¤ã¯ã ã«å¿ãã¦å調æ¸å°ã¨ãªãããå¤åããé¨åã¯é«ã
ç®æãããªãã®ã ã
ãã£ã¦ dp[v]
ã®å¤ã¯ãå¤åç¹ãå«ãã¦ãé«ã
次å
ãã¯ãã«ã§ç®¡çã§ããã
æ¨ DP ãçµãã¨ãã«ããã¼ã¸ãã¯ã使ããããã«ãã¼ãã®ãã¼ã¸ãé«éã«ã§ãã meldable heap ãç¨ãããã¨ã§ ã®è¨ç®éã¨ãªãã
詳細㯠Nachia ããã®ã¦ã¼ã¶è§£èª¬ã«ã¦