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\)为根的子树 ...
随机推荐
- PTA 04-树6 Complete Binary Search Tree (30分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/669 5-7 Complete Binary Search Tree (30分) A ...
- [NOIP2000] 提高组 洛谷P1023 税收与补贴问题
题目背景 每样商品的价格越低,其销量就会相应增大.现已知某种商品的成本及其在若干价位上的销量(产品不会低于成本销售),并假设相邻价位间销量的变化是线性的且在价格高于给定的最高价位后,销量以某固定数值递 ...
- Mysql相关工具
•Mysql相关工具 –Mysqlslap 压力测试工具 –Mysqlsla 日志分析工具 –Mysqlreport 效能监控工具 –Mysqlproxy 快速实现读写分离以及负载均衡 –p ...
- struts2常用类型的Result
2.2.1. dispatcher dispatcher类型是用于转发的Result,可以将请求转发给JSP.这种类型的Result对应的类为 ServletDispatcherResult,它是St ...
- 搭建Spring+mybatis报错
java.lang.ClassCastException: com.sun.proxy.$Proxy12 cannot be cast to com.bdqn.service.impl.UserSer ...
- DATASNAP高效的FIREDAC数据序列和还原
变量定义: varFDConnection: TFDConnection;qCustomers: TFDQuery; qOrders: TFDQuery;FDSchemaAdapter: TFDSch ...
- Meteor部
一个关于 Meteor 主要事项就是如何轻松部署应用程序.当程序完成后,有一个简单的方法来和世界分享你的应用程序.所有需要做的就是在运行命令提示符窗口下面的代码. C:\Users\Administr ...
- CentOS 6.x Inotify+Rsync
CentOS 6.x Inotify+Rsync yum -y install lrzsz [root@rsync ~]# mount -t nfs 10.6.100.75:/volume1/pace ...
- VM Workstation的Unity Mode有什么用
正常情况下,如果我启动了一个VM Workstaion的虚拟机,比如是一个Linux系统,并且没运行任何软件,进入Unity mode之后,我真实系统的左下角会有一个虚拟机的图标 点击这个图标可以打开 ...
- 关键路径(AOE)---《数据结构》严蔚敏
// exam1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include &l ...