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 ...
随机推荐
- 关于根据数据反选checkbox
前两天完成了一个连接hbase数据库的mis系统,mis系统中经常需要修改功能,复选框.多选框等等的自动勾选,感觉很麻烦,在此记录一下修改功能checkbox自动选中. 例子: <div cla ...
- CSS简单样式练习(四)
运行效果: 源代码: 1 <!DOCTYPE html> 2 <html lang="zh"> 3 <head> 4 <meta char ...
- 基于node实现一个简单的脚手架工具(node控制台交互项目)
实现控制台输入输出 实现文件读写操作 全原生实现一个简单的脚手架工具 实现vue-cli2源码 一.实现控制台输入输出 关于控制台的输入输出依然是基于node进程管理对象process,在proces ...
- Linux安装Redis步骤和make遇到的坑
Linux安装Redis服务步骤 1.获取redis资源 cd /usr/local wget https://mirrors.huaweicloud.com/redis/redis-6 ...
- SpringMVC获取请求参数-基本类型
1.Controller中的业务方法的参数名称要与请求参数的name一致,参数值会自动映射匹配 (json形式) <dependency> <groupId>com.faste ...
- zabbix自定义自动发现模板
需求: 自定义发现磁盘io,并实现监控.其他的业务组件自动发现监控其实也和这个大同小异,自动发现主要逻辑就是你要根据组件规则自动匹配出需要监控的所有组件,再通过传参的方式获取对应组件数据. 自动发现无 ...
- Java的虚拟线程(协程)特性开启预览阶段,多线程开发的难度将大大降低
高并发.多线程一直是Java编程中的难点,也是面试题中的要点.Java开发者也一直在尝试使用多线程来解决应用服务器的并发问题.但是多线程并不容易,为此一个新的技术出现了,这就是虚拟线程. 传统多线程的 ...
- 一致性Hash的原理与实现
应用场景 在了解一致性Hash之前,我们先了解一下一致性Hash适用于什么场景,能解决什么问题?这里先放一下我自己认为适用的场景.一致性Hash适用于服务器动态扩展且需要负载均衡的场景 试想以下场景, ...
- Vue基础开发入门之简单语法知识梳理(思维导图详解)
基于个人写的以下关于Vue框架基础学习的三篇随笔,在此基础上,做一个阶段性的知识总结,以此来检验自己对Vue这一段时间学习的成果,内容不多,但很值得一看.(思维导图详解)
- gin框架使用【2.RESTFUL API使用】
package mainimport ( "github.com/gin-gonic/gin")func main() { router := gin.Default() rout ...