并查集初阶2 wireless network
来源:互联网 发布:淘宝微胖女装店 编辑:程序博客网 时间:2024/05/16 00:55
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.
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.
For each Testing operation, print "SUCCESS" if the two computers can communicate, or "FAIL" if not.
4 10 10 20 30 4O 1O 2O 4S 1 4O 3S 1 4
FAILSUCCESS
#include <iostream>#include <stdio.h>#include <math.h>using namespace std;int N,D;struct node{ double x,y; int data; int father; int good; int arank;}a[1005];int find_father(int z){ if(a[z].father==z) return z; return find_father(a[z].father);}void union_computer(int u,int v){ int u1=find_father(u); int v1=find_father(v); if(u1!=v1) { double d=sqrt(pow((a[u].x-a[v].x),2)+pow((a[u].y-a[v].y),2)); if(d<=D) { //if(a[u].arank<a[v].arank) a[u1].father=v1; //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // else //a[v].father=a[u].father; // if(a[u].arank==a[v].arank) // a[u].arank++; } } return; }int main(){ cin>>N>>D; for(int i=1;i<=N;i++) { cin>>a[i].x>>a[i].y; a[i].data=i; a[i].father=i; a[i].good=0; a[i].arank=0; } char c; while(scanf("%c",&c)!=EOF) { if(c=='O') { int num; cin>>num; a[num].good=1; for(int i=1;i<=N;i++) { if(a[i].good==1&&i!=num) { union_computer(num,i); } // cout<<i<<" "<<a[i].father<<" "<<a[num].father<<endl; } } else if(c=='S') { int e,f; cin>>e>>f; int e1=find_father(e); int f1=find_father(f); if(e1!=f1) cout<<"FAIL"<<endl; else cout<<"SUCCESS"<<endl; } }}
阅读全文
0 0
- 并查集初阶2 wireless network
- 【并查集】Wireless Network
- 并查集----Wireless Network
- Wireless Network 并查集
- Wireless Network 并查集
- Wireless Network --并查集
- POJ 2236 Wireless Network(并查集)
- POJ2236 Wireless Network(并查集)
- POJ 2236 Wireless Network //并查集
- POJ2236 Wireless Network(并查集)
- POJ 2236 WIRELESS NETWORK(并查集)
- POJ-2236 wireless network 并查集
- 【hdu】 Wireless Network (并查集)
- POJ2236 Wireless Network [并查集]
- poj2236 Wireless Network 并查集
- poj 2236 Wireless Network(并查集)
- poj 2236 - Wireless Network(并查集)
- POJ P2236 Wireless Network 并查集
- 杨辉三角
- 数据库查出表中重复列的数据
- 7.spring与mybatis整合中Junit的测试
- 全球领先的redis客户端:SFedis
- Deeplearning-吴恩达-卷积神经网络-第一周作业01-Convolution Networks(python)
- 并查集初阶2 wireless network
- shell相关文件
- You need to use a Theme.AppCompat theme (or descendant) with this activity
- Android_自定义描述进度的View
- C#:ListView控件如何实现点击列表头进行排序
- git学习笔记
- webrtc的base库在windows平台的编译
- 用C++实现一个LRU cache
- 88. Merge Sorted Array