QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#86047#5102. Dungeon Crawleryunny_worldWA 1ms4080kbC++142.9kb2023-03-09 10:41:062023-03-09 10:41:08

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-09 10:41:08]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:4080kb
  • [2023-03-09 10:41:06]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long int
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define X first
#define Y second
#define all(v) v.begin(),v.end()
using namespace std;
ll gcd(ll a, ll b)
{
	if (b == 0) return a;
	return gcd(b, a % b);
}
#define lcm(a, b) a * b / gcd(a, b)
const ll INF = 1e13;
const ll MOD = 1e6 + 7;
const double PI = 3.14159265359;
int dx[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
int dy[] = { 2, 1, -1, -2, -2, -1, 1, 2 };

int n, q;
int distSum;
int ecnt;
int e[2005][4005]; // the label of i-th visited node in the tour, e[root][i]
int h[2005][4005];
int p[2005][4005][13]; // p[root][i][j]
int d[2005][2005]; // 깊이, d[root][i]
int dist[2005][2005]; // 거리, dist[root][i]
vector<pair<int, int>> g[2005];

void dfs(int r, int now, int par)
{
	e[r][++ecnt] = now;
	for (auto nxt : g[now])
		if (nxt.X != par)
		{
			p[r][0][nxt.X] = now;
			d[r][nxt.X] = d[r][now] + 1;
			dist[r][nxt.X] = dist[r][now] + nxt.Y;
			dfs(r, nxt.X, now);
		}
	e[r][++ecnt] = par;
}

void solve()
{
	cin >> n >> q;
	for (int i = 0; i < n - 1; i++)
	{
		int u, v, w; cin >> u >> v >> w;
		g[u].push_back({ v, w });
		g[v].push_back({ u, w });
		distSum += w;
	}
	for (int r = 1; r <= n; r++)
	{
		ecnt = 0;
		dfs(r, r, 0);
		for (int i = 1; i < 2 * n; i++)
		{
			if (!h[r][e[r][i]]) h[r][e[r][i]] = i;
			p[r][i][0] = e[r][i];
			//cout << e[r][i] << ' ';
		}
		//cout << '\n';
		for (int j = 1; (1 << j) <= n; j++)
		{
			for (int i = 1; i + (1 << j) - 1 <= n; i++)
			{
				if (d[r][p[r][i][j - 1]] < d[r][p[r][i + (1 << (j - 1))][j - 1]])
					p[r][i][j] = p[r][i][j - 1];
				else
					p[r][i][j] = p[r][i + (1 << (j - 1))][j - 1];
			}
		}
	}
	while (q--)
	{
		int s, k, t; cin >> s >> k >> t;
		int kt, u = h[s][k], v = h[s][t];
		if (u > v) swap(u, v);
		int j = log2(v - u + 1);
		if (d[s][p[s][u][j]] < d[s][p[s][v - (1 << j) + 1][j]])
			kt = p[s][u][j];
		else
			kt = p[s][v - (1 << j) + 1][j];
		//cout << kt << ' ';
		if (kt == t) cout << "impossible\n";
		else
		{
			int ans = INF;
			for (int i = 1; i <= n; i++)
			{
				int ki, u = h[s][k], v = h[s][i];
				if (u > v) swap(u, v);
				int j = log2(v - u + 1);
				if (d[s][p[s][u][j]] < d[s][p[s][v - (1 << j) + 1][j]])
					ki = p[s][u][j];
				else
					ki = p[s][v - (1 << j) + 1][j];
				if (d[s][kt] > d[s][ki]) swap(kt, ki);
				int res = 2 * distSum - dist[s][i] + 2 * (dist[s][ki] - dist[s][kt]);
				ans = min(ans, res);
			}
			cout << ans << '\n';
		}
	}
}
/*
https://cp-algorithms.com/graph/lca_farachcoltonbender.html#algorithm
O(nlogn): preprocess
O(1): query
https://www.geeksforgeeks.org/range-minimum-query-for-static-array/
*/

int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
#endif
	int tc = 1; //cin >> tc;
	while (tc--) solve();
	return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 4080kb

input:

22 5
1 2 7
2 3 5
3 4 8
3 6 6
3 7 3
2 5 6
5 8 2
8 9 11
2 10 16
1 11 12
11 12 4
11 13 9
1 14 25
14 15 4
15 16 5
15 17 6
15 18 1
15 19 8
14 20 7
20 21 9
20 22 17
1 19 9
1 9 19
1 8 9
1 9 8
2 22 11

output:

293
293
293
impossible
294

result:

wrong answer 1st lines differ - expected: '316', found: '293'