結果
| 問題 |
No.1054 Union add query
|
| コンテスト | |
| ユーザー |
akun0716
|
| 提出日時 | 2020-09-30 19:50:39 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,180 bytes |
| コンパイル時間 | 910 ms |
| コンパイル使用メモリ | 89,800 KB |
| 実行使用メモリ | 46,520 KB |
| 最終ジャッジ日時 | 2024-07-06 08:14:18 |
| 合計ジャッジ時間 | 5,480 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | WA * 3 |
| other | WA * 4 RE * 4 |
ソースコード
#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12
//priority_queue<int,vector<int>, greater<int> > q2;
struct UnionFind {
VI par, s;
UnionFind(int N) :par(N), s(N) {
REP(i, N)par[i] = i;
REP(i, N)s[i] = 1;
}
int root(int x) {
if (par[x] == x)return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry)return;
if (!(same(x, y)))s[ry] += s[rx];
par[rx] = ry;
}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
int size(int x) {
return s[root(x)];
}
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, Q; cin >> N >> Q;
int T, A, B;
UnionFind uni(N);
VI po(N, 0), ans(N, 0);
vector<VI>pa(N);
REP(i, N)pa[i].push_back(i);
while (Q--) {
scanf("%lld%lld%lld", &T, &A, &B);
A--;
if (T == 1) {
B--;
pa[A].push_back(B);
ans[A] -= po[uni.root(B)];
uni.unite(A, B);
}
else if (T == 2) {
po[uni.root(A)] += B;
}
else if (T == 3) {
int cnt = 0;
fore(i, pa[A])cnt += po[i];
//if (A == 1)cout << ans[A] << " " << cnt << endl;
cout << ans[A] + cnt << '\n';
}
}
return 0;
}
akun0716