(比赛)A - Simple String Problem

A - Simple String Problem

Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Practice POJ 2236

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

//题意是,电脑都坏了,可以一个一个修复,电脑只能在一定的距离内才能连通,在询问是否连通的时候输出是否连通。第一行是 n ,d ,d 是代表电脑能连通的最大距离,然后是 n 行坐标,在然后是命令 O 代表修复电脑,S 代表查询两个电脑是否连通

并查集简单的应用,压缩了路径就只用了1秒1125 ms

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <math.h>
 4 using namespace std;
 5
 6 struct Com
 7 {
 8     int x,y;
 9     int on;
10 }com[1005];
11 int p[1005];
12
13 int find(int x)
14 {
15     if (x!=p[x])
16         p[x]=find(p[x]);
17     return p[x];
18 }
19
20 int Distance(int a,int b,double x)
21 {
22     double j=(com[a].x-com[b].x)*(com[a].x-com[b].x);
23     double k=(com[a].y-com[b].y)*(com[a].y-com[b].y);
24     double l=sqrt(j+k);
25     if (x<l)
26         return 1;
27     return 0;
28 }
29
30 int main()
31 {
32     int n,i;
33     double s;
34     scanf("%d%lf",&n,&s);
35     for (i=1;i<=n;i++)
36     {
37         scanf("%d%d",&com[i].x,&com[i].y);
38         com[i].on=0;
39         p[i]=i;
40     }
41     char str[2];
42     int k;
43     while(scanf("%s",str)!=EOF)
44     {
45         if (str[0]==‘O‘)
46         {
47             scanf("%d",&k);
48             if (com[k].on==1)
49                 continue;
50             com[k].on=1;
51             for (i=1;i<=n;i++)
52             {
53                 if (i==k||com[i].on==0)   //未修复
54                     continue;
55                 if (Distance(i,k,s))//距离超出
56                      continue;
57                 int fa=find(k);
58                 int fb=find(i);
59                   p[fa]=fb;
60             }
61         }
62         if (str[0]==‘S‘)
63         {
64             int a,b;
65             scanf("%d%d",&a,&b);
66             int fa=find(a),fb=find(b);
67             if (fa==fb)
68             {
69                 printf("SUCCESS\n");
70                 //没有这种情况
71             /*  if (a!=b)
72                     printf("SUCCESS\n");
73                 else if (a==b&&com[a].on)
74                     printf("SUCCESS\n");
75                 else
76                     printf("FAIL\n");*/
77             }
78             else
79                 printf("FAIL\n");
80         }
81     }
82     return 0;
83 }


 
 
时间: 2024-10-13 01:24:09

(比赛)A - Simple String Problem的相关文章

Water --- CSU 1550: Simple String

Simple String Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1550 Mean: 略. analyse: 水题. Time complexity: O(n) Source code:  // Memory Time // 1347K 0MS // by : crazyacking // 2015-03-29-12.08 #include<map> #include<queue> #

csu 1550: Simple String (字符串)

1550: Simple String Time Limit: 1 Sec  Memory Limit: 256 MB Submit: 249  Solved: 112 [Submit][Status][Web Board] Description Welcome,this is the 2015 3th Multiple Universities Programming Contest ,Changsha ,Hunan Province. In order to let you feel fu

hdu 5008(2014 ACM/ICPC Asia Regional Xi&#39;an Online ) Boring String Problem(后缀数组&amp;二分)

Boring String Problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 219    Accepted Submission(s): 45 Problem Description In this problem, you are given a string s and q queries. For each que

hdu 1757 A Simple Math Problem (乘法矩阵)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2441    Accepted Submission(s): 1415 Problem Description Lele now is thinking about a simple function f(x).If x < 10 f(x) =

FZU 2215 Simple Polynomial Problem (多项式乘法 栈)

Problem 2215 Simple Polynomial Problem Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description You are given an polynomial of x consisting of only addition marks, multiplication marks, brackets, single digit numbers, and of course the le

hdu1757 A Simple Math Problem(矩阵快速幂)

题目: A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3522    Accepted Submission(s): 2130 Problem Description Lele now is thinking about a simple function f(x). If x < 10 f

hdu 5008 Boring String Problem(后缀自动机构造后缀树)

hdu 5008 Boring String Problem(后缀自动机构造后缀树) 题意:给出一个字符串s,然后每次询问一个k,求s的所有子串中,字典序第k小的是谁?多个解,则输出最左边的那个 解题思路:这道题应该是为后缀树量身定制的吧.只要构造出了后缀树,然后按字典序遍历就可以得出每个节点包含的子串的字典序的范围了,而且必然是个连续的区间范围.但是我不会后缀树啊..比赛的时候突然想到,后缀自动机是可以构造后缀树的,虽然以前没写过,但还是硬着头皮上吧,居然还真的让我给撸出来了.我的做法是这样的

hdu-5772 String problem(最大权闭合子图)

题目链接: String problem Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Problem Description This is a simple problem about string. Now a string S contains only ‘0’-‘9’. ?? wants to select a subsequence from this stri

BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】

A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 368664-bit integer IO format: %lld      Java class name: Main Prev Submit Status Statistics Discuss Next Type: None None Graph Theory 2-SA