結果

問題 No.2295 Union Path Query (Medium)
ユーザー SSRSSSRS
提出日時 2023-05-05 22:52:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 532 ms / 4,000 ms
コード長 1,601 bytes
コンパイル時間 1,902 ms
コンパイル使用メモリ 170,388 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2024-05-02 17:52:52
合計ジャッジ時間 27,821 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 532 ms
5,376 KB
testcase_04 AC 467 ms
5,376 KB
testcase_05 AC 410 ms
5,376 KB
testcase_06 AC 373 ms
5,376 KB
testcase_07 AC 474 ms
5,376 KB
testcase_08 AC 336 ms
6,528 KB
testcase_09 AC 335 ms
6,400 KB
testcase_10 AC 363 ms
6,400 KB
testcase_11 AC 339 ms
6,528 KB
testcase_12 AC 403 ms
6,400 KB
testcase_13 AC 524 ms
6,528 KB
testcase_14 AC 501 ms
6,400 KB
testcase_15 AC 183 ms
6,400 KB
testcase_16 AC 196 ms
6,528 KB
testcase_17 AC 327 ms
6,400 KB
testcase_18 AC 384 ms
6,400 KB
testcase_19 AC 391 ms
6,400 KB
testcase_20 AC 426 ms
6,528 KB
testcase_21 AC 445 ms
6,528 KB
testcase_22 AC 398 ms
6,528 KB
testcase_23 AC 379 ms
6,400 KB
testcase_24 AC 362 ms
6,528 KB
testcase_25 AC 254 ms
6,400 KB
testcase_26 AC 356 ms
6,400 KB
testcase_27 AC 356 ms
6,400 KB
testcase_28 AC 365 ms
6,400 KB
testcase_29 AC 350 ms
6,400 KB
testcase_30 AC 409 ms
6,400 KB
testcase_31 AC 368 ms
6,400 KB
testcase_32 AC 368 ms
6,400 KB
testcase_33 AC 358 ms
6,400 KB
testcase_34 AC 360 ms
6,528 KB
testcase_35 AC 394 ms
6,400 KB
testcase_36 AC 368 ms
6,400 KB
testcase_37 AC 373 ms
6,400 KB
testcase_38 AC 360 ms
6,400 KB
testcase_39 AC 380 ms
6,400 KB
testcase_40 AC 362 ms
6,400 KB
testcase_41 AC 362 ms
6,400 KB
testcase_42 AC 359 ms
6,528 KB
testcase_43 AC 397 ms
6,400 KB
testcase_44 AC 359 ms
6,400 KB
testcase_45 AC 361 ms
6,528 KB
testcase_46 AC 367 ms
6,400 KB
testcase_47 AC 370 ms
6,400 KB
testcase_48 AC 443 ms
6,400 KB
testcase_49 AC 371 ms
6,400 KB
testcase_50 AC 338 ms
6,528 KB
testcase_51 AC 339 ms
6,400 KB
testcase_52 AC 327 ms
6,528 KB
testcase_53 AC 363 ms
6,400 KB
testcase_54 AC 356 ms
6,400 KB
testcase_55 AC 339 ms
6,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000001;
const long long MOD = 998244353;
struct unionfind{
  vector<int> p, t;
  unionfind(int N): p(N, -1), t(N, INF){
  }
  int root(int x){
    if (p[x] < 0){
      return x;
    } else {
      return root(p[x]);
    }
  }
  bool same(int x, int y){
    return root(x) == root(y);
  }
  int size(int v){
    return -p[root(v)];
  }
  void unite(int x, int y, int w){
    x = root(x);
    y = root(y);
    if (p[x] < p[y]){
      swap(x, y);
    }
    p[y] += p[x];
    p[x] = y;
    t[x] = w;
  }
};
int main(){
  int N, X, Q;
  cin >> N >> X >> Q;
  unionfind UF(N);
  vector<long long> ans(N, 0);
  for (int i = 0; i < Q; i++){
    int t;
    cin >> t;
    if (t == 1){
      int v, w;
      cin >> v >> w;
      int u = X;
      u = UF.root(u);
      v = UF.root(v);
      if (u != v){
        long long res = ans[u] + ans[v] + (long long) UF.size(u) * UF.size(v) % MOD * w;
        res %= MOD;
        UF.unite(u, v, w);
        ans[UF.root(u)] = res;
      }
    }
    if (t == 2){
      int u, v;
      cin >> u >> v;
      if (!UF.same(u, v)){
        cout << -1 << endl;
      } else {
        int ans = 0;
        while (u != v){
          if (UF.t[u] > UF.t[v]){
            swap(u, v);
          }
          ans = UF.t[u];
          u = UF.p[u];
        }
        cout << ans << endl;
        X += ans;
        X %= N;
      }
    }
    if (t == 3){
      int v;
      cin >> v;
      cout << ans[UF.root(v)] << endl;
    }
    if (t == 4){
      int value;
      cin >> value;
      X += value;
      X %= N;
    }
  }
}
0