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. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅴ

    命题Q.对于一个含有N个元素的基于堆叠优先队列,插入元素操作只需要不超过(lgN + 1)次比较,删除最大元素的操作需要不超过2lgN次比较. 证明.由命题P可知,两种操作都需要在根节点和堆底之间移动 ...

  2. pyqt QTreeWidget例子学习

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.QtGui import  * from Py ...

  3. 在Unity3d编辑器中加入菜单以及菜单项

    在引用UZGUI插件时,u3d编辑器的菜单条发生了变化,新增了菜单和菜单项,于是乎自己也像尝试一下,看了EZGUI的About_EZ_GUI脚本文件后,结果大出我所料,原来SO EASY! using ...

  4. Java之Static静态修饰符详解

    Java之Static静态修饰符详解 Java之Static静态修饰符详解 一.特点 1.随着类的加载而加载,随着类的消失而消失,生命周期最长 2.优先于对象存在 3.被所有类的对象共享 4.可以直接 ...

  5. IOS总结_无需自己定义UITabbar也可改变UITabbarController的背景和点击和的颜色

    在application: application didFinishLaunchingWithOptions: launchOptions 增加以下代码就能够实现对tabbar的颜色的改动 //设定 ...

  6. linux 常用 命令 笔记二

    wget 下载,得到网络上的内容 grep 文件搜索工具 EveryThing is a file in the linux system 安装 cowsay sudo apt-get install ...

  7. 如何排版 微信公众号「代码块」之 MarkEditor

    前段时间写过一篇文章 如何排版微信公众号「代码块」,讲的是如何使用浏览器插件 Markdown Here 来排版代码块.虽然用 Markdown Here 排版出来的样式还不错,但存在一个问题,就是代 ...

  8. 一个Demo就懂的Angular之directive

    <body> <div ng-controller="myCtrl"> <hello-word></hello-word> < ...

  9. VML :Vector Markup Language

    在以前老是浏览器IE<9在不支持SVG情况下,IE一般通过VML来绘制图形,图片,文字等 步骤: 必须在头部添加 <HTML xmlns:v="urn:schemas-micro ...

  10. java Object 类

    Object o=new Object(); 其中有两个受保护的方法:             1.protected void finalize()             2.protected ...