Fix the computers(解题报告)
来源:互联网 发布:阿里云账号登录 编辑:程序博客网 时间:2024/05/22 07:55
Fix the computers
Time Limit:10000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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 10 10 20 30 4O 1O 2O 4S 1 4O 3S 1 4
Sample Output
FAILSUCCESS解题思想:本题主要考察并查集,大意为n台电脑,接下来输入n行,每行代表电脑的坐标,n+1行开始O打头代表修复某台电脑,S打头代表问两台电脑在d距离内是否能修好。 数据输入时的处理颇为奇葩,用了一次循环,每次输入二维数组的两列;然后进入while循环,判断开头第一个字符是O还是S,若是O,则输入其中一台电脑,把 它标记下,接下来循环遍历,如果满足被标记过(被修好了)并且距离小于d,建立起联系;若是S,则输入两个数,判断这两个数是否建立起联系。解题代码:#include <stdio.h>#include <string.h>int s[1111][2];int v[1111];int f[1111];void init(int n){ int i,j; for(i=1;i<=n;i++) { f[i] = i; }}int find(int x){ if(f[x]==x) return x; return f[x]=find(f[x]);}int is_friend(int a,int b){ int x=find(a); int y=find(b); if(x==y) return 1; else return 0;}void set_friend(int a,int b){ int x=find(a); int y=find(b); if(x!=y) f[y]=x;}int dis(int a,int b){ return ((s[a][0]-s[b][0])*(s[a][0]-s[b][0]) + (s[a][1]-s[b][1])*(s[a][1]-s[b][1]));}int main(){ int n,d,i,j,t1,t2,t3; char ch[5]; while(scanf("%d%d",&n,&d)!=EOF) { init(n); memset(v,0,sizeof(v)); d *= d; for(i=1;i<=n;i++) scanf("%d%d",&s[i][0],&s[i][1]); while(scanf("%s",ch)!=EOF) { if(ch[0]=='O') { scanf("%d",&t1); v[t1] = 1; for ( i=1;i<=n ;i++ ) { if(v[i]==1 && dis(i,t1)<=d) { set_friend(t1,i); } } } if(ch[0]=='S') { scanf("%d%d",&t2,&t3); if(is_friend(t2,t3)==1) printf("SUCCESS\n"); else if(is_friend(t2,t3)==0) printf("FAIL\n"); } } }}
0 0
- Fix the computers(解题报告)
- Fix the computers ——(并查集) 结题报告
- HDU 1913 Computers (解题报告)
- hdu 1071 - The area(解题报告)
- F - The Suspects(解题报告)
- USACO :The Castle解题报告
- HDOJ_1709 The Balance 解题报告
- USACO The Primes 解题报告
- 解题报告:HDU_1709 The Balance
- The Skyline Problem解题报告
- 解题报告(一):uva 10313 - Pay the Price (dp)
- G - The Fun Number System解题报告(贺奕凯)
- POJ 1166 The Clocks 解题报告(高斯消元法 & 逆矩阵)
- ZOJ 3466 The Hive II 解题报告(插头DP)
- Uva 11752 The Super Powers 解题报告(数学)
- POJ 2142 The Balance 解题报告(模线性方程)
- SGU 106. The equation 解题报告(模线性方程)
- UVa 11100 The Trip, 2007 解题报告(策略)
- javascript 生成UUID随机数
- 关于ioremap,request_mem_region
- Android源码分析-Alarm机制与Binder的交互
- ajax中的get和post说明
- 在同一个位置更新进度
- Fix the computers(解题报告)
- ios系列教程之用instruments来检验你的app
- 视图控制器的基本概念
- UIO-用户空间驱动的新发展
- 基于AOP实现权限管理:访问控制模型RBAC和ACL
- hdu 2255 奔小康赚大钱 KM算法
- CF contest/382/B. Number Busters
- 特色项目
- UITableView应用1