10301 - Rings and Glue
Moderator: Board moderators
10301 - Rings and Glue
The problem says to output the answer in a gramatically correct sentence.
When X=0, the expected answer is "The largest component contains 0 ring.". This is gramatically incorrect, and should be "0 rings".
When X=0, the expected answer is "The largest component contains 0 ring.". This is gramatically incorrect, and should be "0 rings".
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany
10301
Problem:Rings and Glue
I think it's a very easy problem.I built a graph first, then use DFS to calc the largest component.
[pascal]
const
name1 = '';
name2 = '';
maxn = 100;
var
map : array[1 .. maxn, 1 .. maxn] of boolean;
a,b,c : array[1 .. maxn] of real;
used : array[1 .. maxn] of boolean;
n,res : longint;
procedure init;
var i : integer;
begin
readln(n);
for i := 1 to n do readln(a, b, c);
end;
function covered( p, q : integer) : byte;
var tmp : real;
begin
tmp := sqr(a[p] - a[q]) + sqr(b[p] - b[q]);
if tmp > sqr(c[p] + c[q]) then covered := 0
else if tmp < sqr(c[p] - c[q]) then covered := 2
else covered := 1;
end;
function search( p : integer) : integer;
var i,j : integer;
begin
used[p] := true; j := 1;
for i := 1 to n do
if map[p, i] and not used then
j := j + search(i);
search := j;
end;
procedure main;
var i,j,k : integer;
begin
fillchar(used, sizeof(used), false);
fillchar(map, sizeof(map), false);
for i := 1 to n - 1 do
for j := i + 1 to n do begin
k := covered(i,j);
if k = 1 then begin map[i, j] := true; map[j, i] := true; end;
end;
res := 0;
for i := 1 to n do
if not used then begin
j := search(i);
if j > res then res := j;
end;
end;
procedure print;
begin
if res > 1 then writeln('The largest component contains ', res, ' rings.')
else writeln('The largest component contains ', res, ' ring.');
end;
begin
assign(input, name1);
reset(input);
assign(output, name2);
rewrite(output);
repeat
init;
if n >= 0 then begin
main;
print;
end;
until n = -1;
close(input);
close(output);
end.
[/pascal]
I think it's a very easy problem.I built a graph first, then use DFS to calc the largest component.
[pascal]
const
name1 = '';
name2 = '';
maxn = 100;
var
map : array[1 .. maxn, 1 .. maxn] of boolean;
a,b,c : array[1 .. maxn] of real;
used : array[1 .. maxn] of boolean;
n,res : longint;
procedure init;
var i : integer;
begin
readln(n);
for i := 1 to n do readln(a, b, c);
end;
function covered( p, q : integer) : byte;
var tmp : real;
begin
tmp := sqr(a[p] - a[q]) + sqr(b[p] - b[q]);
if tmp > sqr(c[p] + c[q]) then covered := 0
else if tmp < sqr(c[p] - c[q]) then covered := 2
else covered := 1;
end;
function search( p : integer) : integer;
var i,j : integer;
begin
used[p] := true; j := 1;
for i := 1 to n do
if map[p, i] and not used then
j := j + search(i);
search := j;
end;
procedure main;
var i,j,k : integer;
begin
fillchar(used, sizeof(used), false);
fillchar(map, sizeof(map), false);
for i := 1 to n - 1 do
for j := i + 1 to n do begin
k := covered(i,j);
if k = 1 then begin map[i, j] := true; map[j, i] := true; end;
end;
res := 0;
for i := 1 to n do
if not used then begin
j := search(i);
if j > res then res := j;
end;
end;
procedure print;
begin
if res > 1 then writeln('The largest component contains ', res, ' rings.')
else writeln('The largest component contains ', res, ' ring.');
end;
begin
assign(input, name1);
reset(input);
assign(output, name2);
rewrite(output);
repeat
init;
if n >= 0 then begin
main;
print;
end;
until n = -1;
close(input);
close(output);
end.
[/pascal]
10301 Rings and Glue - Inconsistent problem statement
Problem statement says "Input consists of a number (>0) of problems. " (i.e. number of test cases). Sample input does not ![:)](./images/smilies/icon_smile.gif)
bolster
![:)](./images/smilies/icon_smile.gif)
bolster
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany
10301
Why does this code get WA? Is anything I didn't consider yet?
[c]
#include<stdio.h>
#include<math.h>
#define SQUARE(x) ((x)*(x))
int place[99],num[99],n;
void put(int,int);
void main(void)
{
int x,y,max;
double data[99][3],r1,r2,d;
while(1)
{
scanf("%d",&n);
if(n<0)
break;
if(n==0)
{
printf("The largest component contains 0 ring.\n");
continue;
}
for(x=0;x<99;x++)
place[x]=-1;
for(x=0;x<n;x++)
place[x]=x,num[x]=1;
for(x=0;x<n;x++)
scanf("%lf %lf %lf",&data[x][0],&data[x][1],&data[x][2]);
for(x=0;x<n;x++)
for(y=x+1;y<n;y++)
{
r1=data[x][2],r2=data[y][2],d=sqrt(SQUARE(data[x][0]-data[y][0])+SQUARE(data[x][1]-data[y][1]));
if(r1+r2>d && r1+d>r2 && r2+d>r1)
if(place[x]!=place[y])
put(x,y);
}
for(x=0,max=0;x<n;x++)
if(num[x]>max)
max=num[x];
if(max>1)
printf("The largest component contains %d rings.\n",max);
else
printf("The largest component contains 1 ring.\n");
}
}
void put(int i,int j)
{
int a,b,x,temp,temp2;
a=num;
b=num[j];
temp=place;
temp2=place[j];
for(x=0;x<n;x++)
if(place[x]==temp || place[x]==temp2)
{
place[x]=temp;
num[x]=a+b;
}
}
[/c]
[c]
#include<stdio.h>
#include<math.h>
#define SQUARE(x) ((x)*(x))
int place[99],num[99],n;
void put(int,int);
void main(void)
{
int x,y,max;
double data[99][3],r1,r2,d;
while(1)
{
scanf("%d",&n);
if(n<0)
break;
if(n==0)
{
printf("The largest component contains 0 ring.\n");
continue;
}
for(x=0;x<99;x++)
place[x]=-1;
for(x=0;x<n;x++)
place[x]=x,num[x]=1;
for(x=0;x<n;x++)
scanf("%lf %lf %lf",&data[x][0],&data[x][1],&data[x][2]);
for(x=0;x<n;x++)
for(y=x+1;y<n;y++)
{
r1=data[x][2],r2=data[y][2],d=sqrt(SQUARE(data[x][0]-data[y][0])+SQUARE(data[x][1]-data[y][1]));
if(r1+r2>d && r1+d>r2 && r2+d>r1)
if(place[x]!=place[y])
put(x,y);
}
for(x=0,max=0;x<n;x++)
if(num[x]>max)
max=num[x];
if(max>1)
printf("The largest component contains %d rings.\n",max);
else
printf("The largest component contains 1 ring.\n");
}
}
void put(int i,int j)
{
int a,b,x,temp,temp2;
a=num;
b=num[j];
temp=place;
temp2=place[j];
for(x=0;x<n;x++)
if(place[x]==temp || place[x]==temp2)
{
place[x]=temp;
num[x]=a+b;
}
}
[/c]
-
- New poster
- Posts: 17
- Joined: Fri May 24, 2002 4:24 am
- Location: Taiwan
- Contact:
-
- New poster
- Posts: 1
- Joined: Tue Sep 24, 2002 12:39 pm
- Location: Slovenia
10301 - wrong sample output?
Why is the answer to the 2nd testcase:
3
3.0 2.0 2.0
0.0 -0.5 1.0
0.0 0.0 2.0
"The largest component contains 2 rings."?
Isn't the 3rd ring (0.0,0.0,2.0) glued to both the first and the second one? According to my opinion the answer should be "3".
And what's with "0 ring/rings" again?
Thanks, Mojca
3
3.0 2.0 2.0
0.0 -0.5 1.0
0.0 0.0 2.0
"The largest component contains 2 rings."?
Isn't the 3rd ring (0.0,0.0,2.0) glued to both the first and the second one? According to my opinion the answer should be "3".
And what's with "0 ring/rings" again?
Thanks, Mojca
-
- Experienced poster
- Posts: 151
- Joined: Wed Aug 21, 2002 12:07 am
- Location: Seoul, Korea
- Contact:
What's wrong with my code?
I get a WA with this code. What's wrong with my code? I'm wondering why..
I build a graph and use dfs to find the number of the vertices included in the maximum component. I did everything that seems suitable..
(though I really hesitated to post a code.. anyway,)
[c]
#include <stdio.h>
int n;
int graph[100][100];
int visited[100];
double y[100], x[100], radius[100];
int readInput(void)
{
int i;
scanf("%d", &n);
if(n == -1)
return 0;
for(i = 0; i < n; i++)
{
scanf("%lf %lf %lf", &x, &y, &radius);
}
return 1;
}
double sqr(double a)
{
return a*a;
}
double max(double a, double b)
{
if(a > b)
return a;
return b;
}
double min(double a, double b)
{
return -max(-a, -b);
}
double equal(double a, double b)
{
if(a > b)
return a - b < 0.000001;
return b - a < 0.000001;
}
int intersect(double y1, double x1, double r1, double y2, double x2, double r2)
{
double dist = sqr(y1-y2)+sqr(x1-x2);
if(equal(x1, x2) && equal(y1, y2) && equal(r1, r2))
return 1;
if(dist < max(r1, r2))
{
return dist + min(r1, r2) > max(r1, r2);
}
else
return dist < sqr(r1+r2);
}
int visit(int cur)
{
int ret = 1, i;
visited[cur] = 1;
for(i = 0; i < n; i++)
if(graph[cur] && !visited)
ret += visit(i);
return ret;
}
void solve(void)
{
int i, j;
int max = 0, res;
/* build a graph */
for(i = 0; i < n-1; i++)
{
graph = 0;
for(j = i+1; j < n; j++)
{
graph[j] = graph[j] = intersect(x, y[i], radius[i], x[j], y[j], radius[j]);
}
}
for(i = 0; i < n; i++)
visited[i] = 0;
for(i = 0; i < n; i++)
if(!visited[i])
{
res = visit(i);
if(res > max)
max = res;
}
if(max <= 1)
printf("The largest component contains %d ring.\n", max);
else
printf("The largest component contains %d rings.\n", max);
}
int main(void)
{
while(readInput())
solve();
return 0;
}
[/c]
I build a graph and use dfs to find the number of the vertices included in the maximum component. I did everything that seems suitable..
(though I really hesitated to post a code.. anyway,)
[c]
#include <stdio.h>
int n;
int graph[100][100];
int visited[100];
double y[100], x[100], radius[100];
int readInput(void)
{
int i;
scanf("%d", &n);
if(n == -1)
return 0;
for(i = 0; i < n; i++)
{
scanf("%lf %lf %lf", &x, &y, &radius);
}
return 1;
}
double sqr(double a)
{
return a*a;
}
double max(double a, double b)
{
if(a > b)
return a;
return b;
}
double min(double a, double b)
{
return -max(-a, -b);
}
double equal(double a, double b)
{
if(a > b)
return a - b < 0.000001;
return b - a < 0.000001;
}
int intersect(double y1, double x1, double r1, double y2, double x2, double r2)
{
double dist = sqr(y1-y2)+sqr(x1-x2);
if(equal(x1, x2) && equal(y1, y2) && equal(r1, r2))
return 1;
if(dist < max(r1, r2))
{
return dist + min(r1, r2) > max(r1, r2);
}
else
return dist < sqr(r1+r2);
}
int visit(int cur)
{
int ret = 1, i;
visited[cur] = 1;
for(i = 0; i < n; i++)
if(graph[cur] && !visited)
ret += visit(i);
return ret;
}
void solve(void)
{
int i, j;
int max = 0, res;
/* build a graph */
for(i = 0; i < n-1; i++)
{
graph = 0;
for(j = i+1; j < n; j++)
{
graph[j] = graph[j] = intersect(x, y[i], radius[i], x[j], y[j], radius[j]);
}
}
for(i = 0; i < n; i++)
visited[i] = 0;
for(i = 0; i < n; i++)
if(!visited[i])
{
res = visit(i);
if(res > max)
max = res;
}
if(max <= 1)
printf("The largest component contains %d ring.\n", max);
else
printf("The largest component contains %d rings.\n", max);
}
int main(void)
{
while(readInput())
solve();
return 0;
}
[/c]
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany
-
- Experienced poster
- Posts: 151
- Joined: Wed Aug 21, 2002 12:07 am
- Location: Seoul, Korea
- Contact:
I forgot to mention that..
I forgot to mention that I tried everything that was posted..
code with
[c]
if(max == 1)
printf("The largest component contains %d ring.\n", max);
else
printf("The largest component contains %d rings.\n", max);
[/c]
instead of
[c]
if(max <= 1)
printf("The largest component contains %d ring.\n", max);
else
printf("The largest component contains %d rings.\n", max);
[/c]
also results in WA.
What shall I do?![:(](./images/smilies/icon_frown.gif)
(Oops.. I thought "0 ring" was gramatically correct.. --; )
code with
[c]
if(max == 1)
printf("The largest component contains %d ring.\n", max);
else
printf("The largest component contains %d rings.\n", max);
[/c]
instead of
[c]
if(max <= 1)
printf("The largest component contains %d ring.\n", max);
else
printf("The largest component contains %d rings.\n", max);
[/c]
also results in WA.
What shall I do?
![:(](./images/smilies/icon_frown.gif)
(Oops.. I thought "0 ring" was gramatically correct.. --; )