24h購物| | PChome| 登入
2013-06-26 13:39:56| 人氣805| 回應1 | 上一篇 | 下一篇

[UVA] 10301 - Rings and Glue

推薦 0 收藏 0 轉貼0 訂閱站台

Problem B

Rings and Glue

Input: standard input

Output: standard output

Time Limit: 1 second

Memory Limit: 32 MB

 

Little John is in big trouble. Playing with his different-sized (and colored!) rings and glue seemed such a good idea. However, the rings now lay on the floor, glued together with some-thing that will definitely not come off with water. Surprisingly enough, it seems like no rings are actually glued to the floor, only to other rings. How about that!

 

You must help Little John to pick the rings off the floor before his mom comes home from work. Since the glue is dry by now, it seems like an easy enough task. This is not the case. Little John is an irrational kid of numbers, and so he has decided to pick up the largest component (most rings) of glued-together rings first. It is the number of rings in this largest component you are asked to find. Two rings are glued together if and only if they overlap at some point but no rings will ever overlap in only a single point. All rings are of the doughnut kind (with a hole in them). They can however, according to Little John, be considered “infinitely thin”.

 

Input

Input consists of a number (>0) of problems. Each problem starts with the number of rings, n, where 0 £ n < 100. After that, n rows follow, each containing a ring’s physical attributes. That is, 3 floating point numbers, with an arbitrary number of spaces between them, describing the x coordinate and y coordinate for its center and its radius. All floating point numbers fit into the standard floating point type of your favorite programming language (e.g., float for C/C++). Input ends with a single row with the integer -1.

 

Output

Output consists of as many grammatically correct answers as there were problems, each answer, on a separate line, being ‘The largest component contains X ring(s).’ where X is the number of rings in the largest component.

 

Sample Input

4

0.0 0.0 1.0

-1.5 -1.5 0.5

1.5 1.5 0.5

-2.0 2.0 3.5

3

3.0 2.0 2.0

0.0 -0.5 1.0

0.0 0.0 2.0

5

-2.0 0.0 1.0

1.0 -1.0 1.0

0.0 1.0 0.5

2.0 0.0 1.0

-1.0 1.0 1.0

-1

 

Sample Output

The largest component contains 4 rings.

The largest component contains 2 rings.

The largest component contains 3 rings.


(Joint Effort Contest, Problem Source: Swedish National Programming Contest, arranged by department of Computer Science at Lund Institute of Technology.)

戒子掉在地方, 問一次最多可以拿起幾個。

在如果兩個圓有相交的點的話, 則可以被同時拿起, 即計算圖的連通元件。

判斷的時候內離不算交集。

輸出有點糟糕, 在答案是 0 的時候, rings 只能說測資不好。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;
int used[105] = {}, cnt;
vector<int> g[105];
void dfs(int nd) {
    cnt++;
    used[nd] = 1;
    int i;
    for(vector<int>::iterator it = g[nd].begin();
        it != g[nd].end(); it++) {
        if(used[*it] == 0) {
            dfs(*it);
        }
    }
}
int main() {
    int n, i, j;
    double x[105], y[105], r[105];
    while(scanf("%d", &n) == 1 && n >= 0) {
        for(i = 0; i < n; i++)
            scanf("%lf %lf %lf", &x[i], &y[i], &r[i]);
        for(i = 0; i < n; i++)
            g[i].clear();
        int ret = 0;
        memset(used, 0, sizeof(used));
        for(i = 0; i < n; i++) {
            for(j = i+1; j < n; j++) {
                double a= r[i], b = r[j], c = hypot(x[i]-x[j], y[i]-y[j]);
#define eps 1e-9
                if(a+b+eps >= c && fabs(a-b) <= c+eps) {
                    g[j].push_back(i);
                    g[i].push_back(j);
                }
            }
        }
        for(i = 0; i < n; i++) {
            if(used[i] == 0) {
                cnt = 0;
                dfs(i);
                ret = max(ret, cnt);
            }
        }
        if(ret != 1)
            printf("The largest component contains %d rings.\n", ret);
        else
            puts("The largest component contains 1 ring.");
    }
    return 0;
}
/*
....1 ring.
....0 rings.
....x rings.
*/

台長: Morris
人氣(805) | 回應(1)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][二分] 10649 - Danger Point
此分類上一篇:[UVA][博奕dp] 1500 - Alice and Bob

TheodoreJohn
ate and y coordinate for its center and its radius. All floating point numbers fit into the standard floating point type of your favorite programming language (e.g., float for C/C++). Input ends with a single row with the integer -1.
2021-12-19 00:09:53
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文