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\)为根的子树 ...
随机推荐
- python3--算法基础:二分查找/折半查找
算法基础:二分查找/折半查找 #!/usr/bin/env python # -*- coding:utf-8 -*- # 算法基础:二分查找/折半查找 def binarySearch(dataSo ...
- 局部二值模式(Local Binary Patterns)纹理灰度与旋转不变性
Multiresolution Gray Scale and Rotation Invariant Texture Classification with Local Binary Patterns, ...
- ms sqlserver数据库建索引
索引分类:从物理结构上可分为两种:聚集索引和非聚集索引 (此外还有空间索引.筛选索引.XML索引) 因为聚集索引是索引顺序与物理存储顺序一致,所以只能建一个. 聚集索引就是把数据按主键顺序存储: 因为 ...
- BZOJ4580: [Usaco2016 Open]248
n<=248个数字,可以进行这样的操作:将相邻两个相同的数字合并成这个数字+1,求最大能合成多少. f(i,j)--区间i到j能合成的最大值,f(i,j)=max(f(i,k)+1),f(i,k ...
- msp430项目编程12
msp430中项目---温度检测系统 1.ds18b20工作原理 2.电路原理说明 3.代码(显示部分) 4.代码(功能实现) 5.项目总结 msp430项目编程 msp430入门学习
- jxls使用模版导出Excel
/** * 使用模版导出Excel */ @SuppressWarnings({ "unchecked", "deprecation" } ...
- python学习之-- shutil模块
shutil 模块功能:文件/文件夹的复制,压缩处理模块shutil.copyfileobj(fsrc,fdst[,length]):将文件内容拷贝到另一个文件中,也可以是部分内容举例:文件复制 im ...
- BZOJ——2697: 特技飞行
http://www.lydsy.com/JudgeOnline/problem.php?id=2697 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: ...
- iOS开发--URL中汉字出现乱码
NSURL *nurl=[[NSURL alloc] initWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF ...
- coco2d-js demo程序之滚动的小球
近期有一个游戏叫围住神经猫,报道说是使用html5技术来做的. html5的跨平台的优良特性非常不错.对于人手不足,技术不足,选用html5技术实现跨平台的梦想真是不错. 近期在看coco2d-js这 ...