結果
| 問題 | 
                            No.1059 素敵な集合
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2024-11-30 02:07:22 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                WA
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,757 bytes | 
| コンパイル時間 | 920 ms | 
| コンパイル使用メモリ | 88,728 KB | 
| 実行使用メモリ | 66,944 KB | 
| 最終ジャッジ日時 | 2024-11-30 02:08:14 | 
| 合計ジャッジ時間 | 51,301 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | WA * 1 TLE * 2 | 
| other | WA * 5 TLE * 14 | 
ソースコード
#include <vector>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define FOR(i, N) for(int i=0; i<(N); i++)
#define REP(i, N) for(int i=1; i<=(N); i++)
typedef pair<int, int> Edge;
struct Union_Find {
    vector<int> parent, rank;
    Union_Find(int n){
        parent.resize(n, 0);
        rank.resize(n, 0);
        for (int i=0; i<n; i++){
            parent[i] = i;
            rank[i] = 0;
        }
    }
    
    int get_parent(int x){
        if (x != parent[x]){
            parent[x] = get_parent(parent[x]);
        }
        return parent[x];
    }
    void unite(int x, int y){
        x = get_parent(x);
        y = get_parent(y);
        
        if (rank[x] > rank[y]){
            parent[y] = x;
        }else{
            parent[x] = y;
            if (rank[x] == rank[y]){
                rank[y]++;
            }
        }
        return;
    }
};
int main() {
    int L, R;
    vector<pair<int, Edge>> edges;
    
    cin >> L >> R;
    for(int i=L; i<R; i++){
        for(int j=i+1; j<=R; j++){
            int cost = min(i % j, j % i);
            if (cost > 1){ continue; }
            edges.push_back({cost, {i, j}});
        }
    }
    
    sort(edges.begin(), edges.end());
    
    int V = R - L + 1;
    int v_cnt = 0;
    int answer = 0;
    
    Union_Find uf(V);
    cout << "\n";
    for (auto e : edges){
        int cost = e.first;
        int e1 = e.second.first - L;
        int e2 = e.second.second - L;
        
        if (uf.get_parent(e1) != uf.get_parent(e2)){
            uf.unite(e1, e2);
            answer += cost;
            v_cnt++;
        }
        
        if (v_cnt == V-1){
            break;
        }
    }
    
    cout << answer << endl;;
    
	return 0;
}