File size: 3,809 Bytes
5fed0fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

static const int N = 400;
static const int K = 5;
static const int M = (N - 1) * K;

struct DSU {
    vector<int> p, sz;
    DSU(int n = 0) { init(n); }
    void init(int n) {
        p.resize(n);
        sz.assign(n, 1);
        iota(p.begin(), p.end(), 0);
    }
    int find(int x) {
        if (p[x] == x) return x;
        return p[x] = find(p[x]);
    }
    bool unite(int a, int b) {
        a = find(a); b = find(b);
        if (a == b) return false;
        if (sz[a] < sz[b]) swap(a, b);
        p[b] = a;
        sz[a] += sz[b];
        return true;
    }
    int size(int x) {
        x = find(x);
        return sz[x];
    }
    bool same(int a, int b) { return find(a) == find(b); }
};

static inline string trim_str(const string &s) {
    size_t l = 0, r = s.size();
    while (l < r && isspace((unsigned char)s[l])) l++;
    while (r > l && isspace((unsigned char)s[r - 1])) r--;
    return s.substr(l, r - l);
}

int main(int argc, char* argv[]) {
    registerInteraction(argc, argv);

    vector<pair<int,int>> ps(N);
    vector<pair<int,int>> es(M);
    vector<long long> cs(M);

    // Read points
    for (int i = 0; i < N; i++) {
        int x = inf.readInt();
        int y = inf.readInt();
        ps[i] = {x, y};
    }
    // Read edges
    for (int i = 0; i < M; i++) {
        int u = inf.readInt();
        int v = inf.readInt();
        es[i] = {u, v};
    }
    // Read costs
    for (int i = 0; i < M; i++) {
        long long c = inf.readLong();
        cs[i] = c;
    }

    // Send initial data to contestant
    for (int i = 0; i < N; i++) {
        cout << ps[i].first << " " << ps[i].second << endl;
    }
    for (int i = 0; i < M; i++) {
        cout << es[i].first << " " << es[i].second << endl;
    }
    cout.flush();

    DSU uf(N);
    long long chosen_cost = 0;

    // Interactive loop
    for (int i = 0; i < M; i++) {
        // Send current edge cost
        cout << cs[i] << endl;
        cout.flush();

        // Read non-empty line, skip blanks and lines starting with '#'
        string line;
        do {
            line = ouf.readLine();
            line = trim_str(line);
        } while (line.empty() || line[0] == '#');

        if (!(line == "0" || line == "1")) {
            quitf(_wa, "illegal output (%s)", line.c_str());
        }
        int take = (line[0] == '1') ? 1 : 0;
        if (take == 1) {
            uf.unite(es[i].first, es[i].second);
            chosen_cost += cs[i];
        }
    }

    // Check connectivity
    if (uf.size(0) != N) {
        quitf(_wa, "not connected");
    }

    // Compute MST on all edges with given costs
    vector<int> idx(M);
    iota(idx.begin(), idx.end(), 0);
    sort(idx.begin(), idx.end(), [&](int a, int b) {
        if (cs[a] != cs[b]) return cs[a] < cs[b];
        if (es[a].first != es[b].first) return es[a].first < es[b].first;
        return es[a].second < es[b].second;
    });
    DSU mst_dsu(N);
    long long mst_cost = 0;
    for (int id : idx) {
        if (mst_dsu.unite(es[id].first, es[id].second)) {
            mst_cost += cs[id];
        }
    }

    if (chosen_cost <= 0) {
        quitf(_wa, "total chosen cost is non-positive");
    }

    long long score = llround(1e8L * (long double) mst_cost / (long double) chosen_cost);
    long long baseline_value = ans.readLong();
    long long best_value = ans.readLong();

    double score_ratio = max(0.0, min(1.0, (double)(score - baseline_value) / (best_value - baseline_value)));
    double unbounded_ratio = max(0.0, (double)(score - baseline_value) / (best_value - baseline_value));
    quitp(score_ratio, "Value: %lld. Ratio: %.4f, RatioUnbounded: %.4f", score, score_ratio, unbounded_ratio);
    // quitf(_wa, "score (%lld)", score);
    return 0;
}