結果

問題 No.1054 Union add query
ユーザー granddaifukugranddaifuku
提出日時 2020-05-23 23:38:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,104 bytes
コンパイル時間 1,673 ms
コンパイル使用メモリ 176,436 KB
実行使用メモリ 13,896 KB
最終ジャッジ日時 2024-04-17 18:54:37
合計ジャッジ時間 5,695 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
13,760 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < (int)n; ++i)
#define FOR(i, a, b) for(int i = a; i < (int)b; ++i)
#define rrep(i, n) for(int i = ((int)n - 1); i >= 0; --i)

using ll = long long;
using ld = long double;

const ll INF = 1e18;
const int Inf = 1e9;
const double EPS = 1e-9;
const int MOD = 1e9 + 7;

vector<vector<int> > g;
vector<int> sum;
vector<bool> visited;

void dfs(int s, int v) {
    if (visited[s]) return;
    sum[s] += v;
    visited[s] = true;
    rep (i, g[s].size()) {
        dfs(g[s][i], v);
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(0);
    int n, q;
    cin >> n >> q;
    g.resize(n);
    sum.resize(n);
    visited.resize(n);
    vector<bool> vi(n, false);
    rep (i, q) {
        int t, a, b;
        cin >> t >> a >> b;
        a--;
        if (t == 1) {
            b--;
            g[a].push_back(b);
            g[b].push_back(a);
        } else if (t == 2) {
            visited = vi;
            dfs(a, b);
        } else {
            cout << sum[a] << endl;
        }
    }
    
    return 0;
}

0