題目鏈接
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++;
}
}
}
}
}