RE 75求调qwq 除了少数的RE剩下的都过了
查看原帖
RE 75求调qwq 除了少数的RE剩下的都过了
186068
我怂了楼主2023/9/17 11:32

O(n2)O(n^{2}) RE的最后25pts的大数据(个人猜测),一开始以为是vector的问题,后来给换成了另一坨链式前向星也依然寄掉了qwq qwq

这是改完链式前向星之后的代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2505,maxm=1e5+5;
int n,m,k,sco[maxn],head[maxm<<1],to[maxm<<1],last[maxn],cnt,cnt1,head1[maxn*(maxn-1)],last1[maxn],to1[maxn*(maxn-1)];
pair<int,int> val[maxn][3];
bool vis1[maxn];
bool vis[maxn][maxn];
void add(int x,int y){
	head[++cnt]=last[x];
	to[cnt]=y,last[x]=cnt;
}
void add1(int x,int y){
	head1[++cnt1]=last1[x];
	to1[cnt1]=y,last1[x]=cnt1;
}
void bfs(int x){
	queue<pair<int,int> > q;
	q.push({x,-1});
	while(q.front().second<k){
		auto it=q.front();
		q.pop();
		it.second++;
		for(int i=last[it.first];i;i=head[i]){
			if(to[i]==x||vis[x][to[i]]){
				continue;
			}
			add1(x,to[i]);
			vis[x][to[i]]=true;
			q.push({to[i],it.second});
		}
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m>>k;
	for(int i=2;i<=n;i++){
		cin>>sco[i];
	}
	for(int i=1;i<=m;i++){
		int x,y;
		cin>>x>>y;
		add(x,y),add(y,x);
	}
	for(int i=1;i<=n;i++){
		bfs(i);
	}
	int i,j;
	for(int i1=last1[1];i1;i1=head1[i1]){
		i=to1[i1];
		if(i==1){
			continue;
		}
		for(int j1=last1[i];j1;j1=head1[j1]){
			j=to1[j1];
			if(j==1){
				continue;
			}
			vis1[j]=true;
			if(val[j][0].first<sco[i]+sco[j]){
				val[j][2]=val[j][1];
				val[j][1]=val[j][0];
				val[j][0]={sco[i]+sco[j],i};
			}
			else if(val[j][1].first<sco[i]+sco[j]){
				val[j][2]=val[j][1];
				val[j][1]={sco[i]+sco[j],i};
			}
			else if(val[j][2].first<sco[i]+sco[j]){
				val[j][2]={sco[i]+sco[j],i};
			}
		}
	}
	int ans=-0xffff;
	for(int i=2;i<=n;i++){
		for(int j=2;j<=n;j++){
			if(vis[i][j]&&vis1[i]&&vis1[j]){
				for(int p=0;p<3;p++){
					for(int q=0;q<3;q++){
						if((val[i][p].second^j)&&(val[i][p].second^val[j][q].second)&&(val[j][q].second^i)){
							ans=max(ans,val[i][p].first+val[j][q].first);
						}
					}
				}
			}
		}
	}
	cout<<ans;
}

这是原来用vector的代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2505,maxm=1e4+5;
vector<int> v[maxn];
int n,m,k,sco[maxn],head[maxm<<1],to[maxm<<1],last[maxn],cnt;
pair<int,int> val[maxn][3];
bool vis1[maxn];
bool vis[maxn][maxn];
void add(int x,int y){
	head[++cnt]=last[x];
	to[cnt]=y,last[x]=cnt;
}
void bfs(int x){
	queue<pair<int,int> > q;
	q.push({x,-1});
	while(q.front().second<k){
		auto it=q.front();
		q.pop();
		it.second++;		for(int i=last[it.first];i;i=head[i]){
			if(to[i]==x||vis[x][to[i]]){
				continue;
			}
			v[x].push_back(to[i]);
			vis[x][to[i]]=true;
			q.push({to[i],it.second});
		}
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m>>k;
	for(int i=2;i<=n;i++){
		cin>>sco[i];
	}
	for(int i=1;i<=m;i++){
		int x,y;
		cin>>x>>y;
		add(x,y),add(y,x);
	}
	for(int i=1;i<=n;i++){
		bfs(i);
	}
	for(int i:v[1]){
		if(i==1){
			continue;
		}
		for(int j:v[i]){
			if(j==1){
				continue;
			}
			vis1[j]=true;
			if(val[j][0].first<sco[i]+sco[j]){
				val[j][2]=val[j][1];
				val[j][1]=val[j][0];
				val[j][0]={sco[i]+sco[j],i};
			}
			else if(val[j][1].first<sco[i]+sco[j]){
				val[j][2]=val[j][1];
				val[j][1]={sco[i]+sco[j],i};
			}
			else if(val[j][2].first<sco[i]+sco[j]){
				val[j][2]={sco[i]+sco[j],i};
			}
		}
	}
	int ans=-0x8f;
	for(int i=2;i<=n;i++){
		for(int j=2;j<=n;j++){
			if(vis[i][j]&&vis1[i]&&vis1[j]){
				for(int p=0;p<3;p++){
					for(int q=0;q<3;q++){
						if((val[i][p].second^j)&&(val[i][p].second^val[j][q].second)&&(val[j][q].second^i)){
							ans=max(ans,val[i][p].first+val[j][q].first);
						}
					}
				}
			}
		}
	}
	cout<<ans;
}

哪位大佬帮着调一下呗qwq,蒟蒻实在不会了。

2023/9/17 11:32
加载中...