Untitled

 avatar
unknown
plain_text
a year ago
3.1 kB
5
Indexable
#include<bits/stdc++.h>

#define ll long long
#define pp push_back
//#define endl '\n'
#define all(x) x.begin(),x.end()
#define ld long double
#define PI acos(-1)
#define sin(a) sin((a)*PI/180)
#define cos(a) cos((a)*PI/180)
#define ones(x) __builtin_popcountll(x)
//#define int ll

using namespace std;

void Drakon() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifdef Clion
    freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif
}

unsigned long long inf = 1e10;
const double EPS = 1e-6;
const int MOD = 1000000007, N = 200005, LOG = 25;

ll mul(const ll &a, const ll &b) {
    return (a % MOD + MOD) * (b % MOD + MOD) % MOD;
}

ll add(const ll &a, const ll &b) {
    return (a + b + 2 * MOD) % MOD;
}

ll pw(ll x, ll y) {
    ll ret = 1;
    while (y > 0) {
        if (y % 2 == 0) {
            x = mul(x, x);
            y = y / 2;
        } else {
            ret = mul(ret, x);
            y = y - 1;
        }
    }
    return ret;
}


int n, depth[N];
ll k, ans;
vector<int> adj[N];
map<pair<int, int>, ll> cost;

ll ask(int a, int b, int c, int d){
    cout << "? " << a + 1 << " " << b + 1 << " " << c + 1 << " " << d + 1 << endl;
    ll ret;
    cin >> ret;
    return ret;
}

void dfs(int u, int p1, int p2, ll edge) {

    ll ret = ask(p2, u, p2, p1) - 2 * edge;
    cost[{u, p1}] = cost[{p1, u}] = ret;

    for(auto v : adj[u]){
        if(v == p1)continue;
        depth[v] = depth[u] + 1;
        dfs(v, u, p1, ret);
    }
}

void solve() {
    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
        adj[i].clear();
        depth[i] = 0;
    }
    ans = 0;
    cost.clear();

    vector<pair<ll, pair<int, int>>> edges;
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
        edges.push_back({0, {u, v}});
    }

    int root = 0;
    for (int i = 0; i < n; ++i) {
        if(adj[i].size() > 1){
            root = i;
            break;
        }
    }

    ll r1 = ask(adj[root][0], adj[root][1], adj[root][0], root);
    ll r2 = ask(adj[root][0], adj[root][1], root, adj[root][1]);
    ll sum = (r1 + r2) / 3;
    cost[{root, adj[root][0]}] = cost[{adj[root][0], root}] = r1 - sum;
    cost[{root, adj[root][1]}] = cost[{adj[root][1], root}] = r2 - sum;
    for (int i = 2; i < adj[root].size(); ++i) {
        dfs(adj[root][i], root, adj[root][1], r2 - sum);
    }

    for(auto v : adj[adj[root][0]]){
        if(v == root)continue;
        dfs(v, adj[root][0], root, r1 - sum);
    }
    for(auto v : adj[adj[root][1]]){
        if(v == root)continue;
        dfs(v, adj[root][1], root, r2 - sum);
    }

    for (int i = 0; i < n - 1; ++i) {
        edges[i].first = cost[{edges[i].second.first, edges[i].second.second}];
    }
}

signed main() {
    Drakon();
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
}
Editor is loading...
Leave a Comment