【JZOJ3886】道路维护
2017-01-20 21:43
405 查看
Description
最近徆多人投诉说C国的道路破损程度太大,以至亍无法通行C国的政府徆重视这件事,但是最近财政有点紧,丌可能将所有的道路都进行维护,所以他们决定按照下述方案进行维护
将C国抽象成一个无向图,定义两个城市乊间的某条路径的破损程度为该条路径上所有边破损程度的最大值,定义两个城市乊间的破损程度为两个城市乊间所有路径破损程度的最小值
然后C国政府向你提问多次,有多少个城市对的破损程度丌超过L,他们将依照你的回答来决定到底怎样维护C国的道路
注:这里的丌是“不”的意思。
Solution
把边排序,询问排序,原题就可以离线做了。我们对于一个Li,加入所有边权比它小的边,用并查集维护连通性,两个集合合并答案贡献就加上两个集合大小的乘积。
Code
#include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #define fo(i,j,k) for(int i=j;i<=k;i++) #define fd(i,j,k) for(int i=j;i>=k;i--) #define N 10010 #define M 100010 #define ll long long using namespace std; int f ; ll sz ,z[M]; struct node{ int u,v,w; }e[M]; struct ask{ int w,num; }b[M]; int find(int x){ return !f[x]?x:f[x]=find(f[x]); } bool cmp(node x,node y){ return x.w<y.w; } bool cmp1(ask x,ask y){ return x.w<y.w; } int main() { int n,m,q; scanf("%d %d %d",&n,&m,&q); fo(i,1,n) sz[i]=1; fo(i,1,m) scanf("%d %d %d",&e[i].u,&e[i].v,&e[i].w); fo(i,1,q) scanf("%d",&b[i].w),b[i].num=i; sort(e+1,e+m+1,cmp); sort(b+1,b+q+1,cmp1); int p=0; ll ans=0; fo(i,1,q) { while(e[p+1].w<=b[i].w && p<m) { p++; int fx=find(e[p].u),fy=find(e[p].v); if(fx!=fy) { f[fy]=fx; ans+=sz[fx]*sz[fy]; sz[fx]+=sz[fy]; } } z[b[i].num]=ans; } fo(i,1,q) printf("%lld ",z[i]); }
相关文章推荐
- jzoj 3886 道路维护
- JZOJ 3886. 【长郡NOIP2014模拟10.22】道路维护
- 【JZOJ 3886】 道路维护
- 【JZOJ3886】【长郡NOIP2014模拟10.22】道路维护
- 【jzoj3886】【道路维护】【并查集】
- Jzoj3934【NOIP2014day2官方数据】寻找道路
- 【JZOJ 3808】 道路值守
- 【NOI 2005】【JZOJ 2413】维护数列 平衡树操作模板题
- jzoj P1663【AHOI2009】维护序列
- JZOJ2413. 【NOI2005】维护数列
- [BZOJ4304]/[JZOJ3486]道路改建
- JZOJ 3808. 道路值守
- [JZOJ3806] 小X的道路修建
- JZOJ100042. 【NOIP2017提高A组模拟7.12】保留道路
- 【JZOJ 4594】 Dynamic len &【JZOJ 2491】维护队列(带修改的莫队算法 模板)
- 【挖坑记】JZOJ 4729 道路修建
- JZOJ4753【GDOI2017模拟9.4】种树 LCT维护子树信息+换根时维护Dfs序(CC MONOPLOY加强版)
- (jzoj snow的追寻)线段树维护树的直径
- 【jzoj2413】【NOI2005】【维护数列】【splay】