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 ...
随机推荐
- 在TypeScript项目中进行BDD测试
在TypeScript项目中进行BDD测试 什么是BDD? BDD(Behavior-Driven Design)是软件团队的一种工作方式,通过以下方式缩小业务人员和技术人员之间的差距: 鼓励跨角色协 ...
- 人机交互大作业--flash嵌入web(纯界面)
界面: 源代码:最近较忙,后续会上传至github
- 【Android开发】URL 转义与反转义
1,转义 @org.junit.Test public void testEncode(){ String url="http://192.168.0.19:8888/cas/login&q ...
- Mybatis 多表实现多对一查询、添加操作
Mybatis 多表实现多对一查询.添加操作 学习内容: 1. 多对一之添加操作 1.1.需求 1.2.数据库表(多对一或一对多,数据库外键都是设置在多的一方) 1.3.类设计 1.4.Mapper ...
- MySQL中MyISAM和InnoDB引擎的区别
区别: 1. InnoDB支持事务,MyISAM不支持,对于InnoDB每一条SQL语言都默认封装成事务,自动提交,这样会影响速度,所以最好把多条SQL语言放在begin和commit之间,组成一个事 ...
- 使用flex的同时设置超出喜爱是省略号,
超出宽度,显示省略号 overflow:hidden; white-space:nowrap; text-overflow:ellipsis; 需要注意的是,在移动端在flex元素中的内容进行省略文字 ...
- 《头号玩家》AI电影调研报告(五)
4.VR自由行走跑步机 电影中,它可以让玩家朝任意方向无限奔跑并保持在平台最中央,还可以模拟上台阶和走斜坡的情况.而下面这件VR跑步装备,可以让你在游戏世界穿行自如. 奥地利创意公司Cyberith公 ...
- Oracle集群 & Grid(rac)配置,反推创建过程(重要)。
目前机器上,oracle都是安装好的,那么我们怎么知道,之前的安装过程大概是什么样子呢? 大致安装oracle集群的内容: 一.准备和配置: 1.网卡 2.ip资源 3.scanip 4.hosts ...
- 重定向管道流读取TXT文本第一次读取为""空字符串、type xxx.txt | go run . 报错、BOM头、[239,186,191] 字节数组
重定向管道流读取TXT文本第一次读取为""空字符串.type xxx.txt | go run . 报错.BOM头.[239 186 191] 字节数组
- 安卓记账本开发学习day2
今天遇到了xml文件报错,Wrong orientation? No orientation specified, and the default is horizontal, yet this la ...