Easy billiards


Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge

Edward think a game of billiards is too long and boring. So he invented a new game called Easy billiards.

Easy billiards has N balls on a brimless rectangular table in the beginning, and your goal is try to make the number of balls on the table as least as possible by several hit under the following rules:

1: The direction you hit the balls should parallel to the tables border.

2: If ball A crashed into ball B, ball B will moves in the same direction of ball A before the crashing, and ball A will stop in the place of ball B before the crashing.

3: If ball C is moving and there are no balls in front of ball C, it will runs out of the tables border, that means ball C is out of the table.

4: You can choose arbitrary ball on the table to hit, but on a hit, you can't let the ball you choose to hit runs out of the tables border. In another word, a ball could runs out of the table if and only if it was crashed by another ball in a hitting.

Now, Edward wants to know the least number of balls remained on the table after several hits, and how.

Input

There are multiple test cases. For each test cases, in the first line, there is an integer N, which means the number of the balls on the table. There are following N lines, each line contains two integers Xi and Yi, which means the coordinate of ball I. (0<=N<=2000, 0<=Xi, Yi<=10^8)

Output

For each test cases, you should output the least number of balls on the first line. And you should output several lines to show the order of hits following the first line, each line should contains the coordinate of the ball you choose to hit and the direction you hit. (LEFT,RIGHT,UP,DOWN).

Sample Input

4
0 0
2 0
4 0
2 2
9
1 1
2 1
3 1
1 2
2 2
3 2
1 3
2 3
3 3

Sample output

1
(2, 2) DOWN
(4, 0) LEFT
(2, 0) LEFT
1
(1, 3) DOWN
(1, 2) DOWN
(2, 3) DOWN
(2, 2) DOWN
(3, 3) DOWN
(3, 2) DOWN
(3, 1) LEFT
(2, 1) LEFT

题意:一块区域内,通过撞球来使区域内球数最少。撞球规则:A球在B球右边而B球右边没球存在,这时,向右撞击A球,A球会撞击B球,A球会停在B球的位置上,B球则被撞出这片区域。
思路:先并查集得到最少撞球数。再DFS遍历树得到撞球方向。
收获:注意标记位置的放法,用链式前向星建无向图。
 #include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
using namespace std;
#define maxn 10000
#define maxm 1000000
int n,num;
struct Node
{
int x;
int y;
};
Node node[maxn];
int root[maxn];
void init_root()
{
for(int i=;i<n;i++)
root[i]=i;
}
int findroot(int x)
{
if(x!=root[x])
{
root[x]=findroot(root[x]);
}
return root[x];
}
void merge_(int a,int b)
{
int x=findroot(a);
int y=findroot(b);
if(x==y)
return;
else
root[y]=x;
}
struct Edge
{
int u,v,next;
};
Edge edge[maxm];
int head[maxm];
int vis[maxm];
void init_edge()
{
num=;
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
}
void addedge(int u,int v)
{
edge[num].u=u;
edge[num].v=v;
edge[num].next=head[u];
head[u]=num++;
edge[num].u=v;
edge[num].v=u;
edge[num].next=head[v];
head[v]=num++;
}
void dfs(int pre,int u)
{
//printf("%d%d\n",node[u].x,node[u].y);
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].v;
if(!vis[v])
{
dfs(u,v);
//vis[v]=1;
}
}
if(pre!=-)
{
printf("(%d, %d) ",node[u].x,node[u].y);
if(node[pre].x>node[u].x)
printf("RIGHT\n");
if(node[pre].x<node[u].x)
printf("LEFT\n");
if(node[pre].x==node[u].x)
{
if(node[pre].y>node[u].y)
printf("UP\n");
else
printf("DOWN\n");
}
} }
int main()
{
while(~scanf("%d",&n))
{
for(int i=;i<n;i++)
scanf("%d%d",&node[i].x,&node[i].y);
init_root();
init_edge();
for(int i=;i<n;i++)
for(int j=;j<i;j++)
{
if(node[i].x==node[j].x||node[i].y==node[j].y)
{
merge_(i,j);
addedge(i,j);
}
}
memset(vis,,sizeof(vis));
int ans=;
for (int i=;i<n;i++)
if (root[i]==i) ans++;
printf("%d\n",ans);
for(int i=;i<n;i++)
if(root[i]==i)
{
dfs(-,i);
}
}
return ;
}

ZOJ3761(并查集+树的遍历)的更多相关文章

  1. 洛谷P4092 [HEOI2016/TJOI2016]树 并查集/树链剖分+线段树

    正解:并查集/树链剖分+线段树 解题报告: 传送门 感觉并查集的那个方法挺妙的,,,刚好又要复习下树剖了,所以就写个题解好了QwQ 首先说下并查集的方法趴QwQ 首先离线,读入所有操作,然后dfs遍历 ...

  2. BZOJ-3211花神游历各国 并查集+树状数组

    一开始想写线段树区间开方,简单暴力下,但觉得变成复杂度稍高,懒惰了,编了个复杂度简单的 3211: 花神游历各国 Time Limit: 5 Sec Memory Limit: 128 MB Subm ...

  3. 51 nod 1427 文明 (并查集 + 树的直径)

    1427 文明 题目来源: CodeForces 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 160 难度:6级算法题   安德鲁在玩一个叫“文明”的游戏.大妈正在帮助他. 这个游 ...

  4. BZOJ3211 花神游历各国 并查集 树状数组

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3211 题意概括 有n个数形成一个序列. m次操作. 有两种,分别是: 1. 区间开根(取整) 2. ...

  5. hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)

    hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...

  6. 【bzoj4869】[Shoi2017]相逢是问候 扩展欧拉定理+并查集+树状数组

    题目描述 Informatik verbindet dich und mich. 信息将你我连结. B君希望以维护一个长度为n的数组,这个数组的下标为从1到n的正整数.一共有m个操作,可以分为两种:0 ...

  7. CodeForces 455C Civilization(并查集+树直径)

    好久没有写过图论的东西了,居然双向边要开两倍空间都忘了,不过数组越界cf居然给我报MLE??这个题题意特别纠结,一开始一直不懂添加的边长是多长... 题意:给你一些点,然后给一些边,注意没有重边 环, ...

  8. 并查集+树链剖分+线段树 HDOJ 5458 Stability(稳定性)

    题目链接 题意: 有n个点m条边的无向图,有环还有重边,a到b的稳定性的定义是有多少条边,单独删去会使a和b不连通.有两种操作: 1. 删去a到b的一条边 2. 询问a到b的稳定性 思路: 首先删边考 ...

  9. HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...

随机推荐

  1. 【剑指offer】面试题31:连续子数组的最大和

    题目: 在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢?例如:{6,-3,-2, ...

  2. 《Algorithms 4th Edition》读书笔记——3.1 符号表(Elementary Symbol Tables)-Ⅰ

    3.1符号表 符号表最主要的目的就是将一个键和一个值联系起来.用例能够将一个键值对插入符号表并希望在之后能够从符号表的所有键值对中按照键值姐找到对应的值.要实现符号表,我们首先要定义其背后的数据结构, ...

  3. Fancy

    Fancy 在欢喜您可以轻易发现并购买由全球最具品味的社区为您挑选的精美商品.

  4. LinQ 语法基础

    LINQ (Language-Integrated Query,语言集成查询). LINQ to Objects.LINQ to SQL.LINQ to DataSet和LINQ to XML,它们分 ...

  5. java 集合专练

    handsomecui的blog地址为:http://www.cnblogs.com/handsomecui/ 本人网站为:handsomecui.top 引言:本次主要练习单列集合:Collecti ...

  6. Super Jumping! Jumping! Jumping!(dp)

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  7. AFNetwork 作用和使用方法具体解释

    转自:http://www.maxiaoguo.com/clothes/269.html AFNetworking是一个轻量级的iOS网络通信类库.它建立在NSURLConnection和NSOper ...

  8. 5. openCV中常用函数学习

    一.前言 经过两个星期的努力,一边学习,一边写代码,初步完成了毕业论文系统的界面和一些基本功能,主要包括:1 数据的读写和显示,及相关的基本操作(放大.缩小和移动):2 样本数据的选择:3 数据归一化 ...

  9. Android Service(下)

    转载请注册出处:http://blog.csdn.net/guolin_blog/article/details/9797169 在上一篇文章中,我们学习了Android Service相关的许多重要 ...

  10. CDMA电信短信猫支持189等电信号码可二次开发

    CDMA电信短信猫支持189等电信号码可二次开发 这款短信猫采用法国wavecom Q2358C模块,支持短信猫二次开发,可提供短信猫二次开发包下载测试.提供相关资料文档. 此款CDMA电信短信猫有串 ...