离线的并查集
水啊水,昨天太2了
不过题意也够2的,这翻译啊
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct EDGE
{
int u, v, val;
}edge[50000 + 1234];
struct QUE
{
int id, val;
}l[10000 + 123];
int n, m, q;
int p[10000 + 123];
int num[10000 + 123];
int cou[10000 + 123];
bool cmp(EDGE x, EDGE y)
{
return x.val < y.val;
}
bool cmp2(QUE x, QUE y)
{
return x.val < y.val;
}
void init()
{
for(int i = 1; i <= n; i++)
{
p[i] = i;
num[i] = 1;
}
}
int find(int k)
{
if(k != p[k]) p[k] = find(p[k]);
return p[k];
}
int Union(int a, int b)
{
a = find(a);
b = find(b);
if(a == b) return 0;
int t = num[a] * num[b];
p[b] = a;
num[a] += num[b];
num[b] = 0;
return t;
}
int main()
{
while(scanf("%d%d%d", &n, &m, &q) != EOF)
{
for(int i = 0; i < m; i++) scanf("%d%d%d",&edge[i].u, &edge[i].v, &edge[i].val);
sort(edge, edge + m, cmp);
for(int i = 0; i < q; i++)
{
scanf("%d", &l[i].val);
l[i].id = i;
}
sort(l, l + q, cmp2);
init();
int begin = 0;
int ans = 0;
for(int i = 0; i < q; i++)
{
while(edge[begin].val <= l[i].val && begin < m)
{
ans += Union(edge[begin].u, edge[begin].v);
begin++;
}
cou[l[i].id] = ans;
}
for(int i = 0; i < q; i++)
printf("%d\n", cou[i]);
}
return 0;
}