当前位置: 首页 > news >正文

Educational Codeforces Round 129 (Rated for Div. 2) F. Unique Occurrences 线段树分治

F. Unique Occurrences

time limit per test: 6 seconds

memory limit per test: 1024 megabytes

input: standard input

output: standard output


You are given a tree, consisting ofnnnvertices. Each edge has an integer value written on it.

Letf(v,u)f(v, u)f(v,u)be the number of values that appearexactly onceon the edges of a simple path between verticesvvvanduuu.

Calculate the sum off(v,u)f(v, u)f(v,u)over all pairs of verticesvvvanduuusuch that1≤v<u≤n1 \le v < u \le n1v<un.

DeepL 翻译:

给你一棵由nnn个顶点组成的树。每条边上都写有一个整数值。
假设f(v,u)f(v,u)f(v,u)是顶点vvvuuu之间简单路径的边上只出现一次的值的个数。
计算所有顶点vvvuuu之间的f(v,u)f(v,u)f(v,u)1≤v<u≤n1 ≤ v < u ≤ n1v<un

把每种颜色看为时间轴上的事件,进行分治处理。

简单来讲,我们用线段树分治,对于每种颜色全部删除后的树上的 dsu 进行统计,很显然,此时某一条边的贡献就是 左右两个连通块的大小的乘积,然后再把这种颜色的边加回来,再进行下一条边的枚举。

总的复杂度只有 n log n

vector<tuple<int,int,int>>e;vector<vector<PII>>col;intans;structop{intt,a,b;};classDSU{public:vector<int>f,siz;stack<op>save;DSU(){}DSU(intn){init(n);}voidinit(intn){f.resize(n+1);iota(range(f),0);siz.assign(n+1,1);}intfind(intx){returnf[x]==x?x:find(f[x]);}boolsame(intx,inty){returnfind(x)==find(y);}boolmerge(intx,inty){x=find(x);y=find(y);if(x==y){return0;}if(siz[x]>siz[y]){swap(x,y);}save.push({1,x,y});siz[y]+=siz[x];f[x]=y;returntrue;}voidrollback(){auto[t,x,y]=save.top();save.pop();if(t==1){siz[y]-=siz[x];f[x]=x;}else{}}size_tgetSnapshot()const{returnsave.size();}voidroll(inttar){while(save.size()>tar){rollback();}}intsize(intx){returnsiz[find(x)];}};template<typenameT>classSegmentTreeDC{public:inttot;// 时间点数(叶子数量)vector<vector<T>>tr;// 每个节点的操作列表SegmentTreeDC():tot(0LL){}SegmentTreeDC(int_T){init(_T);}voidinit(int_T){tot=max(1LL,_T);tr.clear();tr.resize(4LL*(tot+5));}// 插入一个操作 op 到闭区间 [l, r]voidinsert(intp,intL,intR,intl,intr,constT&op){if(l>r||l>R||r<L)return;if(l<=L&&R<=r){tr[p].push_back(op);return;}intmid=L+R>>1;insert(p<<1,L,mid,l,r,op);insert(p<<1|1,mid+1,R,l,r,op);}voidinsert(intl,intr,constT&op){if(l>r)return;insert(1,1,tot,l,r,op);}voiddfs(DSU&dsu){function<void(int,int,int)>dfs=[&](intp,intl,intr){size_t snap=dsu.getSnapshot();for(auto&e:tr[p]){dsu.merge(e.first,e.second);}if(l==r){for(auto[u,v]:col[l]){ans+=dsu.size(u)*dsu.size(v);}}else{intmid=(l+r)>>1;dfs(p<<1,l,mid);dfs(p<<1|1,mid+1,r);}dsu.roll(snap);};dfs(1,1,tot);}};voidsolve(){intn;cin>>n;e.resize(n+1);col.resize(n+2);for(inti=1;i<n;i++){intu,v,w;cin>>u>>v>>w;e[i]={u,v,w};col[w].push_back({u,v});}DSUdsu(n);SegmentTreeDC<PII>seg(n);for(inti=1;i<n;i++){auto&[u,v,w]=e[i];seg.insert(1,w-1,{u,v});seg.insert(w+1,n,{u,v});}seg.dfs(dsu);cout<<ans<<endl;}signedmain(){cin.tie(nullptr)->ios::sync_with_stdio(false);intT=1;// cin >> T;for(;T_<=T;T_++)solve();return0;}
http://www.jsqmd.com/news/311362/

相关文章:

  • Android System UI/Launcher开发工程师职位深度解析
  • 2026年1月电缆生产厂家推荐:电缆生产厂家排名、知名的电缆生产厂家推荐盘点名单
  • 深入解析:Android开发工程师的核心能力与实战面试指南
  • 2026年质量好的数码组合柔版印刷机/标签柔版印刷机信誉优质供应参考(可靠)
  • 亿航智能安卓工程师岗位深度解析与面试指南
  • 2026年评价高的亚克力浴缸/浴缸厂家口碑推荐汇总
  • 2026年知名的卫星式轮转印刷机/标签轮转印刷机厂家推荐与选择指南
  • 铁路地铁电力电缆生产厂家推荐:中低压、变频、聚乙烯绝缘、聚氯乙烯绝缘电缆生产厂家名单(2026年)
  • 2026年1月矿山煤矿电力电缆生产厂家推荐:中低压、变频、聚乙烯绝缘电缆
  • 2026年1月中国电缆一线品牌推荐:轨道交通、石油石化、矿山煤矿电缆国内一线品牌盘点
  • 2026年1月轨道交通电力电缆生产厂家推荐:中低压、变频、变频、聚乙烯绝缘电缆生产厂家
  • 2026年靠谱的酒店照明系统/酒店照明设计优选服务榜
  • 2026年口碑好的工程浮箱钢模板/圆柱钢模板厂家选购参考汇总
  • 2026年专业的酒店灯具工程采购/酒店灯具主流品牌排行榜
  • px4+ubuntu22.04+ros2开发记录
  • 2026年比较好的建筑3D打印构件/建筑3D打印技术厂家选择参考建议
  • 2026年热门的办公设备塑料齿轮/汽车塑料齿轮厂家推荐与选择指南
  • 2026年靠谱的大连考公线下课/大连考公线上课学员好评榜
  • OCR文字识别组件的深度解析:从传统方法到Vision Transformer与Diffusion的融合
  • 2026年评价高的安全气囊发生器外壳钢管/钢管厂家专业度参考(精选)
  • 2026年知名的大连公考银行编/大连公考省考班学员选择榜
  • 2026年热门的五金冲压弹片成型/精密加工五金冲压优质供应商推荐参考
  • 2026年质量好的气泡轻质土/高强度气泡轻质土厂家热销推荐
  • 2026年知名的全屋净水设备/全屋净水过滤系统用户口碑认可参考(高评价)
  • 2026年知名的反渗透净水器/净水器制造厂家
  • 2026年比较好的高压水阻起动柜/高压励磁起动柜值得信赖厂家推荐(精选)
  • 2026年口碑好的拉伸弹簧/触指弹簧高评价厂家推荐
  • 2026年评价高的路基回填泡沫混凝土/泡沫混凝土厂家选购完整指南
  • 2026年知名的手表盒/塑胶手表盒厂家信誉综合参考
  • 2026年口碑好的二维码贴标机/条码贴标机人气实力厂商推荐