Advertisement
Dmaxiya

扫地机器人 参考代码

Apr 12th, 2025
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.68 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const int maxn = 500000 + 100;
  6. const int Log = 40;
  7. int n, v, u, ans, numN;
  8. int t[maxn], deg[maxn], cnt[maxn], sum[maxn], mx[maxn], ppos[maxn], ssum[maxn];
  9. int dpp[maxn], last[maxn];
  10. vector<int> dp[maxn];
  11. bool vis[maxn], hasTree[maxn];
  12. vector<int> G[maxn];
  13. queue<int> que;
  14. int stmax[maxn][Log], mn[maxn];
  15.  
  16. void Init(int n) {
  17.     mn[0] = -1;
  18.     for(int i = 1; i <= n; ++i) {
  19.         mn[i] = ((i & (i - 1)) == 0)? mn[i - 1] + 1: mn[i - 1];
  20.         stmax[i][0] = ssum[i];
  21.     }
  22.     for(int j = 1; j <= mn[n]; ++j) {
  23.         for(int i = 1; i + (1 << j) - 1 <= n; ++i) {
  24.             stmax[i][j] = max(stmax[i][j - 1], stmax[i + (1 << (j - 1))][j - 1]);
  25.         }
  26.     }
  27. }
  28.  
  29. int rmqMax(int L, int R) {
  30.     int k = mn[R - L + 1];
  31.     return max(stmax[L][k], stmax[R - (1 << k) + 1][k]);
  32. }
  33.  
  34. bool cmp(int a, int b) {
  35.     return a > b;
  36. }
  37.  
  38. void dfs(int root, int fa) {
  39.     ppos[++numN] = root;
  40.     vis[root] = true;
  41.     for (int pos : G[root]) {
  42.         if (pos != fa && !vis[pos]) {
  43.             dfs(pos, root);
  44.         }
  45.     }
  46. }
  47.  
  48. int main() {
  49. #ifdef ExRoc
  50.     freopen("test.txt", "r", stdin);
  51. #endif // ExRoc
  52.     ios::sync_with_stdio(false);
  53.  
  54.     cin >> n;
  55.     for (int i = 1; i <= n; ++i) {
  56.         cin >> t[i];
  57.     }
  58.     for (int i = 1; i <= n; ++i) {
  59.         cin >> u >> v;
  60.         G[u].push_back(v);
  61.         G[v].push_back(u);
  62.         ++deg[u];
  63.         ++deg[v];
  64.     }
  65.     for (int i = 1; i <= n; ++i) {
  66.         if (deg[i] == 1) {
  67.             --deg[i];
  68.             que.push(i);
  69.             vis[i] = true;
  70.             dpp[i] = t[i];
  71.         }
  72.     }
  73.     while (!que.empty()) {
  74.         int tmp = que.front();
  75.         que.pop();
  76.         for (int pos : G[tmp]) {
  77.             if (vis[pos]) {
  78.                 continue;
  79.             }
  80.             dp[pos].push_back(dpp[tmp]);
  81.             hasTree[pos] = true;
  82.             --deg[pos];
  83.             if (deg[pos] == 1) {
  84.                 que.push(pos);
  85.                 vis[pos] = true;
  86.                 sort(dp[pos].begin(), dp[pos].end(), cmp);
  87.                 if (dp[pos].size() >= 2) {
  88.                     ans = max(ans, t[pos] + dp[pos][0] + dp[pos][1]);
  89.                 }
  90.                 dpp[pos] = t[pos] + dp[pos][0];
  91.             }
  92.         }
  93.     }
  94.     int loopSum = 0;
  95.     for (int i = 1; i <= n; ++i) {
  96.         if (!vis[i]) {
  97.             loopSum += t[i];
  98.         }
  99.     }
  100.     for (int i = 1; i <= n; ++i) {
  101.         if (!vis[i]) {
  102.             sort(dp[i].begin(), dp[i].end(), cmp);
  103.             if (dp[i].size() > 0) {
  104.                 ans = max(ans, dp[i][0] + loopSum);
  105.                 mx[i] = dp[i][0];
  106.             }
  107.             if (dp[i].size() > 1) {
  108.                 ans = max(ans, dp[i][1] + dp[i][0] + loopSum);
  109.             }
  110.         }
  111.     }
  112.     for (int i = 1; i <= n; ++i) {
  113.         if (!vis[i]) {
  114.             dfs(i, i);
  115.         }
  116.     }
  117.     for (int i = 0; i < numN; ++i) {
  118.         ppos[i + numN + 1] = ppos[i + 1];
  119.     }
  120.     int sumTmp = 0;
  121.     for (int i = 1; i <= numN * 2; ++i) {
  122.         sumTmp += t[ppos[i]];
  123.         ssum[i] = sumTmp + mx[ppos[i]];
  124.     }
  125.     Init(numN * 2);
  126.     int lastHasTree = 0;
  127.     for (int i = 1; i <= numN * 2; ++i) {
  128.         if (hasTree[ppos[i]]) {
  129.             lastHasTree = i;
  130.         }
  131.         last[i] = lastHasTree;
  132.     }
  133.     sumTmp = 0;
  134.     for (int i = 1; i <= numN; ++i) {
  135.         sumTmp += t[ppos[i]];
  136.         if (hasTree[i]) {
  137.             lastHasTree = last[i + numN - 1];
  138.             if (lastHasTree != i) {
  139.                 ans = max(ans, t[ppos[i]] + mx[ppos[i]] + rmqMax(i + 1, lastHasTree) - sumTmp);
  140.             }
  141.         }
  142.     }
  143.     cout << ans << endl;
  144.  
  145.     return 0;
  146. }
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement