結果

問題 No.2290 UnUnion Find
ユーザー moharan627moharan627
提出日時 2023-05-05 22:06:47
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 459 ms / 2,000 ms
コード長 5,592 bytes
コンパイル時間 7,067 ms
コンパイル使用メモリ 312,076 KB
実行使用メモリ 14,700 KB
最終ジャッジ日時 2023-08-15 04:15:07
合計ジャッジ時間 29,593 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 330 ms
4,384 KB
testcase_03 AC 344 ms
8,884 KB
testcase_04 AC 346 ms
9,000 KB
testcase_05 AC 357 ms
8,900 KB
testcase_06 AC 353 ms
8,916 KB
testcase_07 AC 346 ms
8,932 KB
testcase_08 AC 346 ms
8,824 KB
testcase_09 AC 337 ms
8,924 KB
testcase_10 AC 348 ms
8,824 KB
testcase_11 AC 351 ms
8,876 KB
testcase_12 AC 347 ms
8,872 KB
testcase_13 AC 358 ms
8,828 KB
testcase_14 AC 360 ms
8,928 KB
testcase_15 AC 342 ms
8,924 KB
testcase_16 AC 350 ms
8,836 KB
testcase_17 AC 345 ms
8,876 KB
testcase_18 AC 346 ms
8,996 KB
testcase_19 AC 391 ms
9,620 KB
testcase_20 AC 454 ms
14,700 KB
testcase_21 AC 342 ms
4,380 KB
testcase_22 AC 348 ms
5,932 KB
testcase_23 AC 357 ms
6,096 KB
testcase_24 AC 443 ms
11,900 KB
testcase_25 AC 347 ms
5,308 KB
testcase_26 AC 459 ms
13,360 KB
testcase_27 AC 451 ms
12,436 KB
testcase_28 AC 384 ms
7,940 KB
testcase_29 AC 427 ms
11,004 KB
testcase_30 AC 350 ms
4,620 KB
testcase_31 AC 396 ms
10,320 KB
testcase_32 AC 355 ms
5,440 KB
testcase_33 AC 379 ms
7,940 KB
testcase_34 AC 453 ms
14,640 KB
testcase_35 AC 429 ms
13,096 KB
testcase_36 AC 346 ms
5,504 KB
testcase_37 AC 361 ms
7,368 KB
testcase_38 AC 354 ms
5,544 KB
testcase_39 AC 361 ms
6,236 KB
testcase_40 AC 450 ms
12,568 KB
testcase_41 AC 343 ms
5,104 KB
testcase_42 AC 408 ms
9,408 KB
testcase_43 AC 400 ms
9,284 KB
testcase_44 AC 354 ms
8,872 KB
testcase_45 AC 357 ms
8,900 KB
testcase_46 AC 368 ms
8,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#if !__INCLUDE_LEVEL__

#include __FILE__
// Union-Find
struct UnionFind {
    vector<int> par, rank, siz;
    set<int> Root;

    // 構造体の初期化
    UnionFind(int n) : par(n,-1), rank(n,0), siz(n,1) {
        rep(i,n)Root.insert(i);
     }

    // 根を求める
    int root(int x) {
        if (par[x]==-1) return x; // x が根の場合は x を返す
        else return par[x] = root(par[x]); // 経路圧縮
    }

    // x と y が同じグループに属するか (= 根が一致するか)
    bool issame(int x, int y) {
        return root(x)==root(y);
    }

    // x を含むグループと y を含むグループを併合する
    bool unite(int x, int y) {
        int rx = root(x), ry = root(y); // x 側と y 側の根を取得する
        if (rx==ry) return false; // すでに同じグループのときは何もしない
        // union by rank
        if (rank[rx]<rank[ry]) swap(rx, ry); // ry 側の rank が小さくなるようにする
        par[ry] = rx; // ry を rx の子とする
        Root.erase(ry);
        if (rank[rx]==rank[ry]) rank[rx]++; // rx 側の rank を調整する
        siz[rx] += siz[ry]; // rx 側の siz を調整する
        return true;
    }

    // x を含む根付き木のサイズを求める
    int size(int x) {
        return siz[root(x)];
    }
    int query(int v){
        if(Root.size()==1)return -1;
        int rv = root(v);
        auto itr = Root.begin();
        if(*itr==rv)itr++;
        return *itr+1;
    }
};
int main()
{
    ll N,Q;
    cin >> N >> Q;
    UnionFind uf(N);
    rep(q,Q){
        int t;cin >> t;
        if(t==1){
            int u,v;cin >> u >> v;
            u-=1;
            v-=1;
            uf.unite(u,v);
        }
        if(t==2){
            int v;cin >> v;
            v-=1;
            cout << uf.query(v) << "\n";
        }
    }
}

#else
//tie(a, b, c) = t
//repをllにしているので時間ギリギリならintに変換
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
#define rep(i, n) for(long long int i = 0; i < n; i++)
#define rrep(i, n) for(long long int i = n-1; i >= 0; i--)
#define range(i, m, n) for(long long int i = m; i < n; i++)
#define fore(i,a) for(auto &i:a)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define Sum(v) accumulate(all(v),0LL)
#define minv(v) *min_element(all(v))
#define maxv(v) *max_element(all(v))
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
const ll INF = 1e16;
const ll MOD1 = 1000000007;
const ll MOD2 = 998244353;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
ll SN(char s){return ll(s-'0');}
ll SN(string s){return stoll(s);}
int alpN(char s){return int(s-'a');}
int AlpN(char s){return int(s-'A');}
int Nalp(int n){return char(n+97);}
int NAlp(int n){return char(n+65);}
using mint = modint;
using mint1 = modint1000000007;
using mint2 = modint998244353;
using pll = pair<long long, long long>;
template <class T>ostream &operator<<(ostream &o,const vector<T>&v){for(int i=0;i<(int)v.size();i++)o<<(i>0?" ":"")<<v[i];return o;}//vector空白区切り出力
ostream& operator<<(ostream& os, const mint1& N) {return os << N.val();}//mint出力。デフォはmint1
ostream& operator<<(ostream& os, const mint2& N) {return os << N.val();}
template<class T> bool contain(const std::string& s, const T& v) {
    return s.find(v) != std::string::npos;
}
ll max(int x,ll y){return max((ll)x,y);}
ll max(ll x,int y){return max(x,(ll)y);}
ll min(int x,ll y){return min((ll)x,y);}
ll min(ll x,int y){return min(x,(ll)y);}
template <typename T>
struct edge {
    int src, to;
    T cost;
    edge(int to, T cost) : src(-1), to(to), cost(cost) {}
    edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
    edge& operator=(const int& x) {
        to = x;
        return *this;
    }
    operator int() const { return to; }
};

template <typename T>
using Edges = vector<edge<T> >;
template <typename T>
using WeightedGraph = vector<Edges<T> >;
using UnWeightedGraph = vector<vector<int> >;
template <typename T>
using Matrix = vector<vector<T> >;

//unorderd_mapの拡張…https://qiita.com/hamamu/items/4d081751b69aa3bb3557
template<class T> size_t HashCombine(const size_t seed,const T &v){
    return seed^(std::hash<T>()(v)+0x9e3779b9+(seed<<6)+(seed>>2));
}
/* pair用 */
template<class T,class S> struct std::hash<std::pair<T,S>>{
    size_t operator()(const std::pair<T,S> &keyval) const noexcept {
        return HashCombine(std::hash<T>()(keyval.first), keyval.second);
    }
};
/* vector用 */
template<class T> struct std::hash<std::vector<T>>{
    size_t operator()(const std::vector<T> &keyval) const noexcept {
        size_t s=0;
        for (auto&& v: keyval) s=HashCombine(s,v);
        return s;
    }
};
/* tuple用 */
template<int N> struct HashTupleCore{
    template<class Tuple> size_t operator()(const Tuple &keyval) const noexcept{
        size_t s=HashTupleCore<N-1>()(keyval);
        return HashCombine(s,std::get<N-1>(keyval));
    }
};
template <> struct HashTupleCore<0>{
    template<class Tuple> size_t operator()(const Tuple &keyval) const noexcept{ return 0; }
};
template<class... Args> struct std::hash<std::tuple<Args...>>{
    size_t operator()(const tuple<Args...> &keyval) const noexcept {
        return HashTupleCore<tuple_size<tuple<Args...>>::value>()(keyval);
    }
};
ll ceil(ll a, ll b){
  return (a + b - 1) / b;
}
#endif
0