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 ...
随机推荐
- Makefile.am, Makefile.in 与 Makefile的关系(转)
文章出处:http://blog.mcuol.com/User/wangguangdong/Article/17384_1.htm Makefile.am, Makefile.in, Makefile ...
- Struts2学习小结
1 基础 使用:导入 jar 包,配置 web.xml,并引入 struts.xml 文件 DMI:动态方法调用,调用时使用!分隔 action 名与方法名,如 index ! add.action, ...
- Cocos2d-x教程(35)-三维拾取Ray-AABB碰撞检測算法
欢迎增加Cocos2d-x 交流群:193411763 转载时请注明原文出处 :http://blog.csdn.net/u012945598/article/details/39927911 --- ...
- String、StringBuffer、StringBuilder区别并验证
© 版权声明:本文为博主原创文章,转载请注明出处 String.StringBuffer.StringBuilder的区别 1.String是一个常量,其对象一旦创建完毕就无法改变,当使用“+”拼接字 ...
- VM虚拟机内ubuntu无法连接到网络
VM虚拟机内ubuntu无法连接到网络 解决:编辑网络,将网路都删除掉.又一次加入网络桥接和NAT链接. .又一次连接就可以,查看一下ip地址. 方法2: 虚拟机中新装ubuntu 编辑虚拟网络,先恢 ...
- Mybatis_遇到的问题汇总
1.The setting logImpl is not known 我在参考某个网站学习mybatis时,出现这个错误,后来找到的原因是因为mybatis的版本(3.1.1)太低,换成3.3.1就没 ...
- JLink defective
下载了最新的JLink V622g,打开JLink命令行后,提示以下信息 The connected J-Link is defective,Proper operation cannot be gu ...
- Java 调用R 方法
JAVA 调用 R 语言 1 简介 R是统计计算的强大工具,而JAVA是做应用系统的主流语言,两者天然具有整合的需要.关于整合,一方面,R中可以创建JAVA对象调用JAVA方法,另一方面, ...
- POJ 1860
须要推断是否有正权环存在,Bellman-Ford算法就能够辣~ AC代码: #include <iostream> #include <cstdio> #include &l ...
- Configure the modules to be find by modprobe
sudo ln -s /path/to/module.ko /lib/modules/`uname -r` sudo depmod -a #depmod will output a dependenc ...