QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#827870#9772. Permutation Routingle0nTL 1ms3896kbC++202.6kb2024-12-23 10:55:102024-12-23 10:55:11

Judging History

你现在查看的是最新测评结果

  • [2025-01-10 11:38:24]
  • hack成功,自动添加数据
  • (/hack/1438)
  • [2024-12-23 10:55:11]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3896kb
  • [2024-12-23 10:55:10]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 1005;
vector< pair<int, int> > e[N];
bool vis[N];
int fa[N], fe[N], sz[N], p[N];
bool tag[N], owo[N], ban[N];
vector<int> qwq, P, L, R;
void dfs(int x, int f)
{
	int y;
	qwq.emplace_back(x);
	sz[x] = 1;
	fa[x] = f;
	for(auto o: e[x])
	{
		y = o.first;
		if(!vis[o.second] && y != f)
		{
			fe[y] = o.second;
			dfs(y, x);
			sz[x] += sz[y];
		}
	}
}
vector<int> opt[N * 3], tmp;
int work(int x, int d)
{
	int u, v, i, n, m, y;
	m = 0;
	qwq.clear();
	dfs(x, 0);
	P = qwq;
	if(sz[x] == 1)
		return d;
	n = sz[x];
	for(auto o: qwq)
	{
		tag[o] = 0;
		if(min(sz[o], n - sz[o]) > min(sz[m], n - sz[m]))
			m = o;
	}
	u = fa[m];
	v = m;
	m = fe[m];
	vis[m] = 1;
	qwq.clear();
	dfs(u, v);
	L = qwq;
	qwq.clear();
	dfs(v, u);
	R = qwq;
	for(auto o: R)
		tag[o] = 1;
	// printf("#%d %d(%d, %d):\n", x, m, u, v);
	for(auto o: P)
	{
		owo[o] = tag[p[o]];
		// printf("%d(%d)\n", o, owo[o]);
	}
	while(1)
	{
		tmp.clear();
		for(auto o: P)
			ban[o] = 0;
		for(auto o: L)
			if(!owo[o] && !ban[o])
				for(auto w: e[o])
				{
					y = w.first;
					if(!vis[w.second] && y != fa[o] && owo[y])
					{
						swap(owo[o], owo[y]);
						swap(p[o], p[y]);
						ban[o] = ban[y] = 1;
						// printf("HEH%d %d %d\n", o, y, fe[y]);
						tmp.emplace_back(fe[y]);
						break;
					}
				}
		for(auto o: R)
			if(owo[o] && !ban[o])
				for(auto w: e[o])
				{
					y = w.first;
					if(!vis[w.second] && y != fa[o] && !owo[y])
					{
						swap(owo[o], owo[y]);
						swap(p[o], p[y]);
						ban[o] = ban[y] = 1;
						// printf("HEH%d %d %d\n", o, y, fe[y]);
						tmp.emplace_back(fe[y]);
						break;
					}
				}
		if(!ban[u] && !ban[v] && owo[u] && !owo[v])
		{
			swap(owo[u], owo[v]);
			// printf("HEH%d %d %d\n", u, v, m);
			swap(p[u], p[v]);
			tmp.emplace_back(m);
		}
		if(tmp.empty())
			break;
		++d;
		for(auto o: tmp)
			opt[d].emplace_back(o);
	}
	// printf("WOW%d\n", d);
	return max(work(u, d), work(v, d));
}

int main()
{
	int T, n, i, u, v, d;
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d", &n);
		for(i = 1; i <= n; i++)
			scanf("%d", p + i);
		for(i = 1; i <= n; i++)
			e[i].clear();
		for(i = 1; i < n; i++)
		{
			scanf("%d%d", &u, &v);
			vis[i] = 0;
			e[u].emplace_back(make_pair(v, i));
			e[v].emplace_back(make_pair(u, i));
		}
		d = work(1, 0);
		printf("%d\n", d);
		for(i = 1; i <= d; i++)
		{
			printf("%d", opt[i].size());
			for(auto o: opt[i])
				printf(" %d", o);
			printf("\n");
		}
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3896kb

input:

1
5
1 4 2 5 3
1 2
2 3
2 4
1 5

output:

3
2 4 3
1 1
2 2 4

result:

ok ok, up to 3 steps were used

Test #2:

score: -100
Time Limit Exceeded

input:

10000
5
2 3 1 5 4
1 5
3 2
1 2
1 4
5
1 2 3 4 5
2 3
3 4
2 1
4 5
5
4 2 5 1 3
3 5
2 3
4 1
3 1
5
1 3 4 2 5
5 3
2 1
1 3
2 4
5
1 2 3 4 5
2 1
3 5
2 3
5 4
5
1 2 3 4 5
4 5
3 4
4 2
4 1
5
5 2 1 4 3
2 1
5 1
3 1
1 4
5
4 1 2 5 3
3 1
5 1
1 2
1 4
5
5 3 4 2 1
3 1
3 5
4 3
3 2
5
3 4 1 2 5
3 2
3 5
1 5
3 4
5
3 4 1 2 5
2 ...

output:

5
1 2
1 3
1 4
1 1
1 4
0
1
3 2 1 3
3
4 2 1 3 3
2 3 2
3 4 4 3
0
0
2
5 2 1 3 3 2
3 3 2 3
6
6 2 1 3 3 2 2
4 3 2 3 1
4 4 4 3 4
2 1 2
2 4 4
1 3
5
7 2 1 3 3 2 2 2
5 3 2 3 1 1
5 4 4 3 4 2
3 1 2 3
3 4 4 4
5
8 2 1 3 3 2 2 2 3
6 3 2 3 1 1 2
7 4 4 3 4 2 4 3
4 1 2 3 1
4 4 4 4 4
3
9 2 1 3 3 2 2 2 3 2
8 3 2 3 1 1 ...

result: