Wireless Network

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.

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

题意:

题目大意是说有一些电脑,编号为1到N,现在这些电脑坏了,无法相互连通,我们需要维修,输入首先输入N和d,N表示有多少台电脑,d表示两台已维修好的电脑若它们之间的距离小于等于d,则两台电脑可以互通。接下来输入N行,每行输入a,b两个数,N行中的第i行表示编号为i的电脑的坐标(用来求两台电脑的距离),在接下来的输入各种操作,O a表示编号为a的电脑被维修好了,S a b则表示询问编号为a和b的电脑能不能互通,若能则输出SUCCESS,若不能则输出FAIL。

思路:

首先先来分析样例,一开始所有电脑都是坏的,然后显示O 1操作,代表了编号为1的电脑被修好,发现之前没有修好的电脑,也就表示编号为1的电脑(后面简称1号)没有可以互通的电脑,接着是2号被修好,发现2号与1号的距离为1,所以2号和1号可以互通,所以对2号和1号执行连接(link)操作,接着是4号被修好,发现1号和2号与4号之间的距离都大于d,也就是说没有连接操作,这是有一个S 1 4询问,可以发现1号和4号并不能互通,因此输出FAIL,接下来修好3号,并且进行2号和3号、4号和3号的连接操作,最后的询问发现1号和4号可以通过2号和3号连接,所以输出SUCCESS。
由上面的分析可以清晰地发现这就是个并查集的题,接下来我们来模拟下样例,首先我们定义一个par数组,用来存放每个结点的父亲节点,以自己为父亲的节点就是最终节点,所以par数组的初始状态为:

下标12345
par12345

接下来的连接操作是1号和2号,所以par数组修改为:

下标12345
par11345

再接着的连接操作是2号和3号,首先我们发现2号节点的par是1,说明2号不是最终节点,所以我们要找到1号,发现1号是最终节点,而3号也是最终节点,所以将3号节点的父节点置为1,如下:

下标12345
par11145

看着这个数组,我们只需要判断两个节点的最终节点是否相等,就可以知道两个节点是否互通,例如此时判断1和4号是否互通,此时1号的最终节点是1号,4号的最终节点是4号,所以不想等,所以不互通。
最后被修复的是4号,所以par数组修改如下:

下标12345
par11115

此时可见1号和4号是互通的。
这里我们需要考虑个问题,从上表可以看出上面的节点都是接在1号上面的,这样保证了树的均匀(不知道专业术语怎么说,差不多就是均匀的意思吧)。但是在代码里我们应该如何保证这个均匀呢,对此我们可以设立 一个rank数组,在连接的时候我们可以比较两个节点的rank,我们可以把rank大的节点作为rank小的节点的父亲,若两个rank相等,则成为父亲的那个节点的rank加1。

代码:

找最终节点的代码

int find(int i){
	while (par[i] != i){
		i = par[i];
	}
	return i;
}

连接结点的代码

void Link(int a, int b){
	a = find(a);
	b = find(b);
	if (r[a] < r[b]){
		par[a] = b;
	}
	else{
		par[b] = a;
		if (r[a] == r[b])
			r[a]++;
	}
}

总体代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<math.h>
using namespace std;
#define clr_all(a,b) memset(a,b,sizeof(a))
#define clr(a,b,n) memset(a,b,sizeof(a[0])*n)
template<typename T> T Max(T a, T b){ return a > b ? a : b; }
const int MAXN = 1001 + 10;
struct node{
	int x, y;
}num[MAXN];
int par[MAXN];
int r[MAXN];
int ok[MAXN];
int tot;
void Init(){
	for (int i = 0; i < MAXN; i++)
		par[i] = i;
	tot = 0;
	clr_all(r, 0);
}
int find(int i){
	while (par[i] != i){
		i = par[i];
	}
	return i;
}
bool cal(int a, int b, int D){
	//printf("a:%d %d\n", num[a].x, num[a].y);
	//printf("b:%d %d\n", num[b].x, num[b].y);
	//printf("%.2f\n", pow(num[a].x - num[b].x, 2) + pow(num[a].y - num[b].y, 2));
	if (pow(double(num[a].x - num[b].x), 2) + pow(double(num[a].y - num[b].y), 2) <= D*D){
		return true;
	}
	else
		return false;
}
void Link(int a, int b){
	a = find(a);
	b = find(b);
	if (r[a] < r[b]){
		par[a] = b;
	}
	else{
		par[b] = a;
		if (r[a] == r[b])
			r[a]++;
	}
}
int main(){
	int N, D;
	scanf("%d%d", &N, &D);
	Init();
	for (int i = 1; i <= N; i++){
		scanf("%d%d", &num[i].x, &num[i].y);
	}
	char str[5];
	while (~scanf("%s", str)){
		int a, b;
		if (str[0] == 'O'){
			scanf("%d", &a);
			for (int i = 0; i < tot; i++){
				//printf("L %d %d\n", ok[i], a);
				if (cal(ok[i], a, D)){
					Link(ok[i], a);
					//printf("Link %d %d\n", ok[i], a);
				}
			}
			ok[tot++] = a;
		}
		else{
			scanf("%d%d", &a, &b);
			//printf("find %d %d\n", find(a), find(b));
			if (find(a) == find(b)){
				printf("SUCCESS\n");
			}
			else{
				printf("FAIL\n");
			}
		}
	}
	return 0;
}
Designing a Wireless network<br><br>Foreword xxv<br><br>From Past to Present 1<br><br>Introduction 2<br><br>Exploring Past Discoveries That Led to Wireless 4<br>Discovering Electromagnetism 4<br><br>Exploring Conduction 6<br><br>Inventing the Radio 6<br><br>Mounting Radio-Telephones in Cars 8<br><br>Inventing Computers and Networks 9<br><br>Inventing Cell Phones 11<br><br>Exploring Present Applications for Wireless 12<br>Vertical Markets 13<br>Using Wireless in Delivery Services 14<br>Using Wireless for Public Safety 14<br>Using Wireless in the Financial World 15<br>Using Wireless in the Retail World 15<br>Using Wireless in Monitoring<br>Applications 16<br>Applying Wireless Technology to Horizontal<br>Applications 16<br>Using Wireless in Messaging 17<br>Using Wireless for Mapping 17<br>Using Wireless for Web Surfing 17<br>Exploring This Book on Wireless 18<br>xiii<br><br>Summary 19<br>Solutions Fast Track 20<br>Frequently Asked Questions 21<br>Chapter 2 Radio Elements and Frequency Spectrums 23<br>Introduction 24<br>Learn the Properties of Transmitting Radio Signals Over EM Waves 24<br>Waveforms<br>Anatomy of a Waveform 25<br>Modulating a Radio Signal 27<br>a = Amplitude<br>Propagating a Strong Radio Signal 34<br>v = Velocity of Propagation<br>Understanding Signal Power and<br>τ = Period<br>Signal-to-Noise Ratio 35<br>λ = Wavelength<br>Attenuation 36<br>? = Frequency<br>Rain Attenuation 39<br>Bouncing 39<br>Refracting 41<br>Line of Sight 42<br>Penetration 43<br>Understanding the Wireless Elements 45<br>Generic Radio Components 45<br>Antennas 49<br>Omnidirectional Antennas 50<br>Directional Antennas 51<br>Base Stations and Mobile Stations 56<br>Access Points 57<br>Channelizing the Frequency Spectrum 57<br>Channelizing 59<br>Channel Bandwidth 59<br>Channel Spacing and Buffer Zones 60<br>Multichannel Systems and Channel<br>Offsets 61<br>Extending the Number of Channels<br>(Frequency Reuse) 61<br>Seven Cell Frequency Reuse 62<br>Multiple Accessing 63<br><br>152_wan_toc 6/22/01 4:54 PM Page xv<br>Contents xv<br>Regulating Wireless Communications 64<br>Regulatory Agencies 64<br>The Need to Know 65<br>Regulations for Low Power,Unlicensed<br>Transmitters 66<br>Summary 68<br>Solutions Fast Track 69<br>Frequently Asked Questions 71<br>Chapter 3 TCP/IP and the OSI Model 73<br>Learn to Configure<br>Introduction 74<br>and Maintain Routes<br>Exploring the OSI and DoD Models 74<br>for Full Connectivity<br>Layer 1:The Physical Layer 75<br>Layer 2:The Data-Link Layer 75<br>PC<br>Layer 3:The Network Layer 77<br>Layer 4:The Transport Layer 78<br>T3<br>A<br>T1<br>Layer 5:The Session Layer 78<br>F<br>T3<br>E<br>B<br>Layer 6:The Presentation Layer 79<br>D<br>T3<br>Layer 7:The Application Layer 80<br>T1<br>C<br>T3<br>OSI and DoD Correlation 81<br>Understanding the Network Access Layer 81<br>Server<br>Using Bridging 82<br>The Ethernet Protocol 85<br>Static Routing in a<br>Understanding the ARP Process 86<br>Multihop, Multipath<br>Network<br>Wireless Protocols 87<br>Other Network Access Protocols 88<br>Understanding the Internet Layer 88<br>The Internet Protocol 89<br>IP Addressing 91<br>Conserving Address Space with VLSM 93<br>Routing 95<br>The Internet Control Message Protocol 101<br>Understanding the Host-to-Host Layer 101<br>User Datagram Protocol 102<br>Transmission Control Protocol 102<br>Managing the Application Layer 105<br>Monitoring Tools:SNMP 105<br><br>152_wan_toc 6/22/01 4:54 PM Page xvi<br>xvi Contents<br>Assigning Addresses with DHCP 105<br>Conserving with Network Address<br>Translation 106<br>Summary 110<br>Solution Fast Track 111<br>Frequently Asked Questions 113<br>Chapter 4 Identifying Evolving Wireless<br>Technologies and Standards 115<br>Understand Bluetooth<br>Introduction 116<br>Piconet and Scatternet<br>Fixed Wireless Technologies 117<br>Configuration<br>Multichannel Multipoint Distribution Service 117<br>Local Multipoint Distribution Services 119<br>Scatternet<br>Wireless Local Loop 120<br>Piconet 1 Piconet 2<br>Point-to-Point Microwave 121<br>Legend<br>P S<br>SB<br>SB<br>Wireless Local Area Networks 122<br>Master<br>M<br>S<br>Slave<br>10 m<br>S M<br>M S<br>Why the Need for a Wireless LAN Standard? 123<br>P<br>Parked<br>S S<br>Standby<br>SB<br>What Exactly Does the 802.11 Standard<br>S P<br>Define? 125<br>Does the 802.11 Standard Guarantee <br>Compatibility across Different Vendors? 128<br>802.11b 130<br>802.11a 131<br>802.11e 132<br>Developing WLANs through the 802.11<br>Architecture 133<br>The Basic Service Set 133<br>The Extended Service Set 135<br>Services to the 802.11 Architecture 135<br>The CSMA-CA Mechanism 138<br>The RTS/CTS Mechanism 138<br>Acknowledging the Data 139<br>Configuring Fragmentation 140<br>Using Power Management Options 140<br>Multicell Roaming 140<br>Security in the WLAN 141<br><br>152_wan_toc 6/22/01 4:54 PM Page xvii<br>Contents xvii<br>Developing WPANs through the 802.15<br>Architecture 143<br>Bluetooth 144<br>HomeRF 147<br>High Performance Radio LAN 147<br>Mobile Wireless Technologies 148<br>First Generation Technologies 150<br>Second Generation Technologies 150<br>2.5G Technology 150<br>Third Generation Technologies 151<br>Wireless Application Protocol 151<br>Global System for Mobile Communications 153<br>General Packet Radio Service 155<br>Short Message Service 155<br>Optical Wireless Technologies 156<br>Summary 157<br>Solutions Fast Track 159<br>Frequently Asked Questions 163<br>Chapter 5 Designing a Wireless Network 165<br>Introduction 166<br>Create a Detailed<br>Exploring the Design Process 166<br>Physical Design<br>Conducting the Preliminary Investigation 167<br>Performing Analysis of the Existing<br>Equipment model<br>Environment 168<br>Cabling details<br>Creating a Preliminary Design 169<br>Rack details<br>Finalizing the Detailed Design 169<br>Environment<br>Executing the Implementation 170<br>requirements<br>Capturing the Documentation 171<br>Physical location of<br>Identifying the Design Methodology 172<br>devices<br>Creating the Network Plan 173<br>Detailed RF design<br>Gathering the Requirements 173<br>Baselining the Existing Network 175<br>Analyzing the Competitive Practices 176<br>Beginning the Operations Planning 176<br>Performing a Gap Analysis 176<br>Creating a Technology Plan 177<br><br>152_wan_toc 6/22/01 4:54 PM Page xviii<br>xviii Contents<br>Creating an Integration Plan 178<br>Beginning the Collocation Planning 178<br>Performing a Risk Analysis 179<br>Creating an Action Plan 179<br>Preparing the Planning Deliverables 180<br>Developing the Network Architecture 180<br>Reviewing and Validating the Planning<br>Phase 181<br>Creating a High-Level Topology 181<br>Creating a Collocation Architecture 182<br>Defining the High-Level Services 182<br>Creating a High-Level Physical Design 183<br>Defining the Operations Services 183<br>Creating a High-Level Operating Model 184<br>Evaluating the Products 184<br>Creating an Action Plan 185<br>Creating the Network Architecture<br>Deliverable 186<br>Formalizing the Detailed Design Phase 186<br>Reviewing and Validating the Network<br>Architecture 186<br>Creating the Detailed Topology 187<br>Creating a Detailed Service Collocation<br>Design 188<br>Creating the Detailed Services 188<br>Creating a Detailed Physical Design 189<br>Creating a Detailed Operations Design 190<br>Creating a Detailed Operating Model<br>Design 190<br>Creating a Training Plan 191<br>Developing a Maintenance Plan 192<br>Developing an Implementation Plan 192<br>Creating the Detailed Design Documents 192<br>Understanding Wireless Network Attributes<br>from a Design Perspective 193<br>Application Support 194<br>Subscriber Relationships 196<br><br>152_wan_toc 6/22/01 4:54 PM Page xix<br>Contents xix<br>Physical Landscape 197<br>Network Topology 200<br>Network Security 201<br>Summary 203<br>Solutions Fast Track 204<br>Frequently Asked Questions 206<br>Chapter 6 Designing a Wireless Enterprise<br>Use Two Wireless<br>Network: Hospital Case Study 209<br>Outdoor Routers to<br>Introduction 210<br>Create Redundancy<br>Applying Wireless in an Enterprise Network 210<br>Introducing the Enterprise Case Study 211<br>To Building 100 To Main Hospital11 Mbps link<br>Building 100 Router<br>Assessing the Opportunity 211<br>To Building 101<br>To Building 101<br>Evaluating Network Requirements 213<br>To Building 200<br>Main Hospital Router<br>Assessing the Satellite Buildings’Physical<br>To Building 201<br>Landscape 214<br>To Building 300<br>Evaluating the Outside Physical Landscape 214<br>To Building 301<br>Evaluating the Current Network 216<br>Evaluating the Hospital Conference Room<br>Networking Landscape 216<br>Designing a Wireless Solution 217<br>Project 1:Providing Satellite Building Access 218<br>Project 2:Providing Wireless Technology<br>to the Conference Rooms 219<br>Project 3:Providing Building-to-Building<br>Connectivity 220<br>Describing the Detailed Design of the<br>Building Links 222<br>Implementing and Testing the Wireless Solution 224<br>Project 1:Implementing the Satellite<br>Building LAN Access 224<br>Project 2:Implementing the Hospital<br>Conference Room 224<br>Project 3:Implementing the<br>Building-to-Building Connectivity 225<br>Reviewing the Hospital’s Objectives 227<br>Lessons Learned 228<br><br>152_wan_toc 6/22/01 4:54 PM Page xx<br>xx Contents<br>Summary 229<br>Solutions Fast Track 230<br>Frequently Asked Questions 232<br>Chapter 7 Designing a Wireless Industrial<br>Network: Retail Case Study 233<br>Create an Installation<br>Introduction 234<br>Checklist and Verify<br>the Steps on the List<br>Applying Wireless Technology in an Industrial<br>Network 235<br>Set up the IP<br>Introducing the Industrial Case Study 235<br>information<br>Assessing the Opportunity 236<br>Install the access points<br>Defining the Scope of the Case Study 238<br>Install the AP Manager<br>Reviewing the Current Situation 238<br>software<br>Designing and Implementing the Wireless<br>Test the wireless<br>Network 239<br>network<br>Creating the High-Level Design 239<br>Review the client’s<br>Creating a Detailed Design 240<br>objectives<br>Obtaining a Physical Map 242<br>Determining User Density 247<br>Identifying Constraints 248<br>Conducting the Walk-Through 249<br>Identifying RF Interface Sources 249<br>Plan the RF Pattern for the Network 249<br>Planning the Equipment Placement 250<br>Determining Where to Place the Access<br>Points 251<br>Determining the RF Channel<br>Optimization 254<br>Identifying IP Addresses 255<br>Implementing the Wireless Network 255<br>Selecting the Hardware 256<br>Installing the Wireless Components 258<br>Setting Up IP Information 258<br>Installing the Access Points 258<br>Install the AP Manager Software 260<br>Installing the PC Card in Shipping/<br>Receiving 260<br><br>152_wan_toc 6/22/01 4:54 PM Page xxi<br>Contents xxi<br>Testing the Wireless Network 260<br>Reviewing the Client’s Objectives 261<br>Lessons Learned 262<br>Summary 263<br>Solutions Fast Track 264<br>Frequently Asked Questions 266<br>Chapter 8 Designing a Wireless Campus<br>Network: University Case Study 269<br>Introduction 270<br>Applying Wireless Technology in a Campus<br>Network 270<br>Introducing the Campus Case Study 271<br>Establish High-Level<br>Assessing the Opportunity 271<br>Inter-Building<br>Defining the Scope of the Case Study 272<br>Connectivity<br>Designing the Wireless Campus Network 273<br>The Design Approach 273<br>Field House<br>(Basketball)<br>Liberal Arts<br>Determining the Functional Design<br>Requirements 273<br>Engineering Biological Sciences<br>Tracking the Administration Needs 274<br>Tracking the Athletic Needs 275<br>inistration Building<br>Adm<br>Student Union<br>Tracking the Academic Department<br>Needs 276<br>Internet<br>Tracking Student Union Needs 277<br>200 Meters<br>Tracking Student Needs 277<br>StadiumDorms<br>Constraints and Assumptions 277<br>Identifying the Assumptions 279<br>Identifying the Constraints 281<br>Planning the Equipment Placement:Detailed<br>Design Requirements 283<br>Providing Detailed Administration<br>Requirements 283<br>Providing Detailed Athletic <br>Department Requirements 285<br>Providing Detailed Academic <br>Department Requirements 288<br><br>152_wan_toc 6/22/01 4:54 PM Page xxii<br>xxii Contents<br>Providing Detailed Student Union<br>Department Requirements 290<br>Providing Detailed Student Requirements 291<br>Implementing the Wireless Campus Network 292<br>Implementing the Physical Deployment 293<br>Implementing the Logical Deployment 294<br>Lessons Learned 295<br>Summary 296<br>Solutions Fast Track 297<br>Frequently Asked Questions 299<br>Chapter 9 Designing a Wireless Home <br>Network: Home Office Case Study 301<br>Learn to Build a<br>Introduction 302<br>Wireless Home<br>Network<br>Advantages of a Home Network 302<br>Advantages of a Wireless Home Network 304<br>Assembling the<br>Introducing the Wireless Home Network<br>network components<br>Case Study 305<br>Determining<br>Assessing the Opportunity 305<br>Broadband<br>Defining the Scope of the Case Study 306<br>configuration<br>Designing the Wireless Home Network 306<br>Installing the hardware<br>Determining the Functional Requirements 307<br>Installing and<br>Determining the Needs of Management 307<br>configuring the<br>Determining the Needs of the Family 308<br>software<br>Talking to the IT Department 308<br>Testing the network<br>Creating a Site Survey of the Home 309<br>Assessing the Functional Requirements 310<br>Analyzing the Existing Environment 310<br>Identifying Current Technology Options<br>and Constraints 312<br>Investigating Costs 313<br>Weighing Costs and Benefits 313<br>Assessing the Existing Environment 314<br>Developing a Preliminary Design 315<br>Choosing Vendor Solutions 317<br>Developing a Detailed Design 318<br>Implementing the Wireless Home Network 319<br><br>152_wan_toc 6/22/01 4:54 PM Page xxiii<br>Contents xxiii<br>Assembling the Network Components 319<br>Determining Broadband Configuration 320<br>Installing the Hardware 321<br>Installing and Configuring the Software 322<br>Installing and Configuring the Software<br>for the Home Firewall 322<br>Installing and Configuring the Software<br>for the Wireless Access Point 324<br>Testing the Network 326<br>Designing a Wireless Home Network for Data,<br>Voice,and Beyond 326<br>Current State of the Home Wireless<br>Marketplace 327<br>A Proposed Solution for the Future 329<br>Lessons Learned 330<br>Summary 332<br>Solutions Fast Track 332<br>Frequently Asked Questions 335<br>Designing a Wireless Network Fast Track 337<br>Index 357
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值