Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
这道题用并查集加qsort#include<iostream> using namespace std; struct Trees { int x; int y; int t; int p; }; int sum[16001]; int parent[16001]; int cmp(const void * Aa,const void * Bb) { Trees A = *(Trees *)Aa; Trees B = *(Trees *)Bb; if(A.x == B.x) return A.y - B.y; return A.x - B.x; } int cap(const void * Aa,const void * Bb) { Trees A = *(Trees *)Aa; Trees B = *(Trees *)Bb; if(A.y == B.y) return A.x - B.x; return A.y - B.y; } int com(const void * Aa,const void * Bb) { int A = *(int *)Aa; int B = *(int *)Bb; return B-A; } int find_parent(int x) { if(x != parent[x]) { parent[x] = find_parent(parent[x]); } return parent[x]; } void Tree_union(int x,int y) { int a = find_parent(x); int b = find_parent(y); parent[a] = b; sum[b]+=sum[a]; } int main() { Trees T[16001]; int nums; int region_nums; cin>>nums>>region_nums; int max = 0; for(int i=0;i<nums;i++) { T[i].t=i; parent[i]=i; sum[i] = 1; cin>>T[i].x>>T[i].y; } qsort(T,nums,sizeof(Trees),cmp); for(int i=1;i<nums;i++) { if((T[i].x==T[i-1].x)&&(T[i].y-T[i-1].y==1)) { if(find_parent(T[i-1].t)!=find_parent(T[i].t)) Tree_union(T[i-1].t,T[i].t); } } qsort(T,nums,sizeof(Trees),cap); for(int i=1;i<nums;i++) { if((T[i].y==T[i-1].y)&&(T[i].x-T[i-1].x==1)) { if(find_parent(T[i-1].t)!=find_parent(T[i].t)) Tree_union(T[i-1].t,T[i].t); } } int result[16001]; int j=0; for(int i=0;i<nums;i++) { if(parent[i] == i) result[j++]=sum[i]; } qsort(result,j,sizeof(int),com); for(int i=0;i<region_nums;i++) max+=result[i]; cout<<max<<endl; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator