http://acm.hdu.edu.cn/showproblem.php?pid=3938
Portal
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 113 Accepted Submission(s): 56
6 4 5
2 1 5
8 10 5
7 3 7
7 8 8
半天沒看懂題目,水題一個。
問你有多少對(u,v)使得u,v之間的所有路徑的最大邊權的最小值小於給定的查詢值L。這種題一般都是把查詢和邊統一排序,然後並查集處理。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define M 70050
#define N 10050
struct node
{
int u,v,c;
}a[M];
int father[N],rank[N];
__int64 V[N],ans[N],coun;
int find(int t)
{
if(father[t]!=t)
father[t]=find(father[t]);
return father[t];
}
void Union(int x,int y)
{
int a=find(x),b=find(y);
if(a==b)
return ;
coun+=V[a]*V[b];
if(rank[a]<rank[b])
{
father[a]=b;
V[b]+=V[a];
}
else
{
father[b]=a;
V[a]+=V[b];
if(rank[a]==rank[b])
rank[a]++;
}
}
bool cmp(node a,node b)
{
if(a.c==b.c)
return a.u>b.u;
return a.c<b.c;
}
int main()
{
int n,m,q,i,u,v,c,L;
while(scanf("%d%d%d",&n,&m,&q)!=EOF)
{
for(i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&c);
a[i].u=u;a[i].v=v;a[i].c=c;
}
for(i=0;i<q;i++)
{
scanf("%d",&L);
a[m+i].u=-(i+1),a[m+i].v=-1;a[m+i].c=L;
}
sort(a,a+m+q,cmp);
coun=0;
for(i=1;i<=n;i++)
{
father[i]=i;
rank[i]=1;
V[i]=1;
}
for(i=0;i<m+q;i++)
{
u=a[i].u;v=a[i].v;
if(u>0)
Union(u,v);
else
ans[-u]=coun;
}
for(i=1;i<=q;i++)
printf("%I64d\n",ans[i]);
}
return 0;
}