結果

問題 No.2295 Union Path Query (Medium)
ユーザー SSRSSSRS
提出日時 2023-05-05 22:48:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,603 bytes
コンパイル時間 2,198 ms
コンパイル使用メモリ 167,784 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2023-08-15 05:39:57
合計ジャッジ時間 12,888 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,760 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 430 ms
4,616 KB
testcase_06 AC 395 ms
4,808 KB
testcase_07 WA -
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:68:17: 警告: ‘ans’ may be used uninitialized [-Wmaybe-uninitialized]
   68 |         cout << ans << endl;
      |                 ^~~
main.cpp:60:13: 備考: ‘ans’ はここで定義されています
   60 |         int ans;
      |             ^~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
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(p[x], p[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;
        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