結果
| 問題 |
No.1637 Easy Tree Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-10-21 14:01:00 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 2,400 bytes |
| コンパイル時間 | 1,844 ms |
| コンパイル使用メモリ | 171,472 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-07-01 00:00:07 |
| 合計ジャッジ時間 | 10,180 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | WA * 1 RE * 1 |
| other | WA * 2 RE * 31 |
ソースコード
#include <bits/stdc++.h>
#if 1 && defined(LOCAL)
#include <debug.hpp>
#else
#define debug(...)
#define line
#endif
using namespace std;
using ll = long long;
using ld = long double;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<typename T> inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false)); }
template<typename T> inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); }
struct UnionFind {
vector<int> par, siz;
//初期化
UnionFind(int N) : par(N, -1), siz(N, 1){};
int N = par.size();
//根を求める
int root(int x) {
if (par[x] == -1)
return x;
else
return par[x] = root(par[x]);
}
// xとyが同じグループに属するか(根が一致するか)
bool issame(int x, int y) { return root(x) == root(y); }
// xを含むグループとyを含むグループを併合
bool unite(int x, int y) {
// x,yを根まで移動
x = root(x);
y = root(y);
//同じだったら何もない
if (x == y) return false;
// union by size(y側のサイズを小さくする)
if (siz[x] > siz[y]) swap(x, y);
// yをxの子とする
par[y] = x;
siz[x] += siz[y];
return true;
}
// xを含むグループのサイズ
int size(int x) { return siz[root(x)]; }
//どのようなグループ構成か返す
vector<vector<int>> groups() {
vector<int> unite_buf(N), group_siz(N);
for (int i = 0; i < N; i++) {
unite_buf[i] = root(i);
group_siz[unite_buf[i]]++;
}
vector<vector<int>> res(N);
for (int i = 0; i < N; i++) {
res[i].reserve(group_siz[i]);
}
for (int i = 0; i < N; i++) {
res[unite_buf[i]].push_back(i);
}
res.erase(remove_if(res.begin(), res.end(),
[&](const vector<int>& v) { return v.empty(); }),
res.end());
return res;
}
};
int main(){
int N,Q;
cin>>N>>Q;
UnionFind uf(N);
for(int i=0;i<N;i++){
int a,b;
cin>>a>>b;
a--;b--;
uf.unite(a,b);
}
ll pre=0;
for(int i=0;i<Q;i++){
int x,y;
cin>>x>>y;
x--;
pre+=(ll)uf.size(x)*y;
cout<<pre<<endl;
}
}