日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【POJ - 2236】Wireless Network (并查集)

發布時間:2023/12/10 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 2236】Wireless Network (并查集) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?題干:

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.?

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.?

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

題目大意:

? ?先給出一些點(的坐標x和y),接下來有兩種操作 ?'O'代表修復一個點。'S'代表測試當前情況下是否兩者已經連上。有一個計算機網絡的所有線路都壞了,網絡中有n臺計算機,現在你可以做兩種操作,修理(O)和檢測兩臺計算機是否連通(S),只有修理好的計算機才能連通。連通有個規則,兩臺計算機的距離不能超過給定的最大距離D(一開始會給你n臺計算機的坐標)。檢測的時候輸出兩臺計算機是否能連通。

解題報告:

? ?不算難啊這題,但是要注意,你能并起來的前提是這個電腦已經被修好了,所以用ok[ top ]記錄一下已經被修好的電腦的下標。

#include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; const double eps = 1e-6; int ok[1000 + 5];//代表已經修好的點 存標號即可 然后再結構體查詢 int f[1000 + 5]; int n,d; struct Node {double x,y;} node[1000 + 5]; int getf(int v) {return v==f[v]?v:f[v]=getf(f[v]);} void merge(int u,int v) {int t1=getf(u);int t2=getf(v);if(t1!=t2) {f[t2]=t1;} }double dis(Node a,Node b) {return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } void init() {for(int i = 1; i<=n; i++) {f[i]=i;} } int main() {int u,v;int top=0;char op[5];cin>>n>>d;init();for(int i = 1; i<=n; i++) {scanf("%lf %lf",&node[i].x,&node[i].y);}while(~scanf("%s",op) ) {if(op[0]=='O') {scanf("%d",&u);for(int i = 1; i<=top; i++) {//因為只能與修復的電腦進行連接 if(dis(node[ok[i] ] ,node[u] ) - d < eps) {merge(ok[i],u);}} ok[++top] = u; }else {scanf("%d %d",&u,&v);getf(u) == getf(v) ? printf("SUCCESS\n") : printf("FAIL\n");}}return 0 ; }

總結:

? ?這題有一個技巧,ok數組存的是結構體下標,而不是另開一個ok結構體,這增強了代碼的可讀性與節省了空間。

總結

以上是生活随笔為你收集整理的【POJ - 2236】Wireless Network (并查集)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。