题解 c(留坑)
这题卡常……而且目前还没有卡过去
首先以原树重心为根,向所有子树重心连边,可以建立一棵点分树
点分树有两个性质:
一个是树高只有log层
另一个是两点在点分树上的lca一定在原树上两点间的树上路径上
所以在原树上不断删点,并统计当前子树中的信息就好
至于如何统计,令 \(dp[i][j][k][l]\) 表示分治中心为 \(i\) ,到点 \(j\) ,第一条边颜色为 \(k\) ,最后一条边颜色为 \(j\) 的最大得分
转移挺好写的,询问时暴力枚举相关连边的颜色
留个坑,纯点分治还不会写呢
Code:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 100010
#define ll long long
#define reg register int
#define fir first
#define sec second
#define make make_pair
#define pb push_back
#define min2(a, b) ((a)<(b)?(a):(b))
#define max2(a, b) ((a)>(b)?(a):(b))
//#define int long long
char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
int ans=0, f=1; char c=getchar();
while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
return ans*f;
}
int n, m, q;
int head[N], size;
struct edge{int to, next, val;}e[N*3];
inline void add(int s, int t, int w) {e[++size].to=t; e[size].val=w; e[size].next=head[s]; head[s]=size;}
namespace force{
bool none[N];
int dfs(int u, int to, int fa, int now, int sum) {
//cout<<"dfs "<<u<<' '<<to<<' '<<fa<<' '<<now<<' '<<sum<<endl;
if (u==to) return sum;
int ans=0;
for (int i=head[u],v; ~i; i=e[i].next) {
v = e[i].to;
if (v==fa || none[v]) continue;
ans=max(ans, dfs(v, to, u, e[i].val, sum+(now!=e[i].val)));
}
if (!ans) none[u]=1;
return ans;
}
void solve() {
for (int i=1,u,v,w; i<=m; ++i) {
u=read(); v=read(); w=read();
add(u, v, w); add(v, u, w);
}
q=read();
if (!q) exit(0);
for (int i=1,x,y; i<=q; ++i) {
x=read(); y=read();
memset(none, 0, sizeof(bool)*(n+5));
printf("%d\n", dfs(x, y, 0, 0, 0));
}
exit(0);
}
}
namespace task1{
bool none[N];
int dfs(int u, int to, int fa, int now, int sum, int dis) {
//cout<<"dfs "<<u<<' '<<to<<' '<<fa<<' '<<now<<' '<<sum<<endl;
if (dis>60) {none[u]=1; return 0;}
if (u==to) return sum;
int ans=0;
for (int i=head[u],v; ~i; i=e[i].next) {
v = e[i].to;
if (v==fa || none[v]) continue;
ans=max(ans, dfs(v, to, u, e[i].val, sum+(now!=e[i].val), dis+1));
}
if (!ans) none[u]=1;
return ans;
}
void solve() {
for (int i=1,u,v,w; i<=m; ++i) {
u=read(); v=read(); w=read();
add(u, v, w); add(v, u, w);
}
q=read();
if (!q) exit(0);
for (int i=1,x,y; i<=q; ++i) {
x=read(); y=read();
memset(none, 0, sizeof(bool)*(n+5));
printf("%d\n", dfs(x, y, 0, 0, 0, 0));
}
exit(0);
}
}
namespace task{
int fa[N][25], dep[N], lg[N], rot, siz[N], msiz[N], sumsiz;
bool rm[N];
struct st{int i, j, k; st() {} st(int a, int b, int c):i(a),j(b),k(c){}};
inline bool operator == (st a, st b) {return a.i==b.i&&a.j==b.j&&a.k==b.k;}
inline bool operator < (st a, st b) {return a.k<b.k;}
struct pair_hash{inline size_t operator () (pair<int, int> p) const {return hash<int>()(p.fir*p.sec+p.fir);}};
unordered_map<pair<int, int>, vector<int>*, pair_hash> mp{5000, pair_hash()};
unordered_map<pair<int, int>, vector<st>*, pair_hash> dp{20000, pair_hash()};
unordered_map<pair<int, int>, int, pair_hash> tem{50, pair_hash()};
struct edge{int to, next;}e2[N<<1];
inline void add(int s, int t) {e2[++size].to=t; e2[size].next=head[s]; head[s]=size;}
vector<int> e[N];
void getrt(int u, int fa) {
//cout<<"getrt"<<u<<' '<<fa<<endl;
siz[u]=1; msiz[u]=0;
for (register auto v:e[u]) {
if (!rm[v] && v!=fa) {
getrt(v, u);
siz[u]+=siz[v];
msiz[u]=max(msiz[u], siz[v]);
}
}
//cout<<"sumsiz: "<<sumsiz<<' '<<sumsiz-siz[u]<<' '<<msiz[u]<<' '<<msiz[rot]<<' '<<rot<<endl;
msiz[u]=max(msiz[u], sumsiz-siz[u]);
if (msiz[u]<msiz[rot]) rot=u;
}
void info(int u, int fa) {
//cout<<"info "<<u<<' '<<fa<<endl;
for (register auto v:e[u]) {
if (v==fa || rm[v]) continue;
if (rot==u) {
if (dp.find(make(u, v))==dp.end()) dp[make(u, v)]=new vector<st>;
auto t1=mp[make(u, v)]; auto t2=dp[make(u, v)];
for (register auto it:*t1)
t2->pb(st(it, it, 1));
}
else {
if (dp.find(make(rot, v))==dp.end()) dp[make(rot, v)]=new vector<st>;
for (register auto it:*dp[make(rot, u)])
for (register auto t:*mp[make(u, v)])
tem[make(it.i, t)] = max(tem[make(it.i, t)], it.k+(it.j!=t));
register auto t1=dp[make(rot, v)];
for (register auto it:tem) t1->pb(st(it.fir.fir, it.fir.sec, it.sec));
//assert(tem.size()<=9);
tem.clear();
}
info(v, u);
//cout<<"size: "<<dp.size()<<endl;
}
}
void build(int u) {
//cout<<"build "<<u<<endl;
rm[u]=1;
info(u, 0);
for (register auto v:e[u]) {
rot=0;
if (!rm[v]) {
sumsiz=siz[v];
getrt(v, u);
add(u, rot), add(rot, u);
//info(rot, u, t);
build(rot);
}
}
}
void dfs(int u, int pa) {
//cout<<"dfs "<<u<<' '<<pa<<endl;
for (reg i=1; i<25; ++i)
if (dep[u]>=(1<<i)) fa[u][i] = fa[fa[u][i-1]][i-1];
else break;
for (reg i=head[u],v; ~i; i=e2[i].next) {
v = e2[i].to;
if (v!=pa) dep[v]=dep[u]+1, fa[v][0]=u, dfs(v, u);
}
}
int lca(int a, int b) {
if (dep[a]<dep[b]) swap(a, b);
while (dep[a]>dep[b]) a=fa[a][lg[dep[a]-dep[b]]-1];
if (a==b) return a;
for (reg i=lg[dep[a]]-1; ~i; --i)
if (fa[a][i]!=fa[b][i])
a=fa[a][i], b=fa[b][i];
return fa[a][0];
}
void solve() {
for (reg i=1; i<=n; ++i) lg[i]=lg[i-1]+(1<<lg[i-1]==i);
pair<int, int> t1, t2;
for (reg i=1,u,v,w; i<=m; ++i) {
u=read(); v=read(); w=read();
e[u].pb(v); e[v].pb(u);
t1=make(u, v), t2=make(v, u);
if (mp.find(t1)==mp.end()) mp[t1]=new vector<int>;
mp[t1]->pb(w);
if (mp.find(t2)==mp.end()) mp[t2]=new vector<int>;
mp[t2]->pb(w);
}
for (reg i=1,siz; i<=n; ++i) {
sort(e[i].begin(), e[i].end());
siz=unique(e[i].begin(), e[i].end())-e[i].begin();
e[i].resize(siz);
}
for (register auto it:mp) {
sort(it.sec->begin(), it.sec->end());
int siz=unique(it.sec->begin(), it.sec->end())-it.sec->begin();
it.sec->resize(min2(siz, 3));
}
msiz[0]=sumsiz=n;
getrt(1, 0);
int root=rot;
//cout<<"root: "<<root<<endl;
build(rot);
dep[root]=1;
dfs(root, 0);
//for (auto it:dp) cout<<it.fir.i<<' '<<it.fir.j<<' '<<it.fir.k<<' '<<it.fir.h<<' '<<it.sec<<endl;
q=read(); if (!q) exit(0);
for (reg i=1,x,y,t,ans; i<=q; ++i) {
x=read(); y=read();
if (x==y) {puts("0"); continue;}
t=lca(x, y); ans=0;
//cout<<"lca: "<<x<<' '<<y<<' '<<t<<endl;
if (t==x || t==y) {
//puts("pos1");
if (t!=x) swap(x, y);
for (register auto it:*dp[make(t, y)]) ans=max(ans, it.k);
printf("%d\n", ans);
continue;
}
for (register auto i:*dp[make(t, x)])
for (register auto j:*dp[make(t, y)])
ans = max(ans, i.k+j.k-(i.i==j.i));
printf("%d\n", ans);
}
//cout<<"size: "<<dp.size()<<endl;
exit(0);
}
}
signed main()
{
memset(head, -1, sizeof(head));
n=read(); m=read();
//if (n<100000) force::solve();
//else task1::solve();
task::solve();
return 0;
}
题解 c(留坑)的更多相关文章
- Codeforces Round #364 (Div. 1)(vp) 没什么题解就留坑待填
我就做了前两题,第一题第一次vp就把我搞自闭跑路了,第二题第二次又把我搞自闭了 A. As Fast As Possible 细节题 #include<cstdio> #include&l ...
- [kuangbin带你飞]专题十一 网络流个人题解(L题留坑)
A - ACM Computer Factory 题目描述:某个工厂可以利用P个部件做一台电脑,有N个加工用的机器,但是每一个机器需要特定的部分才能加工,给你P与N,然后是N行描述机器的最大同时加工数 ...
- 题解 queen(留坑)
传送门 博客园突然打不开了,奇奇怪怪的-- 少写个等号没看出来 nm写反了没看出来 考完5min全拍出来了 手残属性加持 不对拍等于爆零 yysy,我连卢卡斯定理的存在都忘了-- 发现要让一大堆皇后能 ...
- 2.18比赛(T2,T3留坑)
2.18比赛(T2,T3留坑) pdf版题面 pdf版题解 超越一切(ak) [题目描述] 夏洛可得到一个(h+1)×(w+1)的巧克力,这意味着她横着最多可 以切 h 刀,竖着最多可以切 w 刀 她 ...
- CPU虚拟化技术(留坑)
留坑~~~ 不知道这个是这么实现的 CPU虚拟化技术就是单CPU模拟多CPU并行,允许一个平台同时运行多个操作系统,并且应用程序都可以在相互独立的空间内运行而互不影响,从而显著提高计算机的工作效率.虚 ...
- 【留坑】uva12299
这么sb的题本来想练练手记过就是过不了 拍半天也没问题 留坑 哪天有空了去linux下面试试 #include<cstdio> #include<cstring> #inclu ...
- 【问题解决方案】Git bash进入多层子目录问题(通配符问题留坑)
cd进入指定路径下:cd 斜杠 斜杠 方法一: 1- 撇丿,不是"那",盘符前面要加上 / (d盘前面也加,不加也行) 2- 路径名不区分大小写 3- 不用空格 4- 如果目录名中 ...
- 题解 Six(留坑)
传送门 考场上搞了个三进制状压,结果正确性假了-- 有想到从约数下手,但觉得就光预处理约数复杂度就爆炸就没往这边想-- 首先是关于约数个数的证明,再一次感谢战神: 因为 \(n = \prod p_i ...
- 培训补坑(day7:线段树的区间修改与运用)(day6是测试,测试题解以后补坑QAQ)
补坑咯~ 今天围绕的是一个神奇的数据结构:线段树.(感觉叫做区间树也挺科学的.) 线段树,顾名思义就是用来查找一段区间内的最大值,最小值,区间和等等元素. 那么这个线段树有什么优势呢? 比如我们要多次 ...
随机推荐
- 315M、433M和2.4G笔记
一.315M无线模块 315m无线模块广泛地运用在车辆监控.遥控.遥测.小型无线网络.无线抄表.门禁系统.小区传呼.工业数据采集系统.无线标签.身份识别.非接触RF智能卡.小型无线数据终端.安全防火系 ...
- DIY一个智能开关kwswitch
源码地址:https://gitee.com/kerwincui/kwswitch 平台简介 该智能开关平台包括服务端.硬件端.PC端和安卓端.硬件使用ESP8266模块,成本相对较低,可以发挥想象力 ...
- postgresql安装及配置
目录 1. 安装 2. PostgrepSQL的简单配置 2.1 修改监听的ip和端口 2.2 修改数据库log相关的参数 2.3 内存参数 3. 数据库的基础操作 3.1 连接数据库控制台 3.2 ...
- 「CF85E」 Guard Towers
「CF85E」 Guard Towers 模拟赛考了这题的加强版 然后我因为初值问题直接炸飞 题目大意: 给你二维平面上的 \(n\) 个整点,你需要将它们平均分成两组,使得每组内任意两点间的曼哈顿距 ...
- Anaconda3中的python安装新模块
1.确认安装位置:D:\Anaconda3 2.进入: D:\Anaconda3\Scripts 3.pip install -i https://pypi.tuna.tsinghua.edu.cn/ ...
- pytorch中网络特征图(feture map)、卷积核权重、卷积核最匹配样本、类别激活图(Class Activation Map/CAM)、网络结构的可视化方法
目录 0,可视化的重要性: 1,特征图(feture map) 2,卷积核权重 3,卷积核最匹配样本 4,类别激活图(Class Activation Map/CAM) 5,网络结构的可视化 0,可视 ...
- 在NestJS 中添加对Stripe 的WebHook 验证
在NestJS 中添加对Stripe 的WebHook 验证 背景介绍 Nest 是一个用于构建高效,可扩展的NodeJS 服务器端应用程序的框架.它使用渐进式JavaScript, 内置并完全支持T ...
- Java中lombok @Builder注解使用详解(十八)
Lombok大家都知道,在使用POJO过程中,它给我们带来了很多便利,省下大量写get.set方法.构造器.equal.toString方法的时间.除此之外,通过@Builder注解,lombok还可 ...
- java 8新特性 并行流
使用并行流,提高cpu利用率,提高运算速度 /** * java 8并行流 * 底层运用fork join框架 */ @Test public void test(){ Instant start = ...
- 记一次Vue跨导航栏问题解决方案
简述 这篇文章是我项目中,遇到的一个issue,我将解决过程和方法记录下来. 本篇文章基于Vue.js进行的前端页面构建,由于仅涉及前端,将不做数据来源及其他部分的叙述.使用的CSS框架是 Boots ...