QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#142012 | #6520. Classic Problem | MaxBlazeResFire | WA | 1595ms | 111904kb | C++20 | 3.4kb | 2023-08-18 10:55:55 | 2023-08-18 10:55:56 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define MAXN 2000005
#define INF 1e18
int n,m,key[MAXN],cnt = 0,pcnt = 0;
int Fa[MAXN],f[MAXN],g[MAXN],Rcnt = 0;
int L[MAXN],R[MAXN],iskey[MAXN];
int minp[MAXN],minc[MAXN],Ans;
map< pair<int,int> , int > M;
map<int,int> vis,bel;
inline int find( int x ){ return Fa[x] == x ? x : Fa[x] = find( Fa[x] ); }
struct edge{
int from,to,dis;
edge(){}
edge( int u , int v , int w ): from(u),to(v),dis(w){}
}E[MAXN];
inline void update( int tar , int id , int dis ){ if( dis < minp[tar] ) minp[tar] = dis,minc[tar] = id; }
inline bool check( int u , int v ){ if( u > v ) swap( u , v ); return M[make_pair( u , v )]; }
inline void Boruvka(){
for( int i = 1 ; i <= pcnt ; i ++ ) minp[i] = INF,minc[i] = 0,f[i] = g[i] = 0;
for( int i = 2 ; i <= pcnt ; i ++ ){
if( !iskey[i] ){
if( find( i ) == find( i - 1 ) ) f[i] = f[i - 1];
else f[i] = i - 1;
}
else{
int now = i - 1;
while( 1 ){
if( !now ){ f[i] = 0; break; }
if( find( i ) == find( now ) ) now = f[now];
else if( check( i , now ) ) now --;
else{ f[i] = now; break; }
}
}
}
for( int i = pcnt - 1 ; i >= 1 ; i -- ){
if( !iskey[i] ){
if( find( i ) == find( i + 1 ) ) g[i] = g[i + 1];
else g[i] = i + 1;
}
else{
int now = i + 1;
while( 1 ){
if( now > pcnt ){ g[i] = 0; break; }
if( find( i ) == find( now ) ) now = g[now];
else if( check( i , now ) ) now ++;
else{ g[i] = now; break; }
}
}
}
//for( int i = 1 ; i <= pcnt ; i ++ ) cerr << f[i] << " " << g[i] << "\n";
for( int i = 1 ; i <= pcnt ; i ++ ){
if( f[i] ) update( find( i ) , f[i] , L[i] - R[f[i]] );
if( g[i] ) update( find( i ) , g[i] , L[g[i]] - R[i] );
}
for( int i = 1 ; i <= m ; i ++ ){
int u = E[i].from,v = E[i].to,w = E[i].dis;
int U = find( bel[u] ),V = find( bel[v] );
if( U != V ){
update( U , V , w );
update( V , U , w );
}
}
//for( int i = 1 ; i <= pcnt ; i ++ ) cerr << minp[i] << " " << minc[i] << "\n";
for( int i = 1 ; i <= pcnt ; i ++ ){
if( minc[i] ){
int u = i,v = minc[i];
int U = find( u ),V = find( v );
if( U != V ) Fa[U] = V,Rcnt --,Ans += minp[i];//cerr << U << " " << V << " " << Ans << "\n";
}
}
}
inline void solve(){
scanf("%lld%lld",&n,&m); Ans = 0; M.clear(),vis.clear(),bel.clear();
for( int i = 1 ; i <= m ; i ++ ){
int u,v,w; scanf("%lld%lld%lld",&u,&v,&w);
E[i] = edge( u , v , w );
if( !vis[u] ) key[++cnt] = u,vis[u] = 1;
if( !vis[v] ) key[++cnt] = v,vis[v] = 1;
}
sort( key + 1 , key + cnt + 1 );
int last = 1;
for( int i = 1 ; i <= cnt ; i ++ ){
if( key[i] > last ) L[++pcnt] = last,R[pcnt] = key[i] - 1;
L[++pcnt] = key[i],R[pcnt] = key[i],iskey[pcnt] = 1,bel[key[i]] = pcnt;
last = key[i] + 1;
}
if( last <= n ) L[++pcnt] = last,R[pcnt] = n;
for( int i = 1 ; i <= m ; i ++ ) M[make_pair( bel[E[i].from] , bel[E[i].to] )] = 1;
for( int i = 1 ; i <= pcnt ; i ++ ) Fa[i] = i,Ans += R[i] - L[i];
Rcnt = pcnt;
while( Rcnt > 1 ) Boruvka();
printf("%lld\n",Ans);
for( int i = 1 ; i <= pcnt ; i ++ ) L[i] = R[i] = Fa[i] = 0,iskey[i] = 0; pcnt = 0;
for( int i = 1 ; i <= cnt ; i ++ ) key[i] = 0; cnt = 0,Rcnt = 0;
for( int i = 1 ; i <= m ; i ++ ) E[i] = edge( 0 , 0 , 0 );
}
signed main(){
int testcase; scanf("%lld",&testcase);
while( testcase -- ) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 12104kb
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
Wrong Answer
time: 1595ms
memory: 111904kb
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:
999999999 446000000000 748120977 999999680 999899999 999966830 127 4 2186 1562 1176 5100 5 503 679 4
result:
wrong answer 3rd numbers differ - expected: '732256441', found: '748120977'