QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#167068#6520. Classic Problemucup-team134#RE 1ms3700kbC++142.0kb2023-09-07 00:59:362023-09-07 00:59:38

Judging History

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

  • [2023-09-07 00:59:38]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3700kb
  • [2023-09-07 00:59:36]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long

const int N=400050;
int p[N];
int Find(int x){return p[x]==x?x:p[x]=Find(p[x]);}
bool Union(int x,int y){
	//printf("Union %i %i\n",x,y);
	x=Find(x);
	y=Find(y);
	if(x==y)return false;
	p[x]=y;
	return true;
}
void Init(int n){
	//printf("Init %i\n",n);
	for(int i=1;i<=n;i++){
		p[i]=i;
	}
}

int main(){
	int t;
	scanf("%i",&t);
	while(t--){
		int n,m;
		scanf("%i %i",&n,&m);
		map<int,vector<int>> E;
		vector<array<int,3>> edges;
		for(int i=1;i<=m;i++){
			int u,v,w;
			scanf("%i %i %i",&u,&v,&w);
			E[u].pb(v);
			E[v].pb(u);
			edges.pb({w,u,v});
		}
		vector<pair<int,int>> nodes;
		ll ans=0;
		int last=1;
		for(auto it=E.begin();it!=E.end();it++){
			if(it->first!=last){
				nodes.pb({last,it->first-1});
				ans+=nodes.back().second-nodes.back().first;
			}
			nodes.pb({it->first,it->first});
			last=it->first+1;
		}
		if(last<=n){
			nodes.pb({last,n});
			ans+=n-last;
		}
		Init(nodes.size());
		int SQ=min(100,(int)sqrt(m)*2);
		for(int i=1;i<=nodes.size();i++){
			int L=nodes[i-1].first;
			int R=nodes[i-1].second;
			//printf("%i %i\n",L,R);
			{
				vector<int>& es=E[L];
				sort(es.begin(),es.end());
				int ptr=(int)es.size()-1;
				int cnt=0;
				for(int j=i-1;j>=1;j--){
					int node=nodes[j-1].second;
					while(ptr>=0 && es[ptr]>node)ptr--;
					if(ptr==-1 || es[ptr]!=node){
						edges.pb({L-node,i,j});
						cnt++;
						if(cnt>=SQ)break;
					}
				}
			}

			{
				vector<int>&es=E[R];
				sort(es.begin(),es.end());
				int ptr=0;
				int cnt=0;
				for(int j=i+1;j<=nodes.size();j++){
					int node=nodes[j-1].first;
					while(ptr<es.size() && es[ptr]<node)ptr++;
					if(ptr==es.size() || es[ptr]!=node){
						edges.pb({node-R,i,j});
						cnt++;
						if(cnt>=SQ)break;
					}
				}
			}
		}
		sort(edges.begin(),edges.end());
		for(auto e:edges){
			if(Union(e[1],e[2])){
				ans+=e[0];
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3700kb

input:

3
5 3
1 2 5
2 3 4
1 5 0
5 0
5 4
1 2 1000000000
1 3 1000000000
1 4 1000000000
1 5 1000000000

output:

4
4
1000000003

result:

ok 3 number(s): "4 4 1000000003"

Test #2:

score: -100
Runtime Error

input:

16
1000000000 0
447 99681
1 2 1000000000
1 3 1000000000
2 3 1000000000
1 4 1000000000
2 4 1000000000
3 4 1000000000
1 5 1000000000
2 5 1000000000
3 5 1000000000
4 5 1000000000
1 6 1000000000
2 6 1000000000
3 6 1000000000
4 6 1000000000
5 6 1000000000
1 7 1000000000
2 7 1000000000
3 7 1000000000
4 7 ...

output:


result: