ZOJ - 3761 Easy billiards 【并查集+DFS】
题目链接
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3761
题意
在一个桌面上,给出一些球 如果在A球的某个方向的前方有B球 那个A球就可以朝那个方向滚 然后 滚到B球的位置后 ,B球往前滚,A球停在B球的位置上
求这样操作后,最后最少剩下多少球,然后要输出操作的方式
思路
其实可以发现,一个球经过一次操作之后,相当于这个球消失了 因为它替代了它前方那个球的位置 这个操作又迭代下去
最后发现 其实只有这个球本身的位置是没有球了
所以 如果一个球的四周有球 这个球就可以消失
那么 我们可以把一个球的四个方向上的球都并起来,然后并起来的球的四个方向的球又可以并起来,最后剩下的球的个数 其实就是连通块的个数
这个 并 我们只需要指向父节点就可以了 而不需要指向祖先节点,因为操作方式中 就是将这个球打向父节点,而不是祖先节点,因为和祖先节点并不一定是在同一个方向上的
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
#define bug puts("***bug***");
#define fi first
#define se second
//#define bug
//#define gets gets_s
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <string, int> psi;
typedef pair <string, string> pss;
typedef pair <double, int> pdi;
const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int maxn = 2e3 + 10;
const int MOD = 1e9 + 7;
int pre[maxn];
int tot[maxn];
int find(int x)
{
while (x != pre[x])
x = pre[x];
return x;
}
void join(int x, int y)
{
int fx = find(x), fy = find(y);
if (fx != fy)
pre[fx] = fy;
}
int n;
int G[maxn][maxn];
int x[maxn], y[maxn];
bool judge(int a, int b)
{
if (x[a] == x[b] || y[a] == y[b])
return true;
return false;
}
void dfs(int root, int vis)
{
for (int i = 1; i <= n; i++)
{
if (G[root][i] == 1 && pre[i] == i && root != i && i != vis)
{
pre[i] = root;
dfs(i, vis);
}
}
}
void clear()
{
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
G[i][j] = 0;
for (int i = 1; i <= n; i++)
{
pre[i] = i;
tot[i] = 0;
}
}
void print(int root, int son)
{
if (x[root] == x[son])
puts(y[root] > y[son] ? "UP" : "DOWN");
else
puts(x[root] > x[son] ? "RIGHT" : "LEFT");
}
int main()
{
while (~scanf("%d", &n))
{
clear();
for (int i = 1; i <= n; i++)
scanf("%d%d", &x[i], &y[i]);
for (int i = 1; i <= n; i++)
{
G[i][i] = 1;
for (int j = i + 1; j <= n; j++)
{
if (judge(i, j))
G[i][j] = G[j][i] = 1;
}
}
//for (int i = 1; i <= n; i++)
// for (int j = 1; j <= n; j++)
// printf("%d%c", G[i][j], j == n ? '\n' : ' ');
for (int i = 1; i <= n; i++)
{
for (int j = i + 1; j <= n; j++)
{
if (G[i][j] && pre[j] == j)
{
pre[j] = i;
dfs(j, i);
}
}
}
map <int, int> m;
for (int i = 1; i <= n; i++)
{
int u = find(i);
m[u]++;
}
for (int i = 1; i <= n; i++)
tot[pre[i]]++;
printf("%d\n", m.size());
int pos = 1;
int len = n - m.size();
while (pos <= len)
{
for (int i = 1; i <= n && pos <= len; i++)
{
int root = pre[i];
if (i != root && tot[i] == 0)
{
printf("(%d, %d) ", x[i], y[i]);
print(root, i);
tot[root]--;
tot[i]--;
pos++;
}
}
}
}
}
ZOJ - 3761 Easy billiards 【并查集+DFS】的更多相关文章
- ZOJ 3761 Easy billiards 月赛E DFS
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3761 题目大意:给定n个位置的小球,小球的运动方向只能是水平或者 ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- 1021.Deepest Root (并查集+DFS树的深度)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- POJ1291-并查集/dfs
并查集 题意:找出给定的这些话中是否有冲突.若没有则最多有多少句是对的. /* 思路:如果第x句说y是对的,则x,y必定是一起的,x+n,y+n是一起的:反之x,y+n//y,x+n是一起的. 利用并 ...
- F2 - Spanning Tree with One Fixed Degree - 并查集+DFS
这道题还是非常有意思的,题意很简单,就是给定一个图,和图上的双向边,要求1号节点的度(连接边的条数)等于K,求这棵树的生成树. 我们首先要解决,如何让1号节点的度时为k的呢???而且求的是生成树,意思 ...
- UVA208-Firetruck(并查集+dfs)
Problem UVA208-Firetruck Accept:1733 Submit:14538 Time Limit: 3000 mSec Problem Description The Ce ...
- 2018 计蒜之道复赛 贝壳找房魔法师顾问(并查集+dfs判环)
贝壳找房在遥远的传奇境外,找到了一个强大的魔法师顾问.他有 22 串数量相同的法力水晶,每个法力水晶可能有不同的颜色.为了方便起见,可以将每串法力水晶视为一个长度不大于 10^5105,字符集不大于 ...
- Codeforces 455C Civilization(并查集+dfs)
题目链接:Codeforces 455C Civilization 题目大意:给定N.M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的.然后是Q次操作.操作分为两种.一种是查询城市x所 ...
- hdu6370 并查集+dfs
Werewolf Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
随机推荐
- 如何检测一个aspx页面的速度慢的原因
最近读到一篇文章,是关于如何提高一个aspx页面的速度.这是一个常见的面试问题.该问题原文出自这个网站. 出现这个问题的原因会多种多样,我们需要一步一步的排查来定位问题真正出现在哪里. 1. 找出那一 ...
- ExtJs的Ext.grid.GridPanel不能选择复制表格中的内容解决方案
今天遇到grid复制的问题,在网上找到了一个解决办法,只需改下CSS和JS,给大家分享一下: 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/dy_paradise/a ...
- 我是怎样理解web页面的
事实上web页面包括三部分东东 1.页面展示的元素(HTML) 2.页面元素展示的样式(CSS) 3.控制页面元素的交互(JavaScript) 不管页面多么复杂,从这三方面去看,都会得到清晰的认识的 ...
- Unity多个场景叠加或大场景处理方法小结
本文章由cartzhang编写.转载请注明出处. 全部权利保留. 文章链接: http://blog.csdn.net/cartzhang/article/details/47614153 作者:ca ...
- [Hibernate开发之路](4)ID生成策略
一 对象关系数据库映射之Id 被映射的类必须定义相应数据库表主键字段.大多数类有一个JavaBeans风格的属性, 为每个实例包括唯一的标识. <id> 元素定义了该属性到数据库表主键字段 ...
- [译]GLUT教程 - 初始化
Lighthouse3d.com >> GLUT Tutorial >> Basics >> Initialization 这一节开始从main函数入手.第一步是线 ...
- SpringBoot Idea 启动报错 Process finished with exit code 1
问题描述:没有其他任何错误日志,只有Process finished with exit code 1 问题原因:Maven POM.xml问题造成 由于是properties是我直接从其他项目中拷贝 ...
- hadoop System times on machines may be out of sync. Check system time and time zones.
之前环境一直好好的,由于玩坏了一个mini3只能复制一个了,但是复制之后就出现这个问题了 解决办法是 设置xshell向每一个窗口发消息http://mofansheng.blog.51cto.com ...
- eclipse +cygwin+C++
用Android eclipse做C++开发,一开始提示no binary的错误,貌似是因为没有编译二进制出来,我本机装了cygwin, 在命令台输入gcc,无显示,说明我没有把cygwin/bin的 ...
- Linux tar包安装Nginx
1.首先安装依赖包(依赖包有点多.我们採用yum的方式来安装) yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel ...