QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#691458#2685. Driving in OptimistankevinyangWA 0ms3940kbC++173.9kb2024-10-31 11:31:432024-10-31 11:31:44

Judging History

This is the latest submission verdict.

  • [2024-10-31 11:31:44]
  • Judged
  • Verdict: WA
  • Time: 0ms
  • Memory: 3940kb
  • [2024-10-31 11:31:43]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;


int f(int l, int r, int k){
    if(l > r){
        swap(l,r);
    }
    return k*l*r + (r-l)*k*(k-1)/2 - (k-1)*k*(2*k-1)/6;
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int n;
    cin >> n;
    vector<vector<int>>d(n+1,vector<int>(n+1));
    for(int i = 1; i<=n; i++){
        for(int j = i+1; j<=n; j++){
            cin >> d[i][j];
            d[j][i] = d[i][j];
        }
    }
    vector<vector<int>>dep(n+1,vector<int>(n+1)); // dep[i][j] is the depth of the lca(i,j)
    for(int i = 2; i<=n; i++){
        for(int j = 2; j<=n; j++){
            if(i==j)continue;
            if((d[1][i] + d[1][j] - d[i][j])%2 !=0){
                cout << "impossible\n";
                return 0;
            }
            dep[i][j] = (d[1][i] + d[1][j] - d[i][j])/2;
        }
    }
    vector<vector<pii>>adj(2*n+5);
    int label = n+1;
    int tot = 0;
    auto solve = [&](auto self, vector<int>nodes, int parent, int w) -> void {
        int mn = 1e9;
        int m = nodes.size();
        if(nodes.size() == 0)return;
        if(nodes.size() == 1){
            int u = nodes[0];
            int depth = d[1][u];
            adj[parent].push_back({depth - w, u});
            adj[u].push_back({depth - w,parent});
            tot += depth-w;
            return;
        }
        int cur = label++;
        
        vector<vector<int>>buckets(m);
        for(int i = 0; i<nodes.size(); i++){
            for(int j = i+1; j<nodes.size(); j++){
                mn = min(mn,dep[nodes[i]][nodes[j]]);
            }
        }
        tot += mn-w;
        adj[parent].push_back({mn-w,cur});
        adj[cur].push_back({mn-w,parent});
        for(int u : nodes){
            //cout << "nodes " << u << '\n';
            for(int i = 0; i<m; i++){
                if(buckets[i].size() == 0){
                    buckets[i].push_back(u);
                    break;
                }
                if(dep[buckets[i][0]][u] != mn){
                    buckets[i].push_back(u);
                    break;
                }
            }
        }
        for(auto vec: buckets){
            self(self, vec, cur, mn);
        }
    };
    vector<int>vec;
    for(int i = 2; i<=n; i++){
        vec.push_back(i);
    }
    solve(solve,vec,1,0);
    // for(int i = 1; i<=2*n; i++){
    //     for(auto [w,y] : adj[i]){
    //         if(i<y)cout << i << ' ' << y << ' ' << w << '\n';
    //     }
    // }
    for(int i = 1; i<=n; i++){
        vector<int>dis(2*n+1);
        vector<bool>vis(2*n+1);
        vis[i] = true;
        queue<int>q;
        q.push(i);
        while(q.size()){
            int cur = q.front(); q.pop();
            for(auto [w,nxt]: adj[cur]){
                if(vis[nxt])continue;
                vis[nxt] = true;
                dis[nxt] = dis[cur] + w;
                q.push(nxt);
            }
        }
        for(int j = 1; j<=n; j++){
            if(d[i][j] != dis[j]){
                cout << "impossible\n";
                return 0;
            }
        }
    }
    double ans = 0;
    vector<int>dp(2*n+1);
    auto dfs = [&](auto self, int u, int p, int w) -> void {
        for(auto [wt, nxt] : adj[u]){
            if(nxt==p)continue;
            self(self,nxt,u,wt);
            dp[u]+=dp[nxt];
            dp[u]+=wt;
        }
        //cout << dp[u]+1 << ' ' << tot-w-dp[u]+1 << ' ' << w << '\n';
        if(w > 0){
            ans += f(dp[u]+1,tot-dp[u],w);
        }
    }; 
    dfs(dfs,1,0,0);
    ans /= (tot) * (tot+1)/2;
    cout << fixed << setprecision(15);
    cout << ans << '\n';
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3940kb

input:

4
4 4 4
2 4
4

output:

2.678571428571429

result:

wrong answer