QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#209466#7103. Red Black TreePetroTarnavskyi#AC ✓725ms24956kbC++172.2kb2023-10-10 15:16:162023-10-10 15:16:16

Judging History

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

  • [2023-10-10 15:16:16]
  • 评测
  • 测评结果:AC
  • 用时:725ms
  • 内存:24956kb
  • [2023-10-10 15:16:16]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first	
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

const int INF = 1'000'000'447;
const LL LINF = (LL)INF * INF;
const int N = 100'447;
const int LOG = 17;
vector<PII> g[N];
LL d[N];
LL cost[N];
bool red[N];
int tin[N];
int tout[N];
int up[LOG][N];
int T = 0;

void dfs(int v, LL c, LL h, int p)
{
	if (red[v])
		c = 0;
	cost[v] = c;
	d[v] = h;
	tin[v] = T++;
	up[0][v] = p;
	FOR (i, 1, LOG)
	{
		up[i][v] = up[i - 1][up[i - 1][v]];
	}
	for (auto [to, w] : g[v])
	{
		if (to != p)
		{
			dfs(to, c + w, h + w, v);
		}
	}
	tout[v] = T++;
}

bool is_par(int p, int v)
{
	return tin[p] <= tin[v] && tout[v] <= tout[p];
}

int lca(int v, int u)
{
	if (is_par(v, u))
		return v;
	RFOR (i, LOG, 0)
	{
		if (!is_par(up[i][v], u))
			v = up[i][v];
	}
	return up[0][v];
}

void solve()
{
	int n, m, q;
	cin >> n >> m >> q;
	FOR (i, 0, m)
	{
		int x;
		cin >> x;
		red[x - 1] = true;
	}
	FOR (i, 0, n - 1)
	{
		int a, b, w;
		cin >> a >> b >> w;
		a--, b--;
		g[a].PB({b, w});
		g[b].PB({a, w});
	}
	dfs(0, 0, 0, 0);
	FOR (i, 0, q)
	{
		int k;
		cin >> k;
		VI v(k);
		FOR (j, 0, k)
		{
			cin >> v[j];
			v[j]--;
		}
		sort(ALL(v), [&](int a, int b)
		{
			return cost[a] > cost[b];
		});
		LL ans = cost[v[0]];
		int lc = v[0];
		LL D = d[v[0]];
		FOR (j, 0, k)
		{
			lc = lca(lc, v[j]);
			D = max(D, d[v[j]]);
			LL x = D - d[lc];
			if (j + 1 != k)
				x = max(x, cost[v[j + 1]]);
			ans = min(ans, x);
		}
		cout << ans << '\n';
	}
	FOR (i, 0, n)
	{
		g[i].clear();
		cost[i] = 0;
		d[i] = 0;
		FOR (j, 0, LOG)
			up[j][i] = 0;
		tin[i] = 0;
		tout[i] = 0;
		red[i] = false;
	}
	T = 0;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(15);
	
	int t;
	cin >> t;
	while (t--)
	{
		solve();
	}
	
	return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 14904kb

input:

2
12 2 4
1 9
1 2 1
2 3 4
3 4 3
3 5 2
2 6 2
6 7 1
6 8 2
2 9 5
9 10 2
9 11 3
1 12 10
3 3 7 8
4 4 5 7 8
4 7 8 10 11
3 4 5 12
3 2 3
1 2
1 2 1
1 3 1
1 1
2 1 2
3 1 2 3

output:

4
5
3
8
0
0
0

result:

ok 7 lines

Test #2:

score: 0
Accepted
time: 725ms
memory: 24956kb

input:

522
26 1 3
1
1 4 276455
18 6 49344056
18 25 58172365
19 9 12014251
2 1 15079181
17 1 50011746
8 9 2413085
23 24 23767115
22 2 26151339
26 21 50183935
17 14 16892041
9 26 53389093
1 20 62299200
24 18 56114328
11 2 50160143
6 26 14430542
16 7 32574577
3 16 59227555
3 15 8795685
4 12 5801074
5 20 57457...

output:

148616264
148616264
0
319801028
319801028
255904892
317070839
1265145897
1265145897
1072765445
667742619
455103436
285643094
285643094
285643094
317919339
0
785245841
691421476
605409472
479058444
371688030
303203698
493383271
919185207
910180170
919185207
121535083
181713164
181713164
181713164
181...

result:

ok 577632 lines

Extra Test:

score: 0
Extra Test Passed