QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#168504#7103. Red Black Treedo_while_trueAC ✓788ms28420kbC++203.1kb2023-09-08 16:22:362023-09-08 16:22:37

Judging History

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

  • [2023-09-08 16:22:37]
  • 评测
  • 测评结果:AC
  • 用时:788ms
  • 内存:28420kb
  • [2023-09-08 16:22:36]
  • 提交

answer

#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<ctime>
#include<random>
#include<assert.h>
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define dbg(x) cerr<<"In Line "<< __LINE__<<" the "<<#x<<" = "<<x<<'\n'
#define dpi(x,y) cerr<<"In Line "<<__LINE__<<" the "<<#x<<" = "<<x<<" ; "<<"the "<<#y<<" = "<<y<<'\n'
#define DE(fmt,...) fprintf(stderr, "Line %d : " fmt "\n",__LINE__,##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<ll,int>pli;
typedef pair<ll,ll>pll;
typedef pair<int,ll>pil;
typedef vector<int>vi;
typedef vector<ll>vll;
typedef vector<pii>vpii;
typedef vector<pll>vpll;
template<typename T>T cmax(T &x, T y){return x=x>y?x:y;}
template<typename T>T cmin(T &x, T y){return x=x<y?x:y;}
template<typename T>
T &read(T &r){
	r=0;bool w=0;char ch=getchar();
	while(ch<'0'||ch>'9')w=ch=='-'?1:0,ch=getchar();
	while(ch>='0'&&ch<='9')r=r*10+(ch^48),ch=getchar();
	return r=w?-r:r;
}
template<typename T1,typename... T2>
void read(T1 &x,T2& ...y){read(x);read(y...);}
const int mod=998244353;
inline void cadd(int &x,int y){x=(x+y>=mod)?(x+y-mod):(x+y);}
inline void cdel(int &x,int y){x=(x-y<0)?(x-y+mod):(x-y);}
inline int add(int x,int y){return (x+y>=mod)?(x+y-mod):(x+y);}
inline int del(int x,int y){return (x-y<0)?(x-y+mod):(x-y);}
int qpow(int x,int y){
	int s=1;
	while(y){
		if(y&1)s=1ll*s*x%mod;
		x=1ll*x*x%mod;
		y>>=1;
	}
	return s;
}
const int N=200010;
int n,m,q;
int fa[N][20],vis[N],dp[N];
ll dep[N],c[N];
vpii eg[N];
void dfs(int x,int pa,ll h){
	fa[x][0]=pa;dp[x]=dp[pa]+1;
	if(vis[x])h=dep[x];
	c[x]=dep[x]-h;
	for(int i=1;i<=19;i++)fa[x][i]=fa[fa[x][i-1]][i-1];
	for(auto i:eg[x])if(i.fi!=pa){
		dep[i.fi]=dep[x]+i.se;
		dfs(i.fi,x,h);
	}
}
int LCA(int x,int y){
	if(dp[x]<dp[y])swap(x,y);
	for(int i=19;~i;--i)
		if(dp[fa[x][i]]>=dp[y])
			x=fa[x][i];
	if(x==y)return x;
	for(int i=19;~i;--i)
		if(fa[x][i]!=fa[y][i])
			x=fa[x][i],y=fa[y][i];
	return fa[x][0];
}
void solve(){
	read(n,m,q);
	for(int i=1;i<=n;i++)vpii().swap(eg[i]),vis[i]=0;
	for(int i=1;i<=m;i++){
		int x;read(x);
		vis[x]=1;
	}
	for(int i=1,u,v,w;i<n;i++){
		read(u,v,w);
		eg[u].pb(mp(v,w));eg[v].pb(mp(u,w));
	}
	dfs(1,0,0);
	while(q--){
		int k;read(k);
		vi vec;
		for(int i=1;i<=k;i++){
			int x;read(x);
			vec.pb(x);
		}
		sort(vec.begin(),vec.end(),[&](const int &x,const int &y){return c[x]>c[y];});
		int lca=vec[0],mx=vec[0];ll ans=c[vec[0]];
		for(int i=0;i<k;i++){
			int x=vec[i];
			lca=LCA(lca,x);
			if(dep[x]>dep[mx])mx=x;
			if(i<k-1)cmin(ans,max(dep[mx]-dep[lca],c[vec[i+1]]));
			else cmin(ans,dep[mx]-dep[lca]);
		}
		cout<<ans<<'\n';
	}
}
signed main(){
	#ifdef do_while_true
		assert(freopen("data.in","r",stdin));
//		assert(freopen("data.out","w",stdout));
	#endif
	int T;read(T);
	while(T--)solve();
    #ifdef do_while_true
//		cerr<<'\n'<<"Time:"<<1.0*clock()/CLOCKS_PER_SEC*1000<<" ms"<<'\n';
	#endif
	return 0;
}

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

詳細信息

Test #1:

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

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: 788ms
memory: 28420kb

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