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. UML建模工具-火龙果软件

     官网地址:http://code.uml.com.cn/index.asp     Bridge桥梁模式    (待逆向) 桥梁模式,通过增加一个类,将抽象部分与它的实现部分分离,使它们都可以独立 ...

  2. QQ聊天界面的布局和设计(IOS篇)-第二季

    QQChat Layout - 第二季 本来第二季是快写好了, 也花了点功夫, 结果gitbook出了点问题, 给没掉了.有些细节可能会一带而过, 如有疑问, 相互交流进步~. 在第一季中我们完成了Q ...

  3. 浅谈C++调用C#的DLL程序方法

    把C#编译成DLL或者Axtive控件,再由C调用!比如使用C++调用C#的DLL. SwfDotNet是.net下输出flash的类库.SwfDotNet是C#编写的,作者的C#水平,真是令我佩服. ...

  4. 从手工测试逆袭为NB自动化测试的学习路线

    在开始之前先学习两个工具商业web自动化测试工具请学习QTP:QTP的学习可以跳过,我是跳过了的.开源web自动化测试工具请学习Selenium:我当年是先学watir,再学selenium 这里主要 ...

  5. vps安全设置

    适合新手及才接触VPS的朋友们看一下.主要是关于VPS安全方面相关内容的 禁止ROOT登陆 保证安全性. 使用DDoS deflate简单防攻击. iftop Linux流量监控工具: 每日自己主动备 ...

  6. hdu 1728 逃离迷宫(dFS+优先队列)

    求转弯最少的走路方式!!!! #include<stdio.h> #include<string.h> #include<queue> using namespac ...

  7. HDU 4122 Alice's mooncake shop (单调队列/线段树)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4122 题意:好难读懂,读懂了也好难描述,亲们就自己凑合看看题意把 题解:开始计算每个日期到2000/1/ ...

  8. Why Hadoop2

    自从Hadoop2出现之后,其迅速代替了Hadoop1的地位,并丰富了Hadoop的应用场景.假设如今有公司使用Hadoop的话,往往直接採用Hadoop2了. Hadoop2能被如此广泛的使用,肯定 ...

  9. Android应用程序启动过程源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6689748 前文简要介绍了Android应用程 ...

  10. .Net+EF连接Oracle 10、11 开发,ODAC安装配置与使用详解

    Oracle数据访问组件 (ODAC)  全称:Oracle Data Access Components 优点:由于微软在.net framework4中会将System.Data.OracleCl ...