【CSP2019】树上的数
题面
题解
我们设每个点上的编号分别为(a_1,a_2...a_n)。
10pts
。。。
菊花
假设现在菊花中心编号是(rt),设你依次拆边((p_1,rt),(p_2,rt)...(p_{n-1},rt)),那么最后你会发现(a_{rt})到了点(p_1),(a_{p_1})到了点(p_2...a_{p_{n-1}})到了(rt)。
我们把点按照((rt,p_1,p_2...p_{n-1}))排出来,那么操作就相当于每个点上的数字向后挪了一位。
于是可以想到贪心构造轮换的过程,就是说按照(1,2,3...n),每个数字选择自己在环上的下一个点是什么,因为你最后肯定是个大环,所以中间过程中不能出现小环,所以用并查集维护一下即可。
链
链有几个很显然的性质,假如数字(a_u)要跑到(v)去(假设方向从左往右),那么对于(u),你必须(u)两边的删边顺序先右后左,对于(u,v)中间的点,一定要是保证删边是连续的所以中间的所有点删边顺序先左后右。
我们将每个点打上一个标记(tag_iin{0,1,2})分别表示没有删边,先右后左,先左后右,那么你肯定还是贪心地去换,如果中间有标记冲突了就证明换不了。
100pts
和链一样,还是考虑如果要将(a_u)放到(v)去,我们这张图需要满足条件是啥:
- (u)在(v)方向上的边是(u)出边中第一个被删除的
- 路径(u,v)上删边连续
- (v)在(u)方向上的边是(v)入边中最后一个被删除的
考虑对于所有的点(u),将它的所有出边抽象成一张图,用一条有向边表示删边的先后关系(即若(i ightarrow j),则(j)必须在选(i)后马上选),同时对于(forall u)它们的图都是不相关的。
显然所有的点出度至多为(1),那么这样的图就是很多条链(有些链中间互不影响,所以会有多条),记录一下每一条链的开头和结尾,那么判定一张图合法的情况有一下三种:
- 图不是由若干条链组成的
- 第一个点有入边,最后一个点有出边
- 第一个点和最后一个点在同一条链中,但是有其他的点不在这条链中
和链一样贪心,用并查集判断一下是否合法即可,实现细节详见代码(这题细节是真的多)。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
return w * data;
}
const int MAX_N = 2e3 + 5;
struct Graph { int to, next; } e[MAX_N << 1];
int fir[MAX_N], e_cnt;
void clearGraph() { memset(fir, -1, sizeof(fir)); e_cnt = 0; }
void Add_Edge(int v, int u) { e[e_cnt] = (Graph){v, fir[u]}, fir[u] = e_cnt++; }
int N, w[MAX_N];
struct Node {
int deg, beg, end, pa[MAX_N];
bool st[MAX_N], ed[MAX_N];
void clear() {
deg = 0, beg = end = -1;
for (int i = 0; i <= N; i++) st[i] = ed[i] = 1, pa[i] = i;
}
int getf(int x) { while (x != pa[x]) x = pa[x] = pa[pa[x]]; return x; }
} t[MAX_N];
int Find(int x, int id) {
int res = N + 1;
if (~id && (t[x].end == -1 || t[x].end == id)) {
if (t[x].ed[id] && (t[x].beg == -1 || t[x].deg <= 1 || t[x].getf(id) != t[x].getf(t[x].beg))) res = x;
}
for (int i = fir[x]; ~i; i = e[i].next) {
if (id == (i >> 1)) continue;
int ed = i >> 1;
if (~id) {
if (id == t[x].end || ed == t[x].beg || t[x].getf(id) == t[x].getf(ed)) continue;
if (!t[x].ed[id] || !t[x].st[ed]) continue;
if (~t[x].beg && ~t[x].end && t[x].deg > 2 &&
t[x].getf(id) == t[x].getf(t[x].beg) && t[x].getf(ed) == t[x].getf(t[x].end)) continue;
res = min(res, Find(e[i].to, ed));
} else {
if (t[x].beg == -1 || t[x].beg == ed) {
if (!t[x].st[ed]) continue;
if (~t[x].end && t[x].deg > 1 && t[x].getf(ed) == t[x].getf(t[x].end)) continue;
res = min(res, Find(e[i].to, ed));
}
else continue;
}
}
return res;
}
bool Link(int x, int id, int p) {
if (x == p) return t[x].end = id, 1;
for (int i = fir[x]; ~i; i = e[i].next) {
if (id == (i >> 1)) continue;
int ed = i >> 1;
if (Link(e[i].to, ed, p)) {
if (~id) {
t[x].ed[id] = t[x].st[ed] = 0, --t[x].deg;
t[x].pa[t[x].getf(id)] = t[x].getf(ed);
}
else t[x].beg = ed;
return 1;
}
}
return 0;
}
int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
int T = gi();
while (T--) {
clearGraph();
N = gi(); for (int i = 1; i <= N; i++) w[i] = gi(), t[i].clear();
if (N == 1) { puts("1"); continue; }
for (int i = 1; i < N; i++) {
int u = gi(), v = gi();
Add_Edge(u, v), Add_Edge(v, u);
++t[u].deg, ++t[v].deg;
}
for (int i = 1; i <= N; i++) {
int p = Find(w[i], -1);
Link(w[i], -1, p);
printf("%d ", p);
}
putchar('
');
}
return 0;
}