QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#162980#7103. Red Black Treeoscaryang#AC ✓317ms41472kbC++172.8kb2023-09-03 18:25:232023-09-03 18:25:24

Judging History

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

  • [2023-09-03 18:25:24]
  • 评测
  • 测评结果:AC
  • 用时:317ms
  • 内存:41472kb
  • [2023-09-03 18:25:23]
  • 提交

answer

#include<bits/stdc++.h>

#define ll long long
#define db double
#define vc vector
#define pb emplace_back
#define pii pair<int,int>
#define mkp make_pair
#define mem(a) memset(a,0,sizeof(a))

#define int long long

using namespace std;
const int N = 1e6+5, P = 998244353;
//const int inf = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;

inline int read() {
	int x = 0, w = 0; char ch = getchar(); while(!isdigit(ch)) w |= (ch=='-'), ch = getchar();
	while(isdigit(ch)) x = x*10+(ch^48), ch = getchar(); return w?-x:x;
}
inline void write(int x) { if(x<0) putchar('-'), x = -x; if(x>9) write(x/10); putchar(x%10+'0'); }
inline void writec(int x) { write(x); putchar(32); }
inline void writee(int x) { write(x); putchar(10); }

inline void inc(int &x,int y) { x += y-P; x += (x>>31)&P; }
inline void dec(int &x,int y) { x -= y; x += (x>>31)&P; }
inline int pls(int x,int y) { x += y-P; x += (x>>31)&P; return x; }
inline void Max(int &x,int y) { if(x<y) x = y; }
inline void Min(int &x,int y) { if(x>y) x = y; }
inline int power(int a,int b) { int res = 1; for(;b;b>>=1,a=1ll*a*a%P) if(b&1) res = 1ll*res*a%P; return res; }

int n, m, q, f[N];
int fa[N], d[N], top[N], sz[N], son[N], dis[N];
bool re[N];
int hd[N], to[N<<1], ne[N<<1], e[N<<1], tc = 1;

inline void add(int x,int y,int z) {
	to[++tc] = y; ne[tc] = hd[x]; hd[x] = tc; e[tc] = z;
	to[++tc] = x; ne[tc] = hd[y]; hd[y] = tc; e[tc] = z;
}

inline void dfs1(int x) {
	if(re[x]) f[x] = 0;
	sz[x] = 1; son[x] = 0;
	for(int i=hd[x],y;i;i=ne[i]) if((y = to[i])!=fa[x]) {
		fa[y] = x; f[y] = f[x]+e[i];
		d[y] = d[x]+1; dis[y] = dis[x]+e[i]; dfs1(y);
		sz[x] += sz[y]; son[x] = sz[y]>sz[son[x]]?y:son[x];
	}
}
inline void dfs2(int x,int t) {
	top[x] = t; if(son[x]) dfs2(son[x],t);
	for(int i=hd[x],y;i;i=ne[i]) if((y = to[i])!=fa[x] && y!=son[x]) dfs2(y,y);
}
inline int lca(int x,int y) {
	for(;top[x]!=top[y];x=fa[top[x]]) if(d[top[x]]<d[top[y]]) swap(x,y);
	return d[x]<d[y]?x:y;	
}

inline void solve() {
	n = read(); m = read(); q = read(); tc = 1;
	for(int i=1;i<=n;i++) re[i] = 0, hd[i] = 0;
	for(int i=1;i<=m;i++) re[read()] = 1;
	for(int i=1,u,v,w;i<n;i++) u = read(), v = read(), w = read(), add(u,v,w);
	d[1] = 1; dfs1(1); dfs2(1,1); 
	int k, x, y, p, val, res; vc<pii> o;
	while(q--) {
		k = read(); o.clear();
		for(int i=1,x;i<=k;i++) x = read(), o.pb(mkp(f[x],x));
		sort(o.begin(),o.end(),[&](pii A,pii B){return A.first>B.first;}); o.pb(mkp(0,0));
		x = o[0].second, res = o[1].first, val = 0;
		for(int i=1;i+1<o.size();i++) {
			y = o[i].second; p = lca(x,y);
			val = max(val+dis[x]-dis[p],dis[y]-dis[p]);
			res = min(res,max(val,o[i+1].first));
			x = p;
		}
		printf("%lld\n",res);
	}
}

signed main() {
	//freopen("a.in","r",stdin);
	int t = read(); while(t--) solve();
	return 0;
}



















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

詳細信息

Test #1:

score: 100
Accepted
time: 5ms
memory: 22020kb

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: 317ms
memory: 41472kb

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