Submission #1112209


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 200000
    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[212345];
queue<P> que[212345];
int main(){
    scanf("%d %d",&N,&Q);
    LL LB=1e15;
    REP(i,Q){
        int a,b;LL c;
        scanf("%d %d %lld",&b,&a,&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));
        }
        swap(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 2086 Byte
Status RE
Exec Time 260 ms
Memory 151168 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",&b,&a,&c);
                                     ^

Judge Result

Set Name sample all
Score / Max Score 0 / 0 0 / 1300
Status
AC × 3
AC × 9
RE × 27
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 AC 108 ms 146816 KB
01-02.txt AC 158 ms 146816 KB
01-03.txt RE 246 ms 146816 KB
01-04.txt RE 195 ms 147840 KB
01-05.txt RE 196 ms 147840 KB
01-06.txt RE 185 ms 146816 KB
01-07.txt RE 187 ms 146816 KB
01-08.txt RE 188 ms 146816 KB
01-09.txt RE 190 ms 146816 KB
01-10.txt RE 214 ms 146816 KB
01-11.txt AC 205 ms 147200 KB
01-12.txt RE 241 ms 146816 KB
01-13.txt RE 241 ms 146816 KB
01-14.txt RE 242 ms 146816 KB
01-15.txt RE 241 ms 146816 KB
01-16.txt RE 242 ms 146816 KB
01-17.txt RE 242 ms 146816 KB
01-18.txt RE 255 ms 149376 KB
01-19.txt RE 198 ms 147840 KB
01-20.txt RE 193 ms 147840 KB
01-21.txt RE 213 ms 148736 KB
01-22.txt RE 255 ms 151168 KB
01-23.txt RE 252 ms 151168 KB
01-24.txt RE 199 ms 147840 KB
01-25.txt RE 257 ms 146816 KB
01-26.txt RE 197 ms 147840 KB
01-27.txt RE 198 ms 147968 KB
01-28.txt RE 236 ms 149888 KB
01-29.txt RE 253 ms 150528 KB
01-30.txt RE 260 ms 151168 KB
sample-01.txt AC 110 ms 146816 KB
sample-02.txt AC 109 ms 146816 KB
sample-03.txt AC 108 ms 146816 KB