problem 2194 Galaxy collisionAccept:14 submit:48
Time limit:30000 mSec Memory limit:327680 KB problem Description
It is predicted that, about 10 billion years later, the Leo Galaxy will collide with the Milky Way galaxy, the collision of two galaxies will merge two galaxies, but no 2 planets will collide with each other. Now some scientists get the result of merging two galaxies, some of the points on the two-dimensional plane, but do not know that the planets belong to the Milky Way, if two planets belong to the same galaxy, then the distance between them is more than 5 light-years, the distance is the Euclidean distance, that is (x1,y1) and (x2, y2) is a distance of sqrt ((x1-x2) * (X1-X2) + (y1-y2) * (Y1-y2)). Now I'd like to ask you to help divide the combined results into 2 sets, one belonging to the Milky Way and one belonging to the Leo Galaxy, because the set-up scheme may have many, now want to know how many planets may belong to the Milky Way. (All planets belong to the Milky Way)
For example: if there are 6 points, you can have the following 4 partition {{1, 2, 4, 5}, {3, 6}}; {{1, 2, 3, 4}, {5, 6}}; {{1, 4,5}, {2, 3, 6}}; {{1, 3, 4}, {2, 5, 6}}, then the first partition {1,2,4,5} belongs to the Milky Way, with the answer 4.
input contains multiple sets of data for each set of data entered in the first row an integer n represents the number of planets (1<=n<=50000), followed by n rows of 2 integers per line x and y for the coordinates of the planet (1<=x,y<=500000), with no coincident points. output output An integer representing the maximum number of planets that belong to the Milky Way. If there is no way to partition then output-1. sample Input61 San 111 713 4 sample OUTPUT4 Analysis: All the planets in the x-coordinate from small to large order, and then enumerate two points (plus pruning) distance, if the distance is less than or equal to 5, if the two points are not in a set, Then joins a set, and the two connected Dianran are different colors, if the two points in a set, then determine the contradiction, because the set of at least three points, their distance between 22 is less than or equal to 5, that is, no matter how assigned they can not meet the requirements of the topic, if there is no contradiction in the situation, It calculates the number of MAX1 and max2 that are dyed into two colors in each set, and finally adds up the larger of all the sets: Sum+=max (Max1[k]+max2[k]) (k belongs to a different set) program;
#include "stdio.h" #include "string.h" #include "math.h" #include "iostream" #include "queue" #include "algorithm" # Include "stack" #include "map" #include "string" #define M 50009#define inf 0x3f3f3f3f#define EPS 1e-9#define LL __ int64using namespace std;struct node{int x, y;} P[m];int CMP (node A,node b) {return a.x<b.x;} ll Dist (node A,node b) {return (LL) (a.x-b.x) * (a.x-b.x) + (LL) (A.Y-B.Y) * (A.Y-B.Y);} int f[m],max1[m],max2[m],sum[m];int finde (int x) {if (x!=f[x]) {int t=f[x]; F[x]=finde (F[x]); Sum[x]= (sum[x]+sum[t])%2; } return f[x];} int ok (int n) {for (int i=0;i<n;i++) {f[i]=i; sum[i]=0; } for (int i=0;i<n;i++) {for (int. j=i+1;j<n;j++) {if (p[j].x-p[i].x>5) break; if (Dist (p[i],p[j]) <=25ll) {int X=finde (i); int Y=finde (j); if (x!=y) {f[y]=x; Sum[y]= (sum[x]+sum[i]+1-sum[j])%2; } else {if (sum[x]==sum[y]) return-1; }}}} memset (Max1,0,sizeof (MAX1)); memset (max2,0,sizeof (MAX2)); for (int i=0;i<n;i++) {int Y=finde (i); F[i]=y; if (sum[i]==0) max1[y]++; else max2[y]++; } int ans=0; for (int i=0;i<n;i++) {if (f[i]==i) {Ans+=max (max1[i],max2[i]); }} return ans; int main () {int n; while (~SCANF ("%d", &n)) {for (int i=0;i<n;i++) {scanf ("%d%d", &p[i].x,&p[i].y); } sort (p,p+n,cmp); printf ("%d\n", OK (n)); } return 0;}
Fzu oj2194 Galaxy Collision (sort + and check set + path compression)