您的位置:首页 > 其它

Codeforces Round #134 (Div. 2)

2012-08-18 17:40 302 查看
A. Mountain Scenery

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Little Bolek has found a picture with n mountain peaks painted on it. The n painted peaks are represented by a non-closed polyline, consisting of 2n segments. The segments go through 2n + 1 points with coordinates (1, y1), (2, y2), ..., (2n + 1, y2n + 1), with the i-th segment connecting the point (i, yi) and the point (i + 1, yi + 1). For any even i (2 ≤ i ≤ 2n) the following condition holds: yi - 1 < yi and yi > yi + 1.

We shall call a vertex of a polyline with an even x coordinate a mountain peak.

View Code

#include <iostream>
#include<cstdio>
#include<string.h>
using namespace std;
struct node
{
int x,y;
}q[110];
int father[110];
int find(int x)
{
if(x!=father[x])
father[x] = find(father[x]);
return father[x];
}
int main()
{
int n,i,j,k,num = 0;
scanf("%d", &n);
for(i = 1; i <= n ;i++)
{
scanf("%d%d",&q[i].x,&q[i].y);
}
for(i = 1; i <= n ;i++)
father[i] = i;
for(i = 1; i < n ; i++)
{
for(j = i+1; j <= n ;j++)
{
if(q[j].x==q[i].x||q[j].y==q[i].y)
{
int px = find(i);
int py = find(j);
if(px!=py);
father[px] = py;
}
}
}
for(i = 1; i <= n ; i++)
if(father[i] == i)
num++;
printf("%d\n",num-1);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: