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\)为根的子树 ...
随机推荐
- [codeforces538E]Demiurges Play Again
[codeforces538E]Demiurges Play Again 试题描述 Demiurges Shambambukli and Mazukta love to watch the games ...
- NOIP2009T3最优贸易(Dfs + spfa)
洛谷传送门 看到这个题,原本想先从后往前dfs,求出能到终点的点,再在这些点里从前往后spfa,用一条边上的两个城市的商品价格的差来作边权,实施过后,发现图中既有负边权,又有回路,以及各种奇奇怪怪的东 ...
- POJ3321Apple Tree【dfs 树状数组】
题目大意:一棵树(不一定是二叉树!!),树的节点上本来都有一个苹果,要求完成以下操作: 1.指定某个节点,如果这个节点原本有苹果则拿去,如果没有苹果则填上一个苹果 2.询问某个节点以及其子树一共有多少 ...
- UML的关联(Association), 聚合(Aggregation), 组合(Composition)区别
转载:http://blog.csdn.net/ocean181/article/details/6117369 UML的关联(Association), 聚合(Aggregation), 组合(Co ...
- msp430项目编程31
msp430中项目---无线通信系统31 1.SPI工作原理 2.nrf24l01工作原理 3.代码(显示部分) 4.代码(功能实现) 5.项目总结
- EclipseEE的Web开发环境配置(使用Tomcat作为Web服务器)
进行JavaWeb开发,我们总共需要5个步骤:JDK的安装与配置:Tomcat的安装:EclipseEE的安装与配置:创建工程;编写代码并运行.安装的三个软件在版本和适用构架上要一致.当JDK是32位 ...
- python绘图入门
python绘图入门 学习了:https://zhuanlan.zhihu.com/p/34200452 API:https://matplotlib.org/api/pyplot_api.html ...
- [RxJS] Implement RxJS `mergeMap` through inner Observables to Subscribe and Pass Values Through
Understanding sources and subscribers makes it much easier to understand what's going on with mergeM ...
- ubuntu下安装pycharm的方法
linux下安装pycharm是比较麻烦的. 安装pycharm之前要安装好JDK8,依次执行如下的命令: sudo add-apt-repository ppa:webupd8team/java s ...
- A7139 无线通信驱动(STM32) 添加FIFO扩展模式,能够发送超大数据包
A7139 拥有电磁波唤醒以及10mW的发射功率,很easy实现长距离通信,眼下測试有障碍物能够轻松达到300m以上. 通过几天的调试,眼下能够发送随意大小的数据包,大小为1-16KB.所有使用中断收 ...