Submission #2303337
Source Code Expand
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}
using taplis=tuple<int,int,int>;
struct UnionFindTree{
vector<int>par,sz;
UnionFindTree(int n){
par.resize(n);
sz.resize(n);
for(int i=0;i<n;i++){
par[i]=i;
sz[i]=1;
}
}
int find(int x){
return x==par[x]?x:par[x]=find(par[x]);
}
void unite(int x,int y){
x=find(x);y=find(y);
if(x==y)return;
if(sz[x]<sz[y])swap(x,y);
sz[x]+=sz[y];
par[y]=x;
}
bool areSame(int x,int y){
return find(x)==find(y);
}
int size(int x){
return sz[find(x)];
}
};
int N,Q;
signed main(){
cin>>N>>Q;
priority_queue<taplis,vector<taplis>,greater<taplis>>que;
rep(i,Q){
int a,b,c;
cin>>a>>b>>c;
que.push(make_tuple(c,a,b));
}
UnionFindTree uf(N);
int ans=0;
while(que.size()){
int c,a,b;
tie(c,a,b)=que.top();
que.pop();
if(b==-1){
if(uf.areSame(a,(a+1)%N));
else{
uf.unite(a,(a+1)%N);
ans+=c;
que.push(make_tuple(c+2,(a+1)%N,-1));
}
}
else{
if(!uf.areSame(a,b)){
uf.unite(a,b);
ans+=c;
}
que.push(make_tuple(c+1,a,-1));
que.push(make_tuple(c+2,b,-1));
}
}
cout<<ans<<endl;
return 0;
}
Submission Info
Submission Time |
|
Task |
G - Zigzag MST |
User |
latte0119 |
Language |
C++14 (GCC 5.4.1) |
Score |
1300 |
Code Size |
1919 Byte |
Status |
AC |
Exec Time |
335 ms |
Memory |
17516 KB |
Judge Result
Set Name |
sample |
all |
Score / Max Score |
0 / 0 |
1300 / 1300 |
Status |
|
|
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 |
1 ms |
256 KB |
01-02.txt |
AC |
237 ms |
7920 KB |
01-03.txt |
AC |
306 ms |
8044 KB |
01-04.txt |
AC |
13 ms |
3328 KB |
01-05.txt |
AC |
13 ms |
3328 KB |
01-06.txt |
AC |
15 ms |
3328 KB |
01-07.txt |
AC |
17 ms |
3328 KB |
01-08.txt |
AC |
24 ms |
3456 KB |
01-09.txt |
AC |
43 ms |
3708 KB |
01-10.txt |
AC |
178 ms |
6128 KB |
01-11.txt |
AC |
301 ms |
8560 KB |
01-12.txt |
AC |
325 ms |
8044 KB |
01-13.txt |
AC |
331 ms |
9324 KB |
01-14.txt |
AC |
330 ms |
8940 KB |
01-15.txt |
AC |
328 ms |
8684 KB |
01-16.txt |
AC |
334 ms |
8044 KB |
01-17.txt |
AC |
328 ms |
8044 KB |
01-18.txt |
AC |
288 ms |
17132 KB |
01-19.txt |
AC |
14 ms |
3328 KB |
01-20.txt |
AC |
33 ms |
3584 KB |
01-21.txt |
AC |
107 ms |
6516 KB |
01-22.txt |
AC |
279 ms |
17132 KB |
01-23.txt |
AC |
285 ms |
17516 KB |
01-24.txt |
AC |
29 ms |
3836 KB |
01-25.txt |
AC |
299 ms |
8432 KB |
01-26.txt |
AC |
13 ms |
3328 KB |
01-27.txt |
AC |
37 ms |
3836 KB |
01-28.txt |
AC |
213 ms |
10736 KB |
01-29.txt |
AC |
322 ms |
7532 KB |
01-30.txt |
AC |
335 ms |
8940 KB |
sample-01.txt |
AC |
1 ms |
256 KB |
sample-02.txt |
AC |
1 ms |
256 KB |
sample-03.txt |
AC |
1 ms |
256 KB |