Links: Https://www.nowcoder.com/acm/contest/84/C
Source: Niu Ke Net
Arbitrary point time limit: C/s 1 sec, other languages 2 seconds
Space limitations: C/C + + 32768K, other languages 65536K
64bit IO Format:%lld
The topic describes a number of points on the plane, starting from each point, you can go in any direction, until you meet another point, and then you can change the direction.
Ask at least how many points to add, so that the point pair can reach each other. Input Description:
The first line an integer n represents the number of points (1 <= n <= 100).
Second row n rows, two integers per line x
I
Y
I
Representation coordinates (1 <= x
I
Y
I
<= 1000).
The y-axis is north in the positive direction and the x-Axis square is east.
Output Description:
Outputs an integer that represents the minimum number of points that need to be added.
Example 1 input
22 11 2
Output
1
Example 2 input
22 14 1
Output
0
This is one and check the set for Unicom fast
1#include <cstdio>2#include <algorithm>3#include <vector>4#include <queue>5#include <cstring>6#include <string>7 using namespacestd;8 Const intMAXN = 1e4 +Ten;9 TentypedefLong LongLL; One intFA[MAXN], X[MAXN], Y[MAXN]; A - voidInitintN) { - for(inti =0; I <= N; i++) { theFa[i] =i; - } - } - intFindintp) { + while(P! = fa[p]) p =Fa[p]; - returnp; + } A intFind_set (intp) at { - while(P!=fa[p]) p=Fa[p]; - returnp; - } - - voidCombineintPintq) { in intNP =Find_set (p); - intNQ =Find_set (q); to if(NP = NQ) FA[NP] =NQ; + } - the intMain () { * intN; $ //freopen ("DATA.txt", "R", stdin);Panax Notoginseng while(SCANF ("%d", &n)! =EOF) { - init (n); the for(inti =1; I <= N; i++) { +scanf"%d%d", &x[i], &y[i]); A } the for(inti =1; I < n; i++) + for(intj = i +1; J <= N; J + +) - if(X[i] = = X[j] | | y[i] = =Y[j]) combine (i, j); $ intAns =0; $ for(inti =1; I <= N; i++) - if(Fa[i] = = i) ans++; -printf"%d\n", ans-1); the } - return 0;Wuyi}
Any point ~ and check set to find the Unicom block