7 条题解

  • 4
    @ 2026-10-9 14:45:52

    【模板】线段树分治

    考虑在一个不存在边权 vv 的图上,一条边边权为 vv 的 x→yx\rightarrow y 贡献显然为 sizx×sizysiz_x\times siz_y。

    做完了,直接上线段树分治处理出所有不存在边权 vv 的图。

    • @ 2026-10-9 15:54:05

      log⁡2\log^2 做法太区了 /咦

  • 4
    @ 2026-10-9 14:45:43

    考虑每种颜色 xx 建出一棵虚树 TxT_x ,记 f(u,0/1)f(u,0/1) 表示子树 uu 内延伸下去的,有 0/10/1 个颜色 xx 的方案数。转移随便做线性。

    那么如果你会线性建虚树的话,总时间复杂度也是 O(n)O(n) 的。

    • -1
      @ 2026-10-9 9:08:19

      • -3
        @ 2026-10-9 12:03:12

        和 T1 一样,考虑拆每个边权的答案贡献

        固定边权 cc,删掉树上所有边权为 cc 的边。剩下的每个连通块缩成一个点,被删掉的边就成为缩点树上的边

        两个点之间的原树路径上,边权 cc 出现的次数,就是对应缩点树上的路径长度。因此边权 cc 在路径上恰好出现一次,当且仅当两端所在的连通块在缩点树上相邻

        对于缩点树上相邻的两个连通块 A,BA,B,一端在 AA、另一端在 BB 的点对有 ∣A∣∣B∣|A||B| 个。因此边权 cc 的贡献为

        ∑(A,B) 是缩点树上的一条边∣A∣∣B∣\sum_{(A,B)\text{ 是缩点树上的一条边}} |A||B|

        最后把所有边权的贡献加起来即可


        把原树以 11 为根。每条非根节点 xx 都对应一条父边,记这条边的边权为 wxw_x

        固定边权 cc,把所有满足 wx=cw_x=c 的节点 xx 看成切边根。对每个切边根 xx,找到它最近的、边权同样为 cc 的祖先 pxp_x;如果没有这样的祖先,就令 px=0p_x=0

        删除所有边权为 cc 的边后:

        • 切边根 xx 对应的连通块,是 xx 的子树去掉所有最近切边后代的子树
        • 如果 px≠0p_x\ne 0,父边 (fa(x),x)(fa(x),x) 连接 xx 对应的连通块和 pxp_x 对应的连通块
        • 如果 px=0p_x=0,这条父边连接 xx 对应的连通块和根所在的连通块

        设 sz(x)sz(x) 是 xx 的子树大小,那么 xx 对应的连通块大小为

        f(x)=sz(x)−∑py=xsz(y)f(x)=sz(x)-\sum_{p_y=x}sz(y)

        边权为 cc 时,根所在的连通块大小为

        fc(0)=n−∑wy=c, py=0sz(y)f_c(0)=n-\sum_{w_y=c,\ p_y=0}sz(y)

        所以,父边 (fa(x),x)(fa(x),x) 对答案的贡献为

        $$f(x) \begin{cases} f(p_x),&p_x\ne 0\\ f_c(0),&p_x=0 \end{cases} $$

        每条原树边都对应一个非根节点的父边,遍历所有非根节点累加贡献就可以得到答案


        接下来只需要快速找到每个节点最近的同边权祖先

        先对树做 DFS,记录每个点的 DFS 序 dfn 和子树大小 subsz。一个点 xx 的子树在 DFS 序中是一段连续区间

        [dfn(x),dfn(x)+subsz(x)−1][dfn(x),dfn(x)+subsz(x)-1]

        按 DFS 序遍历节点时,对每个边权维护一个祖先栈。处理非根节点 xx 时:

        1. 查看边权 wxw_x 对应的栈顶
        2. 如果栈顶的子树区间不包含 xx,说明它不是 xx 的祖先,就弹出
        3. 弹到栈顶是 xx 的祖先为止;此时栈顶就是最近的同边权祖先,栈空则不存在
        4. 把 xx 放入对应的栈

        每个节点最多进栈、出栈一次,因此找祖先是 O(n)O(n) 的

        整个过程都是 O(n)O(n),所以时间是 O(n)O(n),空间也是 O(n)O(n)

        • -3
          @ 2026-10-9 9:08:35

          • -3
            @ 2026-10-9 9:07:42

            • -12
              @ 2026-10-9 12:03:18

              \點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/\點分治/

              點分治輕鬆通過!

              
              #include <bits/stdc++.h>
              
              using namespace std;
              using ll = long long;
              
              const int N = 5e5 + 3;
              
              struct Edge {
                int x, c;
              };
              
              int n, siz[N], dep[N], now[N];
              ll ans, sum2[N], sum3[N], Total1, totalP;
              bool del[N];
              
              vector<Edge> g[N];
              vector<Edge> path;
              
              void S (int x, int fa, int col) {
                if (col) path.push_back ({x, col});
                siz[x] = 1, dep[x] = dep[fa] + 1;
                for (auto to : g[x]) {
                  if (to.x ^ fa && !del[to.x]) {
                    S (to.x, x, to.c);
                    siz[x] += siz[to.x];
                  }
                }
              }
              
              int Heavy (int x) {
                S (x, 0, 0);
                int p = x;
                for (auto i : path) {
                  if (siz[i.x] > siz[x] / 2 && siz[i.x] < siz[p]) {
                    p = i.x;
                  }
                }
                path.clear ();
                return p;
              }
              
              ll single_cnt, single_sum2, single_sum3, all_sum2;
              
              void dfs1 (int x, int fa, int col) {
                if (col) {
                  int c = col;
                  if (now[c] == 0) {
                    now[c] = 1;
                    single_cnt++;
                    single_sum2 += sum2[c];
                    single_sum3 += sum3[c];
                    all_sum2 += sum2[c];
                  } else if (now[c] == 1) {
                    now[c] = 2;
                    single_cnt--;
                    single_sum2 -= sum2[c];
                    single_sum3 -= sum3[c];
                  } else {
                    now[c]++;
                  }
                }
                ans += single_cnt * totalP - single_sum2 - single_sum3 + Total1 - all_sum2;
                for (auto to : g[x]) {
                  if (to.x ^ fa && !del[to.x]) {
                    dfs1 (to.x, x, to.c);
                  }
                }
                if (col) {
                  int c = col;
                  if (now[c] == 1) {
                    now[c] = 0;
                    single_cnt--;
                    single_sum2 -= sum2[c];
                    single_sum3 -= sum3[c];
                    all_sum2 -= sum2[c];
                  } else if (now[c] == 2) {
                    now[c] = 1;
                    single_cnt++;
                    single_sum2 += sum2[c];
                    single_sum3 += sum3[c];
                  } else {
                    now[c]--;
                  }
                }
              }
              
              void dfs2 (int x, int fa, int col) {
                ++now[col];
                if (now[col] == 1) {
                  sum2[col] += siz[x];
                  Total1 += siz[x];
                } else if (now[col] == 2) {
                  sum2[col] -= siz[x];
                  Total1 -= siz[x];
                  sum3[col] += siz[x];
                }
                for (auto to : g[x]) {
                  if (to.x ^ fa && !del[to.x]) {
                    dfs2 (to.x, x, to.c);
                  }
                }
                --now[col];
              }
              
              void Calc (int x) {
                totalP = 1;
                Total1 = 0;
                for (auto to : g[x]) {
                  if (!del[to.x]) {
                    S (to.x, x, to.c);
                    single_cnt = single_sum2 = single_sum3 = all_sum2 = 0;
                    dfs1 (to.x, x, to.c);
                    dfs2 (to.x, x, to.c);
                    totalP += siz[to.x];
                    for (auto i : path) now[i.c] = 0;
                    path.clear ();
                  }
                }
                S (x, 0, 0);
                for (auto i : path) {
                  sum2[i.c] = sum3[i.c] = 0;
                }
                path.clear ();
                totalP = Total1 = 0;
              }
              
              void Cute_LRH (int x) {
                int hvy = Heavy (x);
                Calc (hvy), del[hvy] = 1;
                for (auto to : g[hvy]) {
                  if (!del[to.x]) {
                    Cute_LRH (to.x);
                  }
                }
              }
              
              int main () {
                ios :: sync_with_stdio (0), cin.tie (0);
                cin >> n;
                for (int i = 1, x, y, c; i < n; ++i) {
                  cin >> x >> y >> c;
                  g[x].push_back ({y, c});
                  g[y].push_back ({x, c});
                }
                Cute_LRH (1);
                return cout << ans, 0 ^ 0;
              }
              
              • @ 2026-10-9 15:48:26

                \李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/\李茹惠/

              • @ 2026-10-9 15:48:39
                Cute CXM\color{pink}\Huge{\text{Cute CXM}}
            • 1

            信息

            ID
            75
            时间
            6000ms
            内存
            1000MiB
            难度
            5
            标签
            递交数
            37
            已通过
            15
            上传者