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 ...
随机推荐
- 从0搭建vue后台管理项目到颈椎病康复指南(一)
网上搜索了很久Vue项目搭建指南,并没有找到写的比较符合心意的,所以打算自己撸一个指南,集合众家之所长(不善于排版,有点逼死强迫症,如果觉得写的有问题,可以留言斧正,觉得写的太差的,可以留言哪里差, ...
- 人机交互大作业---C#WinForm酒店预订系统(纯界面)
登录: 所有界面: 源代码:最近较忙,后续会上传至github 材料参考:扬中菲尔斯金陵大酒店
- 一个动态波浪纹Android界面
IndexActivity.java package com.example.rubikrobot; import androidx.appcompat.app.AppCompatActivity; ...
- Blazor组件自做七 : 使用JS隔离制作定位/持续定位组件
1. 运行截图 演示地址 2. 在文件夹wwwroot/lib,添加geolocation子文件夹,添加geolocation.js文件 本组件主要是调用浏览器两个API实现基于浏览器的定位功能,现代 ...
- Java类型转换详解
Java类型转换详解 最近有同学问:自动类型转换老是记不住,到底是大转小,还是小转大 其实这个不用死记硬背,很好理解,我们拿 int 和 short 来举例: int 是 4 字节,也就是 32 bi ...
- Spring Boot-场景启动器
分析上文快速入门 1.查看pom文件导入的依赖(starter的父项目) <parent> <artifactId>spring-boot-starter-parent< ...
- 20220406Java
记个笔记 字符串操作类中s1.compareTo(s)规则: Compares two strings lexicographically. The comparison is based on th ...
- css加载动画(纯css和html)
从jq官网上摘抄下来的一个简单加载动画,个人比较喜欢使用~存在这里,作为记录 话不多说~上代码 <!DOCTYPE html> <html lang="en"&g ...
- 小程序监听-data 或者 子组件properties 数据
observers: { 'plateInfo': (obj) => { console.log('监听', obj) if(Object.keys(obj) ...
- 杭电2091空心三角形Java(AC)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2091 把三角形写入二维数组里,然后输出出来 注意事项: 1.三角形后面没有空格(每一层的后面) 2.三角形 ...