我滴妈这个题好神啊……
Q == Question
A == LOI_a
Q:我去这题怎么做
A:我看看……
A:数据范围?
Q:200000……
A:这题能做?
Q:QAQ
A:我回去看看
(两分钟后)
A:先跑个最小生成树,恩恩,然后在合并两棵树的时候,这条边一定是最大的,那么统计下答案,是左边的大小 * 右边的大小
Q:QAQ
A:显然这个数组是单调的,二分查找下就行
Q:Orz
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 200000 + 5;
struct edge
{
int f,t,v;
bool operator < (const edge &b)const
{
return v < b.v;
}
}l[MAXN];
int n,m;
int fa[MAXN],size[MAXN];
void init()
{
for(int i = 1;i <= n;i ++)
fa[i] = i,size[i] = 1;
return;
}
int find(int x)
{
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
bool same(int x,int y)
{
return find(x) == find(y);
}
void merge(int x,int y)
{
x = find(x);
y = find(y);
if(size[x] > size[y])
swap(x,y);
fa[x] = y;
size[y] += size[x];
return;
}
int tot;
int val[MAXN];//对答案有贡献的边i的权值为val[i]
long long ans[MAXN];//对答案有贡献的边i的答案为ans[i]
void Kruskal()
{
sort(l + 1,l + m + 1);
init();
tot = 0;
for(int i = 1;i <= m;i ++)
{
if(same(l[i].f,l[i].t))
continue;
tot ++;
int z_size = size[find(l[i].f)];
int y_size = size[find(l[i].t)];
val[tot] = l[i].v;
ans[tot] = z_size * y_size + ans[tot - 1];
merge(l[i].f,l[i].t);
}
tot++;
val[tot] = 2147483647;
ans[tot] = (long long)n * n;
return;
}
long long answer(int v)
{
int k = upper_bound(val + 1,val + tot + 1,v) - (val);
return ans[k - 1];
}
int q;
int f,t,v;
int main()
{
scanf("%d %d %d",&n,&m,&q);
for(int i = 1;i <= m;i ++)
scanf("%d %d %d",&l[i].f,&l[i].t,&l[i].v);
Kruskal();
while(q --)
{
scanf("%d",&v);
printf("%lld\n",answer(v));
}
return 0;
}