C
来源:互联网 发布:国外网络电视台直播 编辑:程序博客网 时间:2024/06/01 22:36
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.
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<cstring>using namespace std;int map[10005][10005];int visited[10005];int f[10005];struct node{ int x; int y;}s[1005];int find(int x){ return x==f[x]?x:f[x]=find(f[x]);}void join(int x,int y){ int fx=find(x); int fy=find(y); if(fx!=fy) f[fy]=fx;}int main(){ int n,m; cin>>n>>m; for(int i=1;i<=n;i++) { f[i]=i; cin>>s[i].x>>s[i].y; map[i][i]=0; for(int j=i-1;j>0;j--) map[i][j]=map[j][i]=(s[i].x-s[j].x)*(s[i].x-s[j].x)+(s[i].y-s[j].y)*(s[i].y-s[j].y); } memset(visited,0,sizeof(visited)); char op; int k1,k2; while(cin>>op) { if(op=='O') { cin>>k1; visited[k1]=1; for(int i=1;i<=n;i++) { if(map[k1][i]<=m*m&&i!=k1&&visited[i]) { join(k1,i); } } } else { cin>>k1>>k2; if(visited[k1]&&visited[k2]&&find(k1)==find(k2)) cout<<"SUCCESS"<<endl; else cout<<"FAIL"<<endl; } } return 0;}
阅读全文
0 0
- c
- c
- c
- c
- C
- c
- c
- c
- C+
- c
- C
- c
- c
- c
- C
- C
- c
- C
- 访问者模式
- CSS视频 46-64
- C#开发Windows窗体应用程序的步骤
- Mysql查询结果顺序按 in() 中ID 的顺序排列
- 在Activiti中如何使用自定义的组织架构
- C
- 安卓中margi、algin、padding区别(图解)
- mysql分布式数据库中间件对比
- GDI+学习及代码总结之------画线、区域填充、写字
- 清晰理解 Android Service
- bootstraptable表头居中
- 如何成为一名成功的程序员
- 创建数据库并解决报错this is incompatible with sql_mode=only_full_group_by
- 关于数据库id的重新排序自增