Submission #994693
Source Code Expand
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sstream>
#include <functional>
#include <map>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <list>
#include <numeric>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = 1LL<<29;
const ll mod = 1e9+7;
#define rep(i,n) for(int (i)=0;(i)<(ll)(n);++(i))
#define repd(i,n,d) for(ll (i)=0;(i)<(ll)(n);(i)+=(d))
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset((m),(v),sizeof(m))
#define chmin(X,Y) ((X)>(Y)?X=(Y),true:false)
#define chmax(X,Y) ((X)<(Y)?X=(Y),true:false)
#define fst first
#define snd second
#define UNIQUE(x) (x).erase(unique(all(x)),(x).end())
template<class T> ostream &operator<<(ostream &os, const vector<T> &v){int n=v.size();rep(i,n)os<<v[i]<<(i==n-1?"":" ");return os;}
#define MAX_N 210000
int n, Q;
struct UF {
int par[MAX_N],rank[MAX_N];
void init(int n){
for(int i = 0; i < n; i++){
par[i] = i;
rank[i] = 0;
}
}
int find(int x){
if(par[x] == x){
return x;
}else{
return par[x] = find(par[x]);
}
}
void unite(int x, int y){
x = find(x);
y = find(y);
if(x == y) return;
if(rank[x] < rank[y]){
par[x] = y;
}else{
par[y] = par[x];
if(rank[x] == rank[y]) rank[y]++;
}
}
bool same(int x, int y){
return find(x) == find(y);
}
};
set<P> used;
int main(){
cin>>n>>Q;
priority_queue<PP> q;
rep(i, Q){
int a, b, c;
cin>>a>>b>>c;
q.push(PP(-c, P(a, b)));
}
UF uf;
uf.init(n);
ll res = 0;
int cnt = 0;
while(cnt<n-1){
PP p = q.top(); q.pop();
int c = -p.fst, a = p.snd.fst, b = p.snd.snd;
if(!used.count(P(a, b))) q.push(PP(-c-1, P(b, a+1>=n?0:a+1)));
//cerr<<a<<" "<<b<<" "<<c<<endl;
if(uf.same(a, b)) continue;
uf.unite(a, b);
res += c;
cnt++;
//cerr<<res<<endl;
}
cout<<res<<endl;
return 0;
}
Submission Info
Submission Time |
|
Task |
G - Zigzag MST |
User |
Lepton |
Language |
C++14 (GCC 5.4.1) |
Score |
0 |
Code Size |
2272 Byte |
Status |
TLE |
Exec Time |
2102 ms |
Memory |
6636 KB |
Judge Result
Set Name |
sample |
all |
Score / Max Score |
0 / 0 |
0 / 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 |
Case Name |
Status |
Exec Time |
Memory |
01-01.txt |
AC |
3 ms |
256 KB |
01-02.txt |
AC |
161 ms |
6512 KB |
01-03.txt |
AC |
285 ms |
6636 KB |
01-04.txt |
AC |
8 ms |
1792 KB |
01-05.txt |
AC |
14 ms |
1792 KB |
01-06.txt |
AC |
18 ms |
1792 KB |
01-07.txt |
AC |
30 ms |
1792 KB |
01-08.txt |
AC |
113 ms |
1792 KB |
01-09.txt |
AC |
179 ms |
2172 KB |
01-10.txt |
AC |
451 ms |
4336 KB |
01-11.txt |
AC |
347 ms |
6512 KB |
01-12.txt |
AC |
519 ms |
6636 KB |
01-13.txt |
AC |
698 ms |
6636 KB |
01-14.txt |
AC |
634 ms |
6636 KB |
01-15.txt |
AC |
554 ms |
6636 KB |
01-16.txt |
AC |
525 ms |
6636 KB |
01-17.txt |
AC |
555 ms |
6636 KB |
01-18.txt |
TLE |
2102 ms |
6636 KB |
01-19.txt |
AC |
20 ms |
1792 KB |
01-20.txt |
AC |
180 ms |
1920 KB |
01-21.txt |
AC |
456 ms |
3316 KB |
01-22.txt |
AC |
629 ms |
6636 KB |
01-23.txt |
AC |
636 ms |
6636 KB |
01-24.txt |
TLE |
2102 ms |
2172 KB |
01-25.txt |
TLE |
2102 ms |
6636 KB |
01-26.txt |
AC |
14 ms |
1792 KB |
01-27.txt |
TLE |
2102 ms |
2172 KB |
01-28.txt |
TLE |
2102 ms |
4848 KB |
01-29.txt |
TLE |
2102 ms |
6512 KB |
01-30.txt |
TLE |
2102 ms |
6636 KB |
sample-01.txt |
AC |
3 ms |
256 KB |
sample-02.txt |
AC |
3 ms |
256 KB |
sample-03.txt |
AC |
3 ms |
256 KB |