jzoj4223
考慮這樣一種暴力:將所有<=x的邊按照類似最小生成樹的方式加入答案,然後用下面的方法統計答案:
1.首先加入一條邊
2.看這條邊是否將會合成聯通塊,如果會,那麼加進這條邊,記這條邊一端聯通塊大小為x,另一端為y,則對答案貢獻為(x+y)2−x2−y2(x+y)^2-x^2-y^2(x+y)2−x2−y2
但是這樣太慢,需要優化
不過我們發現:題目沒有要求在線,於是我們可以離線,將詢問按照x值從小到大排序,插進邊裡,這樣我們可以利用以前做過的結果不用重新再做一遍,減少了冗餘運算
#include<bits/stdc++.h>
using namespace std;
struct no{
int a,b,c,t,d;
bool operator <(const no &rhs)const{
return c<rhs.c||c==rhs.c&&t<rhs.t;
}
}e[500010];
int f[500010],ans,s[500010],n,m,q,ct,r[500010];
int fd(int x){return f[x]==x?x:f[x]=fd(f[x]);}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&q);
memset(e,0,sizeof(e));
for(int i=1;i<=n;i++){
f[i]=i;
s[i]=1;
}
ct=0;ans=0;
while(m--){
ct++;
scanf("%d%d%d",&e[ct].a,&e[ct].b,&e[ct].c);
e[ct].t=0;
}
for(int i=1;i<=q;i++){
ct++;
scanf("%d",&e[ct].c);
e[ct].t=1;
e[ct].d=i;
}
int p=1,w=0;
sort(e+1,e+ct+1);
while(w!=q){
while(p<=ct&&!e[p].t){
int xx=fd(e[p].a),yy=fd(e[p].b);
p++;
if(xx!=yy){
f[xx]=yy;
ans-=s[yy]*s[yy];
ans-=s[xx]*s[xx];
s[yy]+=s[xx];
ans+=s[yy]*s[yy];
}
}
w++;
r[e[p].d]=ans;
p++;
}
for(int i=1;i<=q;i++)
printf("%d\n",r[i]);
}
}
jzoj4223的更多相关文章
随机推荐
- [C#]WinForm动态删除控件 Controls.Remove()
今天遇到一个奇怪的问题,在WinForm内动态添加Button后再动态的移除,发生稀奇古怪的现象,Button控件只被规律的移除,没有完全移除 foreach (Control c in this.C ...
- springMVC学习 七 视图解析器
在springMVC中,如果不配置视图解析器,会走默认的视图解析器,但是有时候配置视图解析器,还有一定的作用 <bean id="viewResolver" class=&q ...
- spring学习 十七 scope属性,单例多例
Scope属性是<bean>中的属性,取值可以有, singleton 默认值, 单例, prototype 多例, 每次获取重新实例化, request 每次请求重新实例化, sessi ...
- oracle 中删除表 drop delete truncate
oracle 中删除表 drop delete truncate 相同点,使用drop delete truncate 都会删除表中的内容 drop table 表名 delete from 表名 ...
- day14(内置函数)
print(list(map(func, lambda x : x)) print(list(filter lambda x : x > len(x))) print(zip(l1,l2,tu) ...
- 2018.11.24 spoj New Distinct Substrings(后缀数组)
传送门 双倍经验(弱化版本) 考虑求出来heightheightheight数组之后用增量法. 也就是考虑每增加一个heightheightheight对答案产生的贡献. 算出来是∑∣S∣−heigh ...
- VMware 14 tools位置;
安装后默认位置: CD读入 然后,启动虚拟机. 1.创建挂载点 mkdir /mnt/cdrom 2.挂在光驱 mount /dev/cdrom /mnt/cdrom(我的这句无效) 3.查看 cd ...
- Comment类型
注释在DOM中是通过Comment类型来表示的. nodeType 8 nodeName #Comment nodeValue 注释的内容 parentNode 可能是Document或Element ...
- typecho开启pjax,ajax,无刷新
1.引入jquery和pjax 检查你的网站是否引入1.7.0版本以上的jquery.js,如果没有请全局引入 https://files.cnblogs.com/files/fan-bk/pjax. ...
- 使用MyGeneration创建模板:介绍(翻译)
原文信息 原文地址 原文作者信息: Justin Greenwood MyGeneration Software http://www.mygenerationsoftware.com April 2 ...