結果

問題 No.1365 [Cherry 1st Tune] Whose Fault?
ユーザー milanis48663220milanis48663220
提出日時 2021-01-23 00:33:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 579 ms / 2,000 ms
コード長 2,433 bytes
コンパイル時間 1,417 ms
コンパイル使用メモリ 102,012 KB
実行使用メモリ 8,684 KB
最終ジャッジ日時 2023-08-28 11:59:03
合計ジャッジ時間 24,186 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 51 ms
4,376 KB
testcase_14 AC 35 ms
4,376 KB
testcase_15 AC 9 ms
4,380 KB
testcase_16 AC 6 ms
4,380 KB
testcase_17 AC 31 ms
4,380 KB
testcase_18 AC 16 ms
4,376 KB
testcase_19 AC 50 ms
4,380 KB
testcase_20 AC 42 ms
4,380 KB
testcase_21 AC 45 ms
4,376 KB
testcase_22 AC 39 ms
4,380 KB
testcase_23 AC 102 ms
5,716 KB
testcase_24 AC 226 ms
6,400 KB
testcase_25 AC 475 ms
6,772 KB
testcase_26 AC 321 ms
6,160 KB
testcase_27 AC 387 ms
4,788 KB
testcase_28 AC 195 ms
4,384 KB
testcase_29 AC 132 ms
4,584 KB
testcase_30 AC 579 ms
7,048 KB
testcase_31 AC 510 ms
8,148 KB
testcase_32 AC 274 ms
7,272 KB
testcase_33 AC 476 ms
5,684 KB
testcase_34 AC 369 ms
5,388 KB
testcase_35 AC 200 ms
6,452 KB
testcase_36 AC 418 ms
7,760 KB
testcase_37 AC 88 ms
4,500 KB
testcase_38 AC 106 ms
6,768 KB
testcase_39 AC 548 ms
6,400 KB
testcase_40 AC 544 ms
6,420 KB
testcase_41 AC 405 ms
6,976 KB
testcase_42 AC 398 ms
4,612 KB
testcase_43 AC 2 ms
4,384 KB
testcase_44 AC 221 ms
8,684 KB
testcase_45 AC 382 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

struct UnionFind {
    vector<int> data;
    UnionFind(int size) : data(size, -1) {}
    bool unionSet(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
        if (data[y] < data[x]) swap(x, y);
        data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    bool findSet(int x, int y) {
        return root(x) == root(y);
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(12) << fixed;
    int n; cin >> n;
    vector<int> a(n), b(n), p(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < n; i++) cin >> b[i];
    for(int i = 0; i < n; i++) cin >> p[i];
    UnionFind uf(n);
    vector<long double> sum_ratio(n), sum_val(n);
    vector<ll> sum_ene(n);
    long double ans = 0.0;
    for(int i = 0; i < n; i++){
        sum_ratio[i] = p[i] == 0 ? 1e18 : (long double)1.0/(long double)p[i];
        // sum_ratio[i] =  (long double)1.0/(long double)p[i];
        sum_ene[i] = (a[i]-b[i]);
        sum_val[i] = p[i]*(a[i]-b[i])*(a[i]-b[i]);
        ans += sum_val[i];
    }
    int q; cin >> q;
    for(int i = 0; i < q; i++){
        int x, y; cin >> x >> y; x--; y--;
        if(!uf.findSet(x, y)){
            int xr = uf.root(x), yr = uf.root(y);
            long double sr = sum_ratio[xr]+sum_ratio[yr];
            int se = sum_ene[xr]+sum_ene[yr];
            long double sv = (long double)(se*se)/sr;
            ans += sv-sum_val[xr]-sum_val[yr];
            uf.unionSet(x, y);
            int r = uf.root(x);
            sum_ene[r] = se;
            sum_val[r] = sv;
            sum_ratio[r] = sr;
            // cout << se << ' ' << sr << ' ' << sv << endl;
        }
        cout << ans << endl;
    }
}
0