bzoj 5457 城市
题目大意:
树上每个点有种类$a_i$和数量$b_i$,求每个点的子树内数量最多的种类的数量和这个数量
思路:
显然是线段树合并裸题
学习一下$dsu \space on \space tree$ 主要就是保留重链信息 其余点暴力
多用几个函数
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#define ll long long
#define inf 2139062143
#define MAXN 400100
#define MOD 998244353
#define rep(i,s,t) for(register int i=(s),i##__end=(t);i<=i##__end;++i)
#define dwn(i,s,t) for(register int i=(s),i##__end=(t);i>=i##__end;--i)
#define ren for(register int i=fst[x];i;i=nxt[i])
#define pb(i,x) vec[i].push_back(x)
#define pls(a,b) (a+b)%MOD
#define mns(a,b) (a-b+MOD)%MOD
#define mul(a,b) (1LL*(a)*(b))%MOD
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-;ch=getchar();}
while(isdigit(ch)) {x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,fst[MAXN],nxt[MAXN<<],to[MAXN<<],t[MAXN],val[MAXN];
int sz[MAXN],hvs[MAXN],cnt,num[MAXN],ans[MAXN],res[MAXN],tmp;
void add(int u,int v) {nxt[++cnt]=fst[u],fst[u]=cnt,to[cnt]=v;}
void dfs(int x,int pa)
{
sz[x]=;ren if(to[i]^pa)
{
dfs(to[i],x),sz[x]+=sz[to[i]];
if(sz[to[i]]>sz[hvs[x]]) hvs[x]=to[i];
}
}
void cheq(int &x,int y) {x=(num[y]>num[x]||(num[x]==num[y]&&y<x))?y:x;}
void Get(int x,int pa,int v)
{
num[t[x]]+=v*val[x];if(v>) cheq(tmp,t[x]);
ren if(to[i]^pa) Get(to[i],x,v);
}
void dsu(int x,int pa,int v)
{
ren if(to[i]^pa&&to[i]^hvs[x]) dsu(to[i],x,);
if(hvs[x]) dsu(hvs[x],x,);
ren if(to[i]^pa&&to[i]^hvs[x]) Get(to[i],x,);
num[t[x]]+=val[x];cheq(tmp,t[x]);ans[x]=tmp,res[x]=num[tmp];
if(!v) tmp=,Get(x,pa,-);
}
int main()
{
n=read(),m=read();int a,b;rep(i,,n) a=read(),b=read(),add(a,b),add(b,a);
rep(i,,n) t[i]=read(),val[i]=read();
dfs(,);dsu(,,);rep(i,,n) printf("%d %d\n",ans[i],res[i]);
}
bzoj 5457 城市的更多相关文章
- 线段树合并 || BZOJ 5457: 城市
题面:https://www.lydsy.com/JudgeOnline/problem.php?id=5457 题解: 线段树合并,对于每个节点维护sum(以该节点为根的子树中最大的种类和)和kin ...
- BZOJ #5457: 城市 [线段树合并]
线段树合并的板子题,每次从下到上合并就完事了 // by Isaunoya #include <bits/stdc++.h> using namespace std; #define re ...
- BZOJ:5457: 城市(线段树合并)(尚待优化)
5457: 城市 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 18 Solved: 12[Submit][Status][Discuss] Des ...
- BZOJ 3091: 城市旅行 [LCT splay 期望]
3091: 城市旅行 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1454 Solved: 483[Submit][Status][Discuss ...
- BZOJ 3091 城市旅行
Description Input Output Sample Input 4 5 1 3 2 5 1 2 1 3 2 4 4 2 4 1 2 4 2 3 4 3 1 4 1 4 1 4 Sample ...
- bzoj 3091 城市旅行(LCT+数学分析)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3091 [思路] 膜Popoqqq大爷的题解 click here [代码]是坑... ...
- BZOJ 3091: 城市旅行 lct 期望 splay
https://www.lydsy.com/JudgeOnline/problem.php?id=3091 https://blog.csdn.net/popoqqq/article/details/ ...
- bzoj 3091: 城市旅行 LCT
题目: http://www.lydsy.com/JudgeOnline/problem.php?id=3091 题解: 首先前三个操作就是裸的LCT模板 只考虑第四个操作. 要求我们计算期望,所以我 ...
- [bzoj5457]城市_dsu on tree
bzoj 5457 城市 题目大意 给定一棵以\(1\)为根的\(n\)个节点的有根树. 每个节点有一个民族和该民族在当前节点的人数. 有\(n\)个询问,第\(i\)个询问是求以\(i\)为根的子树 ...
随机推荐
- 简单的发红包的PHP算法
假设有有10元钱 ,发给10个人.保证每个人都有钱拿,最少分得0.01.我们最先想到的肯定就是随机.0.01-10随机.但是会出现第一个人就分得9.99的情况.下面就没人可分了.然后就是我的错误思路 ...
- Java日志实战及解析 - 引导
日志是什么? 程序日志的作用 记录程序运行情况,程序员观察和调试使用 统计分析 日后审计 程序员,开发工程师,架构师等天天打交道. 实际中有4%的代码是日志! http://logging.apach ...
- 【HTML/XML 3】XML 简介,来源
导读:在标记语言出现之前,计算机中的数据一直都是以神秘的二进制在进行处理,但是,标记语言的出现,慢慢的改变了这种认识.那么,标记语言都经过了什么样的发展呢?它又有什么用处呢? 一.追根溯源(XML的产 ...
- CodeForces 20 A+B
A - BerOS file system 水题不解释了,压缩斜杆.要注意最后没有斜杠. char a[105]; ...
- HDU-1041-Computer Transformation,大数递推,水过~~
Computer Transformatio ...
- BZOJ 1221: [HNOI2001] 软件开发【最小费用最大流】
Description 某软件公司正在规划一项n天的软件开发计划,根据开发计划第i天需要ni个软件开发人员,为了提高软件开发人员的效率,公司给软件人员提供了很多的服务,其中一项服务就是要为每个开发人员 ...
- C++常见函数(备忘录)
substr(string的成员函数) 语法: basic_string substr( size_type index, size_type num = npos ); substr()返回本字符串 ...
- 洛谷—— P1133 教主的花园
https://www.luogu.org/problem/show?pid=1133 题目描述 教主有着一个环形的花园,他想在花园周围均匀地种上n棵树,但是教主花园的土壤很特别,每个位置适合种的树都 ...
- xml解析工具mashaller javaee自带解析类
1.怎样去掉Marshaller的格式化? : JAXBContext context = JAXBContext.newInstance(Entity.class); Marshaller mars ...
- 系统优化(一)Maven打包同一个jar有不同的:版本号+时间戳(解决思路)
解决:maven仓库的ear里面有非常多个同样的jar(仅仅是包括不同的:版本号+时间戳) 问题描写叙述: 发现ear里面有非常多个同样的jar,仅仅是包括不同的:版本号+时间戳,例如以下图所看到的: ...