QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#357175#7103. Red Black Treearnold518AC ✓957ms27484kbC++172.0kb2024-03-18 19:02:382024-03-18 19:02:40

Judging History

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

  • [2024-03-18 19:02:40]
  • 评测
  • 测评结果:AC
  • 用时:957ms
  • 内存:27484kb
  • [2024-03-18 19:02:38]
  • 提交

answer

#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e5;

int TC, N, M, Q;
int R[MAXN+10];
int P[MAXN+10];
ll D[MAXN+10];
vector<pii> adj[MAXN+10];
int par[MAXN+10][30], dep[MAXN+10];

void dfs(int now, int bef, ll d, int d2, int root)
{
	par[now][0]=bef;
	dep[now]=d2;
	if(R[now]) root=now, d=0;

	P[now]=root;
	D[now]=d;

	for(auto [nxt, w] : adj[now])
	{
		if(nxt==bef) continue;
		dfs(nxt, now, d+w, d2+1, root);
	}
}

int lca(int u, int v)
{
	if(dep[u]>dep[v]) swap(u, v);
	for(int i=20; i>=0; i--) if(dep[par[v][i]]>=dep[u]) v=par[v][i];
	if(u==v) return u;
	for(int i=20; i>=0; i--) if(par[u][i]!=par[v][i]) u=par[u][i], v=par[v][i];
	return par[u][0];
}

int main() {
	cin.tie(0); ios_base::sync_with_stdio(0);

	scanf("%d", &TC);
	while(TC--)
	{
		scanf("%d%d%d", &N, &M, &Q);
		for(int i=1; i<=M; i++)
		{
			int t;
			scanf("%d", &t);
			R[t]=1;
		}

		for(int i=1; i<N; i++)
		{
			int u, v, w;
			scanf("%d%d%d", &u, &v, &w);
			adj[u].push_back({v, w});
			adj[v].push_back({u, w});
		}

		dfs(1, 1, 0, 1, 1);
		for(int i=1; i<=20; i++) for(int j=1; j<=N; j++) par[j][i]=par[par[j][i-1]][i-1];

		while(Q--)
		{
			int sz;
			scanf("%d", &sz);
			vector<int> V(sz);
			for(auto &it : V) scanf("%d", &it);

			int u=V[0];
			for(int i=0; i<V.size(); i++) if(D[u]<D[V[i]]) u=V[i];

			ll ansv=0, ans=0;
			vector<int> VV;
			for(int i=0; i<V.size(); i++)
			{
				ans=max(ans, D[V[i]]);
				if(P[u]==P[V[i]]) VV.push_back(V[i]);
				else ansv=max(ansv, D[V[i]]);
			}

			sort(VV.begin(), VV.end(), [&](const int &p, const int &q) { return D[p]>D[q]; });
			int now=VV[0];
			for(int i=0; i<VV.size(); i++)
			{
				now=lca(now, VV[i]);
				ll t=0;
				if(i+1<VV.size()) t=D[VV[i+1]];

				ans=min(ans, max({D[VV[0]]-D[now], t, ansv}));
			}
			printf("%lld\n", ans);
		}	
		for(int i=1; i<=N; i++)
		{
			adj[i].clear();
			R[i]=0;
		}
	}
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 957ms
memory: 27484kb

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