POJ 2236 Wireless Network
http://poj.org/problem?id=2236
Main topic:
Give you n the computer coordinates that are damaged, these computers can only communicate with computers that do not exceed the distance d, but X and Y can communicate if both X and Y are capable of C communication. Now give a number of actions,
O P stands for repairing a computer with number p
The S p Q represents whether p and Q are able to communicate.
Ideas:
And check the set can be.
If a computer is repaired, it is placed in a collection with a fixed distance of not more than D.
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxn=1000+10; int Fa[maxn],n,d;bool canuse[maxn];int Find (int cur) {return fa[cur]<0? Cur:fa[cur]=find (Fa[cur]);} struct computers{int x, y;} A[maxn];bool OK (int x1,int y1,int x2,int y2) {return (X1-X2) * (X1-X2) + (y1-y2) * (y1-y2) <= d*d;} int main () {memset (fa,-1,sizeof (FA)); Memset (Canuse,0,sizeof (Canuse)); scanf ("%d%d", &n,&d); for (int i=1;i <=n;i++) scanf ("%d%d", &a[i].x,&a[i].y), Char cmd[10];int p,q;while (~scanf ("%s", cmd)) {if (cmd[0]== ' O ') { scanf ("%d", &p), int root=find (p), for (int i=1;i<=n;i++) {if (i==p) continue;if (OK (A[I].X,A[I].Y,A[P].X,A[P].Y) && Canuse[i]) {int root1=find (i); int root2=find (P); if (Root1!=root2) Fa[root1]=root2;}} Canuse[p]=true;} ELSE{SCANF ("%d%d", &p,&q); int rootp=find (p); int rootq=find (q); if (ROOTP==ROOTQ) printf ("success\n"); elseprintf ("fail\n");}} return 0;}
POJ 1703 Find them, Catch them
http://poj.org/problem?id=1703
Main topic:
XX City has two gangs, give you M message, D a B means a and B are not in a gang. A a B requires output A and B are not in a gang. (In/not/unsure)
Ideas:
And this question POJ 1182 food chain and look at the collection is similar.
Divided into two sets, given that D a B shows a and b are not in a set, then merge A and B+n,b and a+n.
#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;const int maxn=200000+10 ; int fa[maxn];int find (int cur) {return cur==fa[cur]? Cur:fa[cur]=find (Fa[cur]);} void Union_set (int a,int b) {int root_a=find (a); int root_b=find (b); if (Root_a==root_b) return;fa[root_a]=root_b;} int main () {int t;scanf ("%d", &t), while (t--) {int n,m;scanf ("%d%d", &n,&m); int num=n*2;for (int i=1;i<= num;i++) Fa[i]=i;char cmd[5];int a,b;for (int i=0;i<m;i++) {scanf ("%s%d%d", cmd,&a,&b); if (cmd[0]== ' a ') {if ( Find (a) ==find (b)) printf ("In the same gang.\n"), Else if (find (a) ==find (b+n) | | find (b) ==find (a+n)) printf ("in different Gangs.\n "), Else printf (" Not sure yet.\n ");} Else{union_set (a,b+n); Union_set (a+n,b);}} return 0;}