QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#184554#7103. Red Black TreezxzxzxqAC ✓530ms60428kbC++142.9kb2023-09-20 21:04:032023-09-20 21:04:05

Judging History

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

  • [2023-09-20 21:04:05]
  • 评测
  • 测评结果:AC
  • 用时:530ms
  • 内存:60428kb
  • [2023-09-20 21:04:03]
  • 提交

answer

#include<bits/stdc++.h>

#define int long long

using namespace std;

const int maxn=200200;
//const int maxn=200;

int n,m,s1ze,rt[maxn],f[21][maxn];
int vis[maxn],T,q,bl[maxn],lg[maxn];
int dis[maxn],tmp[maxn],k,ans,dep[maxn];
int d0[maxn];

struct mmm{
	int lca,depth,from;
	bool operator <(mmm &b)
	{
		return depth<b.depth;
	}
}tst[maxn];

int getf(int x)
{
	if(x==bl[x]) return x;
	return bl[x]=getf(bl[x]);
}

void merrr(int x,int y)
{
	bl[getf(x)]=bl[getf(y)];
	return ;
}

struct graph{
	
	int head[maxn];
	
	struct edge{
		int next,to,dis;
	}e[maxn*2];
	
	void addedge(int next,int to,int dis)
	{
		e[++s1ze].to=to;
		e[s1ze].next=head[next];
		e[s1ze].dis=dis;
		head[next]=s1ze;
	}
	
}G;

void init()
{
//	memset(f,0,sizeof(f));
	for(int i=1;i<=n;i++)
	{
		bl[i]=i;
		vis[i]=0;
		dis[i]=0;
		dep[i]=0;
		rt[i]=0;
		s1ze=0;
		G.head[i]=0;
	}
	for(int i=1;i<=n;i++)
	for(int j=0;j<=20;j++)
		f[j][i]=0;
	for(int i=1;i<=n*2;i++)
	G.e[i].next=0;
}

void dfs(int t,int fat)
{
	dep[t]=dep[fat]+1;
	vis[t]=1;
//	f[0][t]=fat;
	for(int i=1;i<=20;i++)
	f[i][t]=f[i-1][f[i-1][t]];
	for(int i=G.head[t];i;i=G.e[i].next)
	{
		int j=G.e[i].to;
		if(rt[j]&&j!=fat&&!vis[j])
		{
			dfs(j,j);
			continue;
		}
		if(j==fat) continue;
		if(vis[j]) continue;
		merrr(t,j);
		dis[j]=dis[t]+G.e[i].dis;
		f[0][j]=t;
		dfs(j,t);
	}
}

int lca(int x,int y)
{
	if(dep[x]<dep[y]) swap(x,y);
	int tot=0;
	while(dep[x]>dep[y])
	{
//		tot++;
		x=f[lg[dep[x]-dep[y]]][x];
//		if(tot==30) break;
	}
	if(x==y) return x;
	for(int i=lg[dep[x]];i>=0;i--)
	{
		if(f[i][x]==f[i][y]) continue;
		x=f[i][x],y=f[i][y];
	}
	return f[0][x];
}

void check(int x)
{
	int cnt=0;
	int b0ans=0;
	for(int i=1;i<=k;i++)
	{
		int y=tmp[i];
		if(getf(x)!=getf(y))
		{
			b0ans=max(b0ans,dis[y]);
			continue;
		}
		else
		{
			int lcaa=lca(x,y);
			if(rt[lcaa]) 
			{
				b0ans=max(b0ans,dis[y]);
				continue;
			}
			tst[++cnt].depth=dep[lcaa];
			tst[cnt].lca=lcaa;
			tst[cnt].from=y;
		}
	}
	sort(tst+1,tst+cnt+1);
	int nowmmxdis=0;
	for(int i=1;i<=cnt;i++)
	{
		ans=min(ans,max(b0ans,max(dis[x]-dis[tst[i].lca],nowmmxdis)));
		nowmmxdis=max(nowmmxdis,dis[tst[i].from]);
	}
	cout<<ans<<"\n";
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>T;
	for(int i=2;i<=maxn-1;i++) lg[i]=lg[i>>1]+1;
	while(T--)
	{
		cin>>n>>m>>q;
		init();
		for(int i=1;i<=n;i++)
		{
			f[0][i]=0;
			rt[i]=0;
		}
		for(int i=1;i<=m;i++)
		{
			int tmp;
			cin>>tmp;
			rt[tmp]=1;
		}
		for(int i=1;i<n;i++)
		{
			int t1,t2,t3;
			cin>>t1>>t2>>t3;
			G.addedge(t1,t2,t3);
			G.addedge(t2,t1,t3);
		}
		dfs(1,1);
		for(int i=1;i<=q;i++)
		{
			ans=0;
			cin>>k;
			int dmmx=0;
			for(int j=1;j<=k;j++)
			{
				cin>>tmp[j];
				if(dis[tmp[j]]>dis[dmmx]) dmmx=tmp[j];
			}
			ans=dis[dmmx];
			check(dmmx);
		}
	}
	
}

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

詳細信息

Test #1:

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

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: 530ms
memory: 60428kb

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