結果
| 問題 |
No.1365 [Cherry 1st Tune] Whose Fault?
|
| コンテスト | |
| ユーザー |
milanis48663220
|
| 提出日時 | 2021-01-23 00:48:56 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 592 ms / 2,000 ms |
| コード長 | 2,610 bytes |
| コンパイル時間 | 1,007 ms |
| コンパイル使用メモリ | 98,432 KB |
| 最終ジャッジ日時 | 2025-01-18 07:00:50 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 46 |
ソースコード
#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;
const long double inf = 1e9;
for(int i = 0; i < n; i++){
sum_ratio[i] = p[i] == 0 ? inf : (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] == 0 ? 0 : 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;
if(2*sr > inf) {
sv = 0.0;
}
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;
// if(2*sr > inf && uf.size(r) == n) ans = 0.0;
// cout << se << ' ' << sr << ' ' << sv << endl;
}
cout << ans << endl;
}
}
milanis48663220