Wireless Network
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 30456 | Accepted: 12644 |
Description
An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers are repaired one by one, and the network gradually began to work again. Because of the hardware restricts, each computer can only directly communicate with the computers that are not farther than d meters from it. But every computer can be regarded as the intermediary of the communication between two other computers, that is to say computer A and computer B can communicate if computer A and computer B can communicate directly or there is a computer C that can communicate with both A and B.
In the process of repairing the network, workers can take two kinds of operations at every moment, repairing a computer, or testing if two computers can communicate. Your job is to answer all the testing operations.
In the process of repairing the network, workers can take two kinds of operations at every moment, repairing a computer, or testing if two computers can communicate. Your job is to answer all the testing operations.
Input
The first line contains two integers N and d (1 <= N <= 1001, 0 <= d <= 20000). Here N is the number of computers, which are numbered from 1 to N, and D is the maximum distance two computers can communicate directly. In the next N lines, each contains two integers xi, yi (0 <= xi, yi <= 10000), which is the coordinate of N computers. From the (N+1)-th line to the end of input, there are operations, which are carried out one by one. Each line contains an operation in one of following two formats:
1. "O p" (1 <= p <= N), which means repairing computer p.
2. "S p q" (1 <= p, q <= N), which means testing whether computer p and q can communicate.
The input will not exceed 300000 lines.
1. "O p" (1 <= p <= N), which means repairing computer p.
2. "S p q" (1 <= p, q <= N), which means testing whether computer p and q can communicate.
The input will not exceed 300000 lines.
Output
For each Testing operation, print "SUCCESS" if the two computers can communicate, or "FAIL" if not.
Sample Input
4 1 0 1 0 2 0 3 0 4 O 1 O 2 O 4 S 1 4 O 3 S 1 4
Sample Output
FAIL SUCCESS
Source
POJ Monthly,HQM
题目大意
n台电脑都坏了。修好每台电脑只能与它距离<=d的电脑联系。
一些命令 有修好某台电脑,有询问某两台电脑能否联通。
题解
并查集 当这台电脑修好时,遍历一遍它能到的电脑vi,如果vi修好了,
这两台电脑加入一个集合中。
MMP在poj上交题 WA=绝望。一开始 ‘欧’看成‘0’,无限WA,然后字符串用
字符%c输入的因为空格等无限WA,距离没用double无限wa
╭(╯^╰)╮以后用字符数组
代码
#include<iostream> #include<vector> #include<cmath> #include<algorithm> #include<cstdio> using namespace std; vector<int>vec[1005]; int n,p,q; double d; int xi[1005],yi[1005],fa[1005],hasfix[1005]; char od[5]; double muldis(int i,int j){ return sqrt((xi[i]-xi[j])*(xi[i]-xi[j])+(yi[i]-yi[j])*(yi[i]-yi[j])); } int f(int x){return fa[x]==x?x:fa[x]=f(fa[x]);} void unionn(int x,int y){ int fx=f(x),fy=f(y); if(fx!=fy) fa[fx]=fy; } int main(){ scanf("%d%lf",&n,&d); for(int i=1;i<=n;i++)fa[i]=i; for(int i=1;i<=n;i++)scanf("%d%d",&xi[i],&yi[i]); for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++){ if(muldis(i,j)<=d)vec[i].push_back(j),vec[j].push_back(i); } // getchar(); while(scanf("%s%d",&od,&p)!=EOF){ //以后还是开字符数组什么的否则还要换行 if(od[0]=='O'){ hasfix[p]=1; for(int i=0;i<vec[p].size();i++){ int v=vec[p][i]; if(hasfix[v]) unionn(v,p); } continue; } scanf("%d",&q); if(f(p)==f(q)&&hasfix[p]&&hasfix[q]) printf("SUCCESS "); else printf("FAIL "); } return 0; }