HDU 4750 Count The Pairs(并查集)

题目链接

没有发现那个点,无奈。

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
#define LL __int64
int o[],rank[];
struct node
{
int u,v,w;
} edge[];
int n,m;
int que[];
LL ans[];
bool cmp(node a,node b)
{
return a.w < b.w;
}
int find(int x)
{
int r,t;
r = x;
while(x != o[x])
x = o[x];
while(r != x)
{
t = o[r];
o[r] = x;
r = t;
}
return x;
}
void merge(int x,int y)
{
x = find(x);
y = find(y);
if(x != y)
{
o[x] = y;
rank[y] += rank[x];
}
}
int bin(int x)
{
int str,end,mid;
str = ;
end = m-;
if(x > que[end])
return m;
while(str < end)
{
mid = (str+end)/;
if(que[mid] < x)
str = mid + ;
else
end = mid;
}
return str;
}
int main()
{
int i,u,v,t;
LL sum;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i = ;i < n;i ++)
{
rank[i] = ;
o[i] = i;
}
for(i = ; i < m; i ++)
{
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
que[i] = edge[i].w;
ans[i] = ;
}
sort(que,que+m);
sort(edge,edge+m,cmp);
sum = ;
for(i = ; i < m; i ++)
{
u = find(edge[i].u);
v = find(edge[i].v);
if(u != v)
{
ans[i] = (LL)rank[u] * rank[v]*;
sum += (LL)rank[u] * rank[v]*;
merge(u,v);
}
}
for(i = ;i < m;i ++)
ans[i] = ans[i] + ans[i-];
scanf("%d",&t);
for(i = ; i < t; i ++)
{
scanf("%d",&u);
v = bin(u);
if(v == )
printf("%I64d\n",sum);
else
printf("%I64d\n",sum-ans[v-]);
}
}
return ;
}
上一篇:HDU 4750 Count The Pairs ★(图+并查集+树状数组)


下一篇:读书笔记--SQL必知必会06--用通配符进行过滤