Submission #1112221


Source Code Expand

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

#define fin "\n"

#define FOR(i,bg,ed) for(int i=(bg);i<(ed);i++)
#define REP(i,n) FOR(i,0,n)
template <typename T>
inline void chmin(T &l,T r){l=min(l,r);}
template <typename T>
inline void chmax(T &l,T r){l=max(l,r);}

typedef tuple<int,int,LL> T;
typedef pair<int,int> P;


namespace _DSU{
#define SZ 212345
    int mem[2][SZ];
}
class UnionFind{
private:
    int *par,*rank;
    int find(int x){
	if (par[x] == x) return x;
	else return par[x] = find(par[x]);
    }
public:
    UnionFind(int n,int *par,int *rank) :par(par),rank(rank){
	for(int i = 0; i < n; i++)par[i] = i,rank[i] = 0;
    }
    UnionFind(int n):UnionFind(n,_DSU::mem[0],_DSU::mem[1]){}
    bool unite(int x, int y){
	x = find(x);y = find(y);
	if (x == y)return false;
	if (rank[x] < rank[y]) par[x] = y;
	else{
	    par[y] = x;
	    if (rank[x] == rank[y])rank[x]++;
	}
	return true;
    }
    bool same(int x, int y){
	return find(x) == find(y);
    }
};

#define fi first
#define se second
int N,Q;

T abc[412345];
queue<P> que[412345];
int main(){
    scanf("%d %d",&N,&Q);
    LL LB=1e15;
    REP(i,Q){
        int a,b;LL c;
        scanf("%d %d %lld",&a,&b,&c);
        abc[i]=T(a,b,c);
        chmin(LB,c);
    }
    
    REP(i,Q){
        int a,b;LL c;
        tie(a,b,c)=abc[i];
        c-=LB;
        if(c<2*N&&a!=b){
            que[c].push(P(a,b));
        }
        a=(a+1)%N;
        c++;
        if(c<2*N&&a!=b){
            que[c].push(P(a,b));
        }
    }
    UnionFind uf(N);
    LL res=LB*(N-1);
    REP(i,2*N){
        while(!que[i].empty()){
            auto v=que[i].front();que[i].pop();
            int a,b;tie(a,b)=v;
            int c=i;
            if(!uf.same(a,b)){
                //cout<<a<<b<<c<<endl;
                res+=c;
                uf.unite(a,b);
                que[c+2].push(P((a+1)%N,(b+1)%N));
            }
        }
    }

    
    
    cout<<res<<endl;

    return 0;
}

Submission Info

Submission Time
Task G - Zigzag MST
User btk15049
Language C++14 (GCC 5.4.1)
Score 0
Code Size 2066 Byte
Status MLE
Exec Time 339 ms
Memory 288640 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:58:25: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&N,&Q);
                         ^
./Main.cpp:62:37: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %lld",&a,&b,&c);
                                     ^

Judge Result

Set Name sample all
Score / Max Score 0 / 0 0 / 1300
Status
MLE × 3
MLE × 36
Set Name Test Cases
sample sample-01.txt, sample-02.txt, sample-03.txt
all sample-01.txt, sample-02.txt, sample-03.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt, sample-01.txt, sample-02.txt, sample-03.txt
Case Name Status Exec Time Memory
01-01.txt MLE 205 ms 279936 KB
01-02.txt MLE 255 ms 284032 KB
01-03.txt MLE 272 ms 285312 KB
01-04.txt MLE 217 ms 281216 KB
01-05.txt MLE 217 ms 281216 KB
01-06.txt MLE 218 ms 281216 KB
01-07.txt MLE 215 ms 281216 KB
01-08.txt MLE 214 ms 281216 KB
01-09.txt MLE 220 ms 281216 KB
01-10.txt MLE 286 ms 283264 KB
01-11.txt MLE 322 ms 284544 KB
01-12.txt MLE 336 ms 285312 KB
01-13.txt MLE 338 ms 285312 KB
01-14.txt MLE 339 ms 285312 KB
01-15.txt MLE 338 ms 285312 KB
01-16.txt MLE 337 ms 285312 KB
01-17.txt MLE 338 ms 285312 KB
01-18.txt MLE 298 ms 286976 KB
01-19.txt MLE 217 ms 281216 KB
01-20.txt MLE 234 ms 281216 KB
01-21.txt MLE 253 ms 284288 KB
01-22.txt MLE 295 ms 288640 KB
01-23.txt MLE 294 ms 288640 KB
01-24.txt MLE 221 ms 281216 KB
01-25.txt MLE 300 ms 285312 KB
01-26.txt MLE 227 ms 281216 KB
01-27.txt MLE 241 ms 281344 KB
01-28.txt MLE 276 ms 285312 KB
01-29.txt MLE 295 ms 288000 KB
01-30.txt MLE 301 ms 288640 KB
sample-01.txt MLE 204 ms 279936 KB
sample-02.txt MLE 204 ms 279936 KB
sample-03.txt MLE 205 ms 279936 KB