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 ...
随机推荐
- 设计模式之十八:桥接模式(Bridge)
桥接模式: 将抽象部分和它的实现部分相分离开来,以使它们能够单独地变化. UML图: 主要包含: Abstraction:定义了抽象部分的接口.操作一个实现部分对象的引用. RefinedAbstra ...
- showModalDialog后如何刷新父页面
最近一个项目使用到的.在网上查了好久,有的可行,有的就不行.总结一下吧.方案一:父页面:window.showModalDialog('User.jsf?USERCODE='001'&Rnd= ...
- kernel feature collection
Fault injection http://lwn.net/Articles/209257/ The framework can cause memory allocation failures a ...
- IntelliJ IDEA(2017)下载并破解
idea激活,JetBrain旗下软件激活 我在修改这个博主的文章再添加了code码 http://blog.csdn.net/qq_24504453/article/details/77407329 ...
- .Vue 文件 ES6 语法 webstorm 中的一个识别Bug
webstorm 2017 版本中即使安装了vue template file 设置了 js 语言为 es6 语法仍旧会出现识别不了划线的情况,苦寻很久,最后解决方式如下 <script typ ...
- Unicode utf8等编码类型的原理
1.ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(byte). ...
- Eval,Bind,<% %>,<%# %>和<%= %> 笔记
1.<% %>用来绑定后台代码 如: < % for(int i=0;i<100;i++) { Reaponse.Write(i.ToString()); } %> 2. ...
- 智能路由器开发指南_book
最近购得一个openwrt书籍<智能路由器开发指南>,作者张永智. Building a smart router with openwrt 作者网址:http://openwrt.bjb ...
- 信号量semaphore解析
1 基础概念 信号量在创建时须要设置一个初始值,表示同一时候能够有几个任务能够訪问该信号量保护的共享资源.初始值为1就变成相互排斥锁(Mutex),即同一时候仅仅能有一个任务能够訪问信号量保护的共享资 ...
- 安装Hadoop 1.1.2 (二 安装配置SSH)
1 查找SSH yum search ssh 2 如果没有安装, yum install openssh.x86_64 4 直接运行 ssh-keygen -t dsa -P '' -f /roo ...