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. [转]jQuery EasyUI 扩展-- 主题(Themes)

    主题(Themes)允许您改变站点的外观和感观.使用主题可以节省设计的时间,让您腾出更多的时间进行开发.您也可以创建一个已有主题的子主题. 主题生成器(Theme Builder) jQuery UI ...

  2. Android实现Live Photos 加源代码

    在Android手机上实现类似于iphone中的LivePhoto的功能 源代码分享 github:https://github.com/amazingyyc/DeepRed 代码说明: 1.改变视频 ...

  3. php用apc实现的临界区 解决并发,资源互斥同步访问

    在面对线程或进程的互斥同步的控制问题时,常用的解决办法是:临界区,互斥锁,信号量 临界区保证在某一时刻只有一个线程能够访问到所需资源的方法. 任何时候,只能至多有一个线程处于临界区中.如果多个线程要求 ...

  4. jdbc资料收集

    1.Hibernate史上最简单的Hibernate入门简介http://blog.csdn.net/doodoofish/article/details/43207/ jdbc不足 尽管JDBC在J ...

  5. java生成字符串md5函数类(javaSE)

    //实现生成MD5值 import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.B ...

  6. SVN版本分支合并

    SVN,开发中常用的工具,也没什么可说的.这里只是记录一下,以免太久不用了想用的时候又忘了. 首先已经有两个目录,一个是分支目录SVNChild,一个是主干目录SVNMain.SVNChild是从SV ...

  7. jquery简单切换插件

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  8. Response.ContentType 详细列表 (转载)

    不同的ContentType 会影响客户端所看到的效果.默认的ContentType为 text/html 也就是网页格式.代码如: <% response.ContentType =" ...

  9. Linux串口编程のtermios 结构

    termios 结构是在POSIX规范中定义的标准接口,它类似于系统V中的termio接口,通过设置termios类型的数据结构中的值和使用一小 组函数调用,你就可以对终端接口进行控制. 可以被调整来 ...

  10. UIStepper UISlider UISwitch UITextField 基本控件

    1.UIStepper 步进控件 必掌握 1.重要属性: .value 初始值 .maximumValue 最大值 .minimumValue 最小值 .stepValue 间隔 2.常用事件: Va ...