CF1681F Unique Occurrences
- 题意:一棵树,问每条路径上只出现一次的值的个数的和。
- 思路:
显然想到考虑边贡献。每条边权下放到下面的哪个点。\(up_i\)为上面第一个点权等于它的点。我们需要一个子树内点权等于它的点(如果满足祖孙关系,不要孙)(除它自己的)sz和。
这样每个点的\(sz\)向\(up\)贡献。
这样差分求出上面的点和下面的点方案数。乘一下就好了 - code:
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
typedef long long ll;
int up[N],n,sz[N],fa[N],lst[N],sum[N],s1[N],nxt[N],to[N],head[N],len[N],ecnt;
void add_edge(int u,int v,int w) {
nxt[++ecnt]=head[u];to[ecnt]=v;len[ecnt]=w;head[u]=ecnt;
nxt[++ecnt]=head[v];to[ecnt]=u;len[ecnt]=w;head[v]=ecnt;
}
void init(int u) {
sz[u]=1;
for(int i=head[u];i;i=nxt[i]) {
int v=to[i];if(v==fa[u])continue;
fa[v]=u;init(v);sz[u]+=sz[v];
}
}
void init_(int u) {
for(int i=head[u];i;i=nxt[i]) {
int v=to[i],w=len[i],pre=lst[w];if(v==fa[u])continue;
if(pre)sum[pre]+=sz[v];
else s1[w]+=sz[v];
up[v]=pre;lst[w]=v;
init_(v);
lst[w]=pre;
}
}
ll ans;
void init__(int u) {
for(int i=head[u];i;i=nxt[i]) {
int v=to[i],w=len[i];if(v==fa[u])continue;
int y=up[v];
ll w1,w2=sz[v]-sum[v];
if(!y) {w1=(sz[1]-sz[v])-(s1[w]-sz[v]);}
else {w1=(sz[y]-sz[v])-(sum[y]-sz[v]);}
// printf("(%d-%d) w1=%lld w2=%lld\n",u,v,w1,w2);
ans+=w1*w2;
init__(v);
}
}
int main() {
scanf("%d",&n);
for(int i=1;i<n;i++) {int u,v,w;scanf("%d%d%d",&u,&v,&w);add_edge(u,v,w);}
init(1),init_(1),init__(1);
// for(int i=1;i<=n;i++)printf("i=%d sum=%lld s1=%lld\n",i,sum[i],s1[i]);
printf("%lld",ans);
return 0;
}
CF1681F Unique Occurrences的更多相关文章
- kettle用mysql创建资源库执行sql代码报错
一.原因: sql语句里边使用 'Y' 'N' 给boolean类型的赋值产生sql失败 二.解决方法:将insert语句中‘Y’或‘N’ 改成TRUE或FALSE即可,共两张表3个地方 ...
- 【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 ...
- LeetCode.1207-唯一的元素出现次数(Unique Number of Occurrences)
这是小川的第次更新,第篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第269题(顺位题号是1207).给定一个整数数组arr,当且仅当该数组中每个元素的出现次数唯一时,返回tr ...
- 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 ...
- [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写
A string such as "word" contains the following abbreviations: ["word", "1or ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- Emscripten教程之代码可移植性与限制(一)
Emscripten教程之代码可移植性与限制(一) 翻译:云荒杯倾本文是Emscripten-WebAssembly专栏系列文章之一,更多文章请查看专栏.也可以去作者的博客阅读文章.欢迎加入Wasm和 ...
- 两个echarts地图联动高亮区域
项目要求左右两张地图能够在鼠标悬浮的时候高亮部分联动,曾尝试了connect不好使,所以自己写了这段代码.代码思路为: 鼠标移入地图时,另一侧的地图根据鼠标悬浮获取到的区域name使该区域高亮: 鼠标 ...
- 【Android开发】APP桌面角标问题
Demo:https://github.com/baitutang1221/BadgeNumberManager 参考:https://juejin.im/post/59f2e59751882578c ...
- CentOS安装图形界面以及eclipse的安装
图形界面的安装,以GNOME为例: 1.首先运行命令:yum grouplist 会显示可安装的包,可以自己选择安装. 2.运行 yum gruopinstall "GNOME" ...
- 数组 indexOf()
众所周知,indexOf()这个方法经常出现在字符串的使用中,也许是用来寻找字符串中某一字符在字符串中的位置,或者也可以用来寻找字符串中重复出现的字符有哪些.对于刚接触 JS 的我们来说,在对数组的操 ...
- 适配手机端rpx像素
<script src="static/js/adaptive.js"></script> <script type="text/javas ...
- C++---使用类
运算符重载 概念 运算符重载就是想法转换, 目的是简化函数调用的方式 重载就是赋予新的含义, 运算符重载也是, 即同一个运算符可以有不同的功能 C++本身已经对一些运算符进行了重载, 同时C++允许程 ...
- /dev/dm-0 ....(/dev/mapper机制)
查看/dev下时候,会看到dm-xx的设备,那么这些设备到底是什么设备呢, Device Mapper 是 Linux 2.6 内核中提供的一种从逻辑设备到物理设备的映射框架机制, 在该机制下,用户可 ...
- FinClip 黑客马拉松正式开赛,码力集结,等你来战!
从2017到2022,小程序已经走过了5年的光景.从无人问津到互联网巨头纷纷入局,短短数年间,小程序已然发展成为超级 App 的标配!微信.支付宝.百度.抖音.今日头条--这些超级app的背后都有巨量 ...
- LAN交换机自学习算法
LAN交换机自学习算法 提示 第二层交完全忽略帧的数据部分协议,仅根据第二层以太网的MAC地址做出转发决策. MAC地址表有时又被称作内容可编址内存(CAM)表 检查源MAC地址 如果源MAC地址不存 ...