1 条题解

  • 1
    @ 2022-9-2 22:09:25
    #include <bits/stdc++.h>
    using namespace std;
    namespace Std {
    int n, m, head[200010][2], nxt[800010], to[800010], cnt, dfn[200010],
        fa[200010], tot, num, f[200010][2], g[200010][2], a[200010];
    bool mark[100010];
    inline void add(int x, int y, bool z) {
      to[++cnt] = y;
      nxt[cnt] = head[x][z];
      head[x][z] = cnt;
    }
    void dfs(int x, int y) {
      dfn[x] = ++tot;
      fa[x] = y;
      for (int i = head[x][0]; i; i = nxt[i]) {
        if (to[i] == y) continue;
        if (!dfn[to[i]]) {
          mark[x] = 0;
          dfs(to[i], x);
          if (!mark[x]) add(x, to[i], 1);
        } else {
          if (dfn[to[i]] > dfn[x]) continue;
          int u = x;
          ++num;
          while (u != to[i]) {
            add(num, u, 1);
            u = fa[u];
            mark[u] = 1;
          }
          add(to[i], num, 1);
        }
      }
    }
    void dp(int x) {
      f[x][1] = a[x];
      for (int i = head[x][1]; i; i = nxt[i]) {
        if (to[i] <= n) {
          dp(to[i]);
          f[x][1] += f[to[i]][0];
          f[x][0] += max(f[to[i]][1], f[to[i]][0]);
        } else {
          int u = to[i], f1, f2, f3, f4;
          for (int j = head[u][1]; j; j = nxt[j]) dp(to[j]);
          tot = 0;
          g[0][0] = f[to[head[u][1]]][0];
          g[0][1] = 0xc3c3c3c3;
          for (int j = head[u][1]; j; j = nxt[j]) {
            if (j == head[u][1]) continue;
            ++tot;
            g[tot][1] = f[to[j]][1] + g[tot - 1][0];
            g[tot][0] = f[to[j]][0] + max(g[tot - 1][0], g[tot - 1][1]);
          }
          f1 = g[tot][0];
          f2 = g[tot][1];
          g[0][0] = 0xc3c3c3c3;
          g[0][1] = f[to[head[u][1]]][1];
          tot = 0;
          for (int j = head[u][1]; j; j = nxt[j]) {
            if (j == head[u][1]) continue;
            ++tot;
            g[tot][1] = f[to[j]][1] + g[tot - 1][0];
            g[tot][0] = f[to[j]][0] + max(g[tot - 1][0], g[tot - 1][1]);
          }
          f3 = g[tot][0];
          f4 = g[tot][1];
          f[x][0] += max(max(f1, f2), max(f3, f4));
          f[x][1] += f1;
        }
      }
    }
    int main() {
      scanf("%d%d", &n, &m);
      num = n;
      int u, v;
      for (int i = 1; i <= m; ++i) {
        scanf("%d%d", &u, &v);
        add(u, v, 0);
        add(v, u, 0);
      }
      for (int i = 1; i <= n; ++i) scanf("%d", a + i);
      dfs(1, 0);
      dp(1);
      printf("%d\n", max(f[1][0], f[1][1]));
      return 0;
    }
    }  // namespace Std
    int main() { return Std::main(); }
    
    • 1

    信息

    ID
    3341
    时间
    1000ms
    内存
    125MiB
    难度
    6
    标签
    递交数
    3
    已通过
    1
    上传者