QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#167065 | #6520. Classic Problem | ucup-team134# | RE | 1ms | 3732kb | C++14 | 2.0kb | 2023-09-07 00:57:55 | 2023-09-07 00:57:55 |
Judging History
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=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: 3732kb
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 ...