結果

問題 No.2295 Union Path Query (Medium)
ユーザー SSRSSSRS
提出日時 2023-05-05 22:50:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,597 bytes
コンパイル時間 2,721 ms
コンパイル使用メモリ 169,280 KB
実行使用メモリ 6,520 KB
最終ジャッジ日時 2023-08-15 05:43:04
合計ジャッジ時間 27,109 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 418 ms
4,580 KB
testcase_06 AC 364 ms
5,128 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 RE -
testcase_15 AC 176 ms
6,148 KB
testcase_16 AC 175 ms
6,156 KB
testcase_17 WA -
testcase_18 RE -
testcase_19 WA -
testcase_20 AC 360 ms
6,444 KB
testcase_21 AC 386 ms
6,204 KB
testcase_22 WA -
testcase_23 AC 351 ms
6,192 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 354 ms
6,196 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 348 ms
6,172 KB
testcase_32 AC 347 ms
6,296 KB
testcase_33 WA -
testcase_34 AC 352 ms
6,288 KB
testcase_35 AC 358 ms
6,172 KB
testcase_36 AC 354 ms
6,168 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 358 ms
6,424 KB
testcase_40 AC 358 ms
6,136 KB
testcase_41 AC 361 ms
6,264 KB
testcase_42 AC 360 ms
6,204 KB
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 RE -
testcase_53 AC 347 ms
6,156 KB
testcase_54 WA -
testcase_55 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 = 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;
        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