HDU 4750 并查集+离线
Count The Pairs
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 103 Accepted Submission(s): 47
Problem Description

Input
Multiple cases, end with EOF. First line:n m n tourist spots ( 1<n<=10000), spots’ index starts from 0. m undirected roads ( 1<m<=500000).
Next m lines, 3 integers, a b c From tourist spot a to tourist spot b, its length is c. 0<a, b<n, c(0<c<1000000000), all c are different.
Next one line, 1 integer, p (0<p<=100000) It means p guests coming.
Next p line, each line one integer, t(0<=t) The value t you need to consider to satisfy f>=t.
Next m lines, 3 integers, a b c From tourist spot a to tourist spot b, its length is c. 0<a, b<n, c(0<c<1000000000), all c are different.
Next one line, 1 integer, p (0<p<=100000) It means p guests coming.
Next p line, each line one integer, t(0<=t) The value t you need to consider to satisfy f>=t.
Output
For each guest's requirement value t, output the number of pairs satisfying f>=t. Notice, (1,2), (2,1) are different pairs.
Sample Input
2 1
0 1 2
3
1
2
3
3 3
0 1 2
0 2 4
1 2 5
5
0
2
3
4
5
Sample Output
2
2
0
6
6
4
4
0
与前一题一模一样,不解释。
代码:
#include<iostream> #include<stdio.h> #include<algorithm> #include<string> #include<string.h> using namespace std; struct node { int u,v,d; }a[1000010]; bool cmp1(node a,node b) { return a.d<b.d; } struct NODE { int id,l; }b[110000]; bool cmp2(NODE a,NODE b) { return a.l<b.l; } int fa[100010],num[100100],out[1000000]; int find(int x) { if(fa[x]!=x)fa[x]=find(fa[x]); return fa[x]; } int bin(int x,int y) { int px=find(x); int py=find(y),t=0; if(px!=py) { fa[py]=px; t=num[px]*num[py]; num[px]+=num[py]; } return t; } int main() { int i,j,k,m,n,p,q; while(~scanf("%d%d",&n,&m)) { for(i=0;i<=n;i++) { fa[i]=i; num[i]=1; } for(i=0;i<m;i++)scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].d); scanf("%d",&q); for(i=0;i<q;i++)scanf("%d",&b[i].l),b[i].id=i; sort(a,a+m,cmp1); sort(b,b+q,cmp2); j=0; int ans=0; for(i=0;i<q;i++) { while(j<m&&a[j].d<b[i].l) { ans+=bin(a[j].u,a[j].v)*2; j++; } out[b[i].id]=ans; } for(i=0;i<q;i++)printf("%d\n",n*(n-1)-out[i]); } return 0; }

浙公网安备 33010602011771号