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 ...
随机推荐
- 用SimpleDateFormat求出哪天是星期几,如2008-11-11
题目5: 巧妙利用SimpleDateFormat求出: 2008-11-11是星期几?import java.text.ParseException;import java.text.SimpleD ...
- Tomcat安装(安装版)
安装Tomcat(安装版) 下载地址https://tomcat.apache.org/ 下载成功,双击进行安装(一路Next). 等待安装结束. 然后打开浏览器输入地址:http://localho ...
- js判断是否为电话号码
/* 用途:检查输入字符串是否符合国内固话或者传真格式 输入: s:字符串 格式例如:030-4557777返回: 如果通过验证返回true,否则返回false */ function isTel( ...
- 自定义API(Jar包)的创建与使用(简单实现案例)
@ 目录 学习内容 1. 自定义工具类 2. 导出jar 3. 加载Jar包 4. 调用自定义的API方法 总结 学习内容 1. 自定义工具类 新建一个java项目,然后创建包和工具类StringUt ...
- 多条命令同时执行的包concurrently
npm i concurrently use "script":{ "client:build": "webpack --config build/w ...
- Python 图_系列之基于<链接表>实现无向图最短路径搜索
图的常用存储方式有 2 种: 邻接炬阵 链接表 邻接炬阵的优点和缺点都很明显.优点是简单.易理解,对于大部分图结构而言,都是稀疏的,使用炬阵存储空间浪费就较大. 链接表的存储相比较邻接炬阵,使用起来更 ...
- python---希尔排序的实现
def shell_sort(alist): """希尔排序""" n = len(alist) gap = n // 2 # 插入算法执行 ...
- Mysql各版本号的含义
1)MySQL Community Server 社区版本,开源免费,但不提供官方技术支持2)MySQL Enterprise Edition企业版本,需付费,可以试用30天3)MySQL Clust ...
- 想减少代码量,快设置一个有感知的 Aware Spring Bean
摘要:正常情况下,Spring 中的 Bean 对 Spring 是无感知的,Spring 框架提供了这种扩展能力,能让一个 bean 成为有感知的. 本文分享自华为云社区<有感知的 Aware ...
- Linux 开启远程访问
系统:Ubuntu16.0 虚拟机 问题:Windows访问Ubun被拒绝 解决方法: 1.先检测是否安装SSH服务 1 sudo apt-get install ssh 2.编辑ssh配置文件 1 ...