結果

問題 No.2295 Union Path Query (Medium)
ユーザー SSRSSSRS
提出日時 2023-05-05 22:52:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 546 ms / 4,000 ms
コード長 1,601 bytes
コンパイル時間 2,578 ms
コンパイル使用メモリ 167,588 KB
実行使用メモリ 6,424 KB
最終ジャッジ日時 2023-08-15 05:45:58
合計ジャッジ時間 30,943 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 535 ms
4,384 KB
testcase_04 AC 501 ms
4,952 KB
testcase_05 AC 430 ms
4,700 KB
testcase_06 AC 393 ms
4,872 KB
testcase_07 AC 504 ms
4,380 KB
testcase_08 AC 374 ms
6,116 KB
testcase_09 AC 380 ms
6,120 KB
testcase_10 AC 399 ms
6,124 KB
testcase_11 AC 371 ms
6,164 KB
testcase_12 AC 435 ms
6,104 KB
testcase_13 AC 544 ms
6,188 KB
testcase_14 AC 546 ms
6,120 KB
testcase_15 AC 205 ms
6,108 KB
testcase_16 AC 202 ms
6,156 KB
testcase_17 AC 357 ms
6,104 KB
testcase_18 AC 423 ms
6,192 KB
testcase_19 AC 429 ms
6,276 KB
testcase_20 AC 403 ms
6,168 KB
testcase_21 AC 416 ms
6,204 KB
testcase_22 AC 435 ms
6,208 KB
testcase_23 AC 408 ms
6,284 KB
testcase_24 AC 397 ms
6,120 KB
testcase_25 AC 262 ms
6,192 KB
testcase_26 AC 393 ms
6,272 KB
testcase_27 AC 401 ms
6,420 KB
testcase_28 AC 395 ms
6,424 KB
testcase_29 AC 398 ms
6,188 KB
testcase_30 AC 396 ms
6,120 KB
testcase_31 AC 396 ms
6,196 KB
testcase_32 AC 396 ms
6,164 KB
testcase_33 AC 399 ms
6,156 KB
testcase_34 AC 404 ms
6,192 KB
testcase_35 AC 403 ms
6,104 KB
testcase_36 AC 402 ms
6,212 KB
testcase_37 AC 401 ms
6,216 KB
testcase_38 AC 397 ms
6,200 KB
testcase_39 AC 398 ms
6,196 KB
testcase_40 AC 401 ms
6,148 KB
testcase_41 AC 400 ms
6,144 KB
testcase_42 AC 402 ms
6,212 KB
testcase_43 AC 401 ms
6,108 KB
testcase_44 AC 400 ms
6,268 KB
testcase_45 AC 393 ms
6,192 KB
testcase_46 AC 403 ms
6,172 KB
testcase_47 AC 402 ms
6,108 KB
testcase_48 AC 410 ms
6,148 KB
testcase_49 AC 414 ms
6,148 KB
testcase_50 AC 360 ms
6,272 KB
testcase_51 AC 367 ms
6,116 KB
testcase_52 AC 363 ms
6,196 KB
testcase_53 AC 391 ms
6,404 KB
testcase_54 AC 396 ms
6,152 KB
testcase_55 AC 384 ms
6,152 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