• 题意:一棵树,问每条路径上只出现一次的值的个数的和。
  • 思路:

    显然想到考虑边贡献。每条边权下放到下面的哪个点。\(up_i\)为上面第一个点权等于它的点。我们需要一个子树内点权等于它的点(如果满足祖孙关系,不要孙)(除它自己的)sz和。

    这样每个点的\(sz\)向\(up\)贡献。

    这样差分求出上面的点和下面的点方案数。乘一下就好了
  • code:
点击查看代码
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N=1e6+5;
  4. typedef long long ll;
  5. int up[N],n,sz[N],fa[N],lst[N],sum[N],s1[N],nxt[N],to[N],head[N],len[N],ecnt;
  6. void add_edge(int u,int v,int w) {
  7. nxt[++ecnt]=head[u];to[ecnt]=v;len[ecnt]=w;head[u]=ecnt;
  8. nxt[++ecnt]=head[v];to[ecnt]=u;len[ecnt]=w;head[v]=ecnt;
  9. }
  10. void init(int u) {
  11. sz[u]=1;
  12. for(int i=head[u];i;i=nxt[i]) {
  13. int v=to[i];if(v==fa[u])continue;
  14. fa[v]=u;init(v);sz[u]+=sz[v];
  15. }
  16. }
  17. void init_(int u) {
  18. for(int i=head[u];i;i=nxt[i]) {
  19. int v=to[i],w=len[i],pre=lst[w];if(v==fa[u])continue;
  20. if(pre)sum[pre]+=sz[v];
  21. else s1[w]+=sz[v];
  22. up[v]=pre;lst[w]=v;
  23. init_(v);
  24. lst[w]=pre;
  25. }
  26. }
  27. ll ans;
  28. void init__(int u) {
  29. for(int i=head[u];i;i=nxt[i]) {
  30. int v=to[i],w=len[i];if(v==fa[u])continue;
  31. int y=up[v];
  32. ll w1,w2=sz[v]-sum[v];
  33. if(!y) {w1=(sz[1]-sz[v])-(s1[w]-sz[v]);}
  34. else {w1=(sz[y]-sz[v])-(sum[y]-sz[v]);}
  35. // printf("(%d-%d) w1=%lld w2=%lld\n",u,v,w1,w2);
  36. ans+=w1*w2;
  37. init__(v);
  38. }
  39. }
  40. int main() {
  41. scanf("%d",&n);
  42. for(int i=1;i<n;i++) {int u,v,w;scanf("%d%d%d",&u,&v,&w);add_edge(u,v,w);}
  43. init(1),init_(1),init__(1);
  44. // for(int i=1;i<=n;i++)printf("i=%d sum=%lld s1=%lld\n",i,sum[i],s1[i]);
  45. printf("%lld",ans);
  46. return 0;
  47. }

CF1681F Unique Occurrences的更多相关文章

  1. kettle用mysql创建资源库执行sql代码报错

    一.原因:  sql语句里边使用 'Y' 'N'  给boolean类型的赋值产生sql失败    二.解决方法:将insert语句中‘Y’或‘N’ 改成TRUE或FALSE即可,共两张表3个地方  ...

  2. 【leetcode】1207. Unique Number of Occurrences

    题目如下: Given an array of integers arr, write a function that returns true if and only if the number o ...

  3. LeetCode.1207-唯一的元素出现次数(Unique Number of Occurrences)

    这是小川的第次更新,第篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第269题(顺位题号是1207).给定一个整数数组arr,当且仅当该数组中每个元素的出现次数唯一时,返回tr ...

  4. Ruby: Count unique elements and their occurences in an array

    Is there a method in Ruby that takes an array, and counts all unique elements and their occurrences ...

  5. [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  6. [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写

    A string such as "word" contains the following abbreviations: ["word", "1or ...

  7. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  8. [LeetCode] Unique Word Abbreviation 独特的单词缩写

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

  9. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

随机推荐

  1. 从0搭建vue后台管理项目到颈椎病康复指南(一)

    网上搜索了很久Vue项目搭建指南,并没有找到写的比较符合心意的,所以打算自己撸一个指南,集合众家之所长(不善于排版,有点逼死强迫症,如果觉得写的有问题,可以留言斧正,觉得写的太差的,可以留言哪里差, ...

  2. 人机交互大作业---C#WinForm酒店预订系统(纯界面)

    登录: 所有界面: 源代码:最近较忙,后续会上传至github 材料参考:扬中菲尔斯金陵大酒店

  3. 一个动态波浪纹Android界面

    IndexActivity.java package com.example.rubikrobot; import androidx.appcompat.app.AppCompatActivity; ...

  4. Blazor组件自做七 : 使用JS隔离制作定位/持续定位组件

    1. 运行截图 演示地址 2. 在文件夹wwwroot/lib,添加geolocation子文件夹,添加geolocation.js文件 本组件主要是调用浏览器两个API实现基于浏览器的定位功能,现代 ...

  5. Java类型转换详解

    Java类型转换详解 最近有同学问:自动类型转换老是记不住,到底是大转小,还是小转大 其实这个不用死记硬背,很好理解,我们拿 int 和 short 来举例: int 是 4 字节,也就是 32 bi ...

  6. Spring Boot-场景启动器

    分析上文快速入门 1.查看pom文件导入的依赖(starter的父项目) <parent> <artifactId>spring-boot-starter-parent< ...

  7. 20220406Java

    记个笔记 字符串操作类中s1.compareTo(s)规则: Compares two strings lexicographically. The comparison is based on th ...

  8. css加载动画(纯css和html)

    从jq官网上摘抄下来的一个简单加载动画,个人比较喜欢使用~存在这里,作为记录 话不多说~上代码 <!DOCTYPE html> <html lang="en"&g ...

  9. 小程序监听-data 或者 子组件properties 数据

      observers: {     'plateInfo': (obj) => {       console.log('监听', obj)       if(Object.keys(obj) ...

  10. 杭电2091空心三角形Java(AC)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2091 把三角形写入二维数组里,然后输出出来 注意事项: 1.三角形后面没有空格(每一层的后面) 2.三角形 ...