QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#160880#7103. Red Black Treeucup-team1447#AC ✓635ms46320kbC++144.1kb2023-09-02 21:41:502023-09-02 21:41:52

Judging History

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

  • [2023-09-02 21:41:52]
  • 评测
  • 测评结果:AC
  • 用时:635ms
  • 内存:46320kb
  • [2023-09-02 21:41:50]
  • 提交

answer

// what is matter? never mind. 
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2") 
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
#define int long long
#define ull unsigned long long
using namespace std;
inline int read()
{
	char c=getchar();int x=0;bool f=0;
	for(;!isdigit(c);c=getchar())f^=!(c^45);
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
	if(f)x=-x;return x;
}

#define mod 998244353
struct modint{
	int x;
	modint(int o=0){x=o;}
	modint &operator = (int o){return x=o,*this;}
	modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
	modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;}
	modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
	modint &operator ^=(int b){
		modint a=*this,c=1;
		for(;b;b>>=1,a*=a)if(b&1)c*=a;
		return x=c.x,*this;
	}
	modint &operator /=(modint o){return *this *=o^=mod-2;}
	friend modint operator +(modint a,modint b){return a+=b;}
	friend modint operator -(modint a,modint b){return a-=b;}
	friend modint operator *(modint a,modint b){return a*=b;}
	friend modint operator /(modint a,modint b){return a/=b;}
	friend modint operator ^(modint a,int b){return a^=b;}
	friend bool operator ==(modint a,int b){return a.x==b;}
	friend bool operator !=(modint a,int b){return a.x!=b;}
	bool operator ! () {return !x;}
	modint operator - () {return x?mod-x:0;}
	bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}

vector<modint> fac,ifac,iv;
inline void initC(int n)
{
	if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
	int m=iv.size(); ++n;
	if(m>=n)return;
	iv.resize(n),fac.resize(n),ifac.resize(n);
	For(i,m,n-1){
		iv[i]=iv[mod%i]*(mod-mod/i);
		fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
	}
}
inline modint C(int n,int m){
	if(m<0||n<m)return 0;
	return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;

#define maxn 200005
#define inf 0x3f3f3f3f

int n,m,Q;
vector<pii> e[maxn];

int fa[maxn][20],dep[maxn],val[maxn],dis[maxn],vis[maxn],top[maxn];
void dfs(int u,int pa){
	fa[u][0]=pa,dep[u]=dep[pa]+1;
	For(i,1,19)fa[u][i]=fa[fa[u][i-1]][i-1];
	for(auto [v,w]:e[u])
		if(v!=pa){
			dis[v]=dis[u]+w;
			if(vis[v]) val[v]=0,top[v]=v;
			else val[v]=val[u]+w,top[v]=top[u];
			dfs(v,u);
		}
}
int lca(int u,int v){
	if(dep[u]<dep[v])swap(u,v);
	Rep(i,19,0)if(dep[fa[u][i]]>=dep[v])u=fa[u][i];
	if(u==v)return u;
	Rep(i,19,0)if(fa[u][i]!=fa[v][i])u=fa[u][i],v=fa[v][i];
	return fa[u][0];
}
int dist(int u,int v){return dep[u]+dep[v]-2*dep[lca(u,v)];}
int kth(int u,int k){
	For(i,0,19)if(k>>i&1)u=fa[u][i];
	return u;
}
int jump(int u,int v){return kth(u,dep[u]-dep[v]-1);}
int go(int u,int v,int k){
	int d=dep[lca(u,v)];
	return k<=dep[u]-d?kth(u,k):kth(v,dep[v]+dep[u]-2*d-k);
}

int a[maxn];

void work()
{
	n=read(),m=read(),Q=read();
	For(i,1,n)e[i].clear(),val[i]=vis[i]=dis[i]=dep[i]=0;
	For(i,1,m)vis[read()]=1;
	For(i,2,n){
		int u=read(),v=read(),w=read();
		e[u].pb(mkp(v,w));
		e[v].pb(mkp(u,w));
	}
	top[1]=1;
	dfs(1,0);
	For(i,1,n) assert(val[i]==dis[i]-dis[top[i]]);
	For(_,1,Q){
		int k=read();
		For(i,1,k)a[i]=read();
		sort(a+1,a+k+1,[&](int x,int y){
			return val[x]<val[y];
		});
	//	For(i,1,k)cout<<a[i]<<" "<<top[a[i]]<<"\n";
		int res=val[a[k]];
		int lc=0;
		Rep(i,k,1){
			if(top[a[k]]!=top[a[i]]) break;
			if(i==k) lc=a[k];
			else lc=lca(lc,a[i]);
			res=min(res,max(dis[a[k]]-dis[lc],val[a[i-1]]));
		}
		cout<<res<<"\n";
	}
}

signed main()
{
	int T=read();
	while(T--)work();
	return 0;
}
/*
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
*/

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

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 19344kb

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: 635ms
memory: 46320kb

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