Problem: cf1129_e
Difficulty: medium
Categories: graph, search
Time limit: 3000 ms, memory limit: 256 MB

This is an interactive problem.

A legendary tree rests deep in the forest. Legend has it that individuals who realize this tree would eternally become a Legendary Grandmaster.

To help you determine the tree, Mikaela the Goddess has revealed to you that the tree contains $n$ vertices, enumerated from $1$ through $n$. She also allows you to ask her some questions as follows. For each question, you should tell Mikaela some two disjoint non-empty sets of vertices $S$ and $T$, along with any vertex $v$ that you like. Then, Mikaela will count and give you the number of pairs of vertices $(s, t)$ where $s \in S$ and $t \in T$ such that the simple path from $s$ to $t$ contains $v$.

Mikaela the Goddess is busy and will be available to answer at most $11\,111$ questions.

This is your only chance. Your task is to determine the tree and report its edges.

Input

The first line contains an integer $n$ ($2 \leq n \leq 500$) — the number of vertices in the tree.

Output

When program has realized the tree and is ready to report the edges, print "ANSWER" in a separate line. Make sure that all letters are capitalized.

Then, print $n-1$ lines, each containing two space-separated integers, denoting the vertices that are the endpoints of a certain edge. Each edge should be reported exactly once. Your program should then immediately terminate.

Interaction

For each question that you wish to ask, interact as follows.

1. First, print the size of $S$ in its own line. In the following line, print $|S|$ space-separated distinct integers, denoting the vertices in $S$.
2. Similarly, print the size of $T$ in its own line. In the following line, print $|T|$ space-separated distinct integers, denoting the vertices in $T$.
3. Then, in the final line, print $v$ — the vertex that you choose for this question.
4. Read Mikaela's answer from input.

Be reminded that $S$ and $T$ must be disjoint and non-empty.

After printing a query do not forget to output end of line and flush the output. Otherwise you will get Idleness limit exceeded. To do this, use:

* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.

If your program asks too many questions, asks an invalid question or does not correctly follow the interaction guideline above, it may receive an arbitrary verdict. Otherwise, your program will receive the Wrong Answer verdict if it reports an incorrect tree.

Note that the tree is fixed beforehand and does not depend on your queries.

Example

Input

```
5  
5
```

Output

```
3  
1 2 3  
2  
4 5  
2  
ANSWER  
1 2  
2 3  
3 4  
2 5
```

Note

In the sample, the tree is as follows.

 ![](https://espresso.codeforces.com/68451e8e2b6bbd2f7d62c9d31f0a5b85bec48a6f.png) 

$n = 5$ is given to the program. The program then asks Mikaela a question where $S = \{1, 2, 3\}$, $T = \{4, 5\}$, and $v = 2$, to which she replies with $5$ (the pairs $(s, t)$ are $(1, 4)$, $(1, 5)$, $(2, 4)$, $(2, 5)$, and $(3, 5)$).

Non-adaptive interactor (C++):
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

static long long queries = 0;
static long long query_limit = 0;

static void log_metrics() {
    try {
        tout << "queries=" << queries << '\n';
        tout << "query_limit=" << query_limit << '\n';
        tout.flush();
    } catch (...) {
        
    }
}

static void finish(TResult verdict, const string& msg) {
    log_metrics();
    quitf(verdict, "%s", msg.c_str());
}

static bool readToken(string &tok) {
    tok.clear();
    if (!(cin >> tok)) return false;
    return true;
}

static bool isAllDigits(const string& s) {
    if (s.empty()) return false;
    for (char c : s) if (c < '0' || c > '9') return false;
    return true;
}

static bool readIntStrict(long long &x) {
    string tok;
    if (!readToken(tok)) return false;
    if (!isAllDigits(tok)) return false;
    try {
        x = stoll(tok);
    } catch (...) {
        return false;
    }
    return true;
}

static vector<int> readVertexSet(int n, long long sz, const string& what, vector<char>& used, vector<int>* outVec) {
    if (sz < 1 || sz > n) finish(_pe, what + " size out of range");
    vector<int> v;
    v.reserve((size_t)sz);
    for (long long i = 0; i < sz; i++) {
        long long x;
        if (!readIntStrict(x)) finish(_pe, "Failed to read " + what + " element");
        if (x < 1 || x > n) finish(_pe, what + " element out of range");
        if (used[(int)x]) finish(_pe, what + " has duplicate element");
        used[(int)x] = 1;
        v.push_back((int)x);
    }
    if (outVec) *outVec = v;
    return v;
}

int main(int argc, char** argv) {
    registerInteraction(argc, argv);
    atexit(log_metrics);

    int n = inf.readInt(2, 500, "n");
    vector<pair<int,int>> hiddenEdges;
    hiddenEdges.reserve(max(0, n - 1));
    vector<vector<int>> adj(n + 1);
    set<pair<int,int>> hiddenSet;
    for (int i = 0; i < n - 1; i++) {
        int u = inf.readInt(1, n, "u");
        int v = inf.readInt(1, n, "v");
        if (u == v) finish(_fail, "Invalid hidden edge: self-loop");
        hiddenEdges.push_back({u, v});
        adj[u].push_back(v);
        adj[v].push_back(u);
        hiddenSet.insert({min(u, v), max(u, v)});
    }

    query_limit = 11111;
    log_metrics(); 

    
    cout << n << endl;

    
    vector<vector<int>> group(n + 1, vector<int>(n + 1, -1));
    for (int v = 1; v <= n; v++) {
        group[v][v] = 0;
        int id = 1;
        for (int nei : adj[v]) {
            if (group[v][nei] != -1) continue;
            deque<pair<int,int>> dq;
            dq.push_back({nei, v});
            group[v][nei] = id;
            while (!dq.empty()) {
                auto [x, p] = dq.front();
                dq.pop_front();
                for (int y : adj[x]) {
                    if (y == p) continue;
                    if (y == v) continue;
                    if (group[v][y] != -1) continue;
                    group[v][y] = id;
                    dq.push_back({y, x});
                }
            }
            id++;
        }
        for (int x = 1; x <= n; x++) {
            if (group[v][x] == -1) finish(_fail, "Internal error: disconnected tree/group computation failed");
        }
    }

    const long long hard_cap = max(200000LL, 10LL * query_limit);

    while (true) {
        string first;
        if (!readToken(first)) finish(_pe, "Unexpected EOF from contestant");

        if (first == "ANSWER") {
            set<pair<int,int>> got;
            got.clear();
            for (int i = 0; i < n - 1; i++) {
                long long a, b;
                if (!readIntStrict(a) || !readIntStrict(b)) finish(_pe, "Failed to read reported edge");
                if (a < 1 || a > n || b < 1 || b > n) finish(_pe, "Reported edge endpoint out of range");
                if (a == b) finish(_wa, "Reported self-loop");
                int u = (int)a, v = (int)b;
                got.insert({min(u, v), max(u, v)});
            }
            if ((int)got.size() != n - 1) finish(_wa, "Reported edges are not unique or count is wrong");
            if (got != hiddenSet) finish(_wa, "Reported tree is incorrect");
            finish(_ok, "Correct");
        }

        
        if (!isAllDigits(first)) finish(_pe, "Expected S size or ANSWER");
        long long szS;
        try { szS = stoll(first); } catch (...) { finish(_pe, "Invalid S size token"); }

        vector<char> usedS(n + 1, 0), usedT(n + 1, 0);
        vector<int> S, T;

        
        (void)readVertexSet(n, szS, "S", usedS, &S);

        
        long long szT;
        if (!readIntStrict(szT)) finish(_pe, "Failed to read T size");
        if (szT < 1 || szT > n) finish(_pe, "T size out of range");

        
        T.reserve((size_t)szT);
        for (long long i = 0; i < szT; i++) {
            long long x;
            if (!readIntStrict(x)) finish(_pe, "Failed to read T element");
            if (x < 1 || x > n) finish(_pe, "T element out of range");
            int xi = (int)x;
            if (usedT[xi]) finish(_pe, "T has duplicate element");
            if (usedS[xi]) finish(_pe, "S and T are not disjoint");
            usedT[xi] = 1;
            T.push_back(xi);
        }

        long long vv;
        if (!readIntStrict(vv)) finish(_pe, "Failed to read v");
        if (vv < 1 || vv > n) finish(_pe, "v out of range");
        int v = (int)vv;

        queries++;
        if (queries > hard_cap) finish(_pe, "Hard cap exceeded (likely infinite loop/output spam)");

        
        
        vector<int> cntS(n + 1, 0), cntT(n + 1, 0);
        for (int x : S) cntS[group[v][x]]++;
        for (int x : T) cntT[group[v][x]]++;

        long long total = (long long)S.size() * (long long)T.size();
        long long same = 0;
        for (int g = 0; g <= n; g++) same += 1LL * cntS[g] * cntT[g];
        long long ans = total - same;
        if (ans < 0) ans = 0;

        cout << ans << endl;
    }
}


Adaptive interactor (C++):
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

static long long queries = 0;
static long long query_limit = 0;

static void log_metrics() {
    try {
        tout << "queries=" << queries << "\n";
        tout << "query_limit=" << query_limit << "\n";
        tout.flush();
    } catch (...) {
        
    }
}

static void finish(TResult verdict, const string& msg) {
    log_metrics();
    quitf(verdict, "%s", msg.c_str());
}

static bool readToken(string& tok) {
    return (cin >> tok) ? true : false;
}

static bool parseNonNegLL(const string& s, long long& out) {
    if (s.empty()) return false;
    if (s.size() > 1 && s[0] == '0') {
        for (char c : s) if (!isdigit((unsigned char)c)) return false;
    } else {
        for (char c : s) if (!isdigit((unsigned char)c)) return false;
    }
    __int128 val = 0;
    for (char c : s) {
        val = val * 10 + (c - '0');
        if (val > (__int128)LLONG_MAX) return false;
    }
    out = (long long)val;
    return true;
}

static long long readIntStrict(const string& what, long long lo, long long hi, TResult onFailVerdict = _pe) {
    string tok;
    if (!readToken(tok)) finish(onFailVerdict, "unexpected EOF while reading " + what);
    long long x;
    if (!parseNonNegLL(tok, x)) finish(onFailVerdict, "invalid integer token for " + what);
    if (x < lo || x > hi) finish(onFailVerdict, what + " out of range");
    return x;
}

static vector<uint64_t> makeBits(int n) {
    return vector<uint64_t>((n + 63) / 64, 0ULL);
}
static inline void setBit(vector<uint64_t>& b, int x) {
    int idx = (x - 1) >> 6;
    int off = (x - 1) & 63;
    b[idx] |= (1ULL << off);
}
static inline bool testBit(const vector<uint64_t>& b, int x) {
    int idx = (x - 1) >> 6;
    int off = (x - 1) & 63;
    return (b[idx] >> off) & 1ULL;
}
static inline int popcountAnd(const vector<uint64_t>& a, const vector<uint64_t>& b) {
    int res = 0;
    for (size_t i = 0; i < a.size(); i++) res += __builtin_popcountll(a[i] & b[i]);
    return res;
}

struct ParsedTree {
    int n = 0;
    vector<pair<int,int>> edges; 
    vector<vector<int>> adj;
    vector<vector<char>> hasEdge; 
};

static ParsedTree readContestantTree(int n) {
    ParsedTree pt;
    pt.n = n;
    pt.adj.assign(n + 1, {});
    pt.hasEdge.assign(n + 1, vector<char>(n + 1, 0));
    pt.edges.clear();
    pt.edges.reserve(max(0, n - 1));

    for (int i = 0; i < n - 1; i++) {
        long long uLL = readIntStrict("edge_u", 1, n, _pe);
        long long vLL = readIntStrict("edge_v", 1, n, _pe);
        int u = (int)uLL, v = (int)vLL;
        if (u == v) finish(_wa, "self-loop in answer");
        int a = min(u, v), b = max(u, v);
        if (pt.hasEdge[a][b]) finish(_wa, "duplicate edge in answer");
        pt.hasEdge[a][b] = pt.hasEdge[b][a] = 1;
        pt.adj[u].push_back(v);
        pt.adj[v].push_back(u);
        pt.edges.push_back({a, b});
    }

    
    vector<int> q;
    q.reserve(n);
    vector<char> vis(n + 1, 0);
    q.push_back(1);
    vis[1] = 1;
    for (int qi = 0; qi < (int)q.size(); qi++) {
        int x = q[qi];
        for (int y : pt.adj[x]) if (!vis[y]) {
            vis[y] = 1;
            q.push_back(y);
        }
    }
    for (int i = 1; i <= n; i++) {
        if (!vis[i]) finish(_wa, "answer graph is not connected");
    }
    return pt;
}

static bool isStarCenteredAt(const ParsedTree& pt, int center) {
    int n = pt.n;
    if ((int)pt.edges.size() != n - 1) return false;
    if ((int)pt.adj[center].size() != n - 1) return false;
    for (int v = 1; v <= n; v++) {
        if (v == center) continue;
        if ((int)pt.adj[v].size() != 1) return false;
        if (pt.adj[v][0] != center) return false;
    }
    return true;
}

struct FixedAnswerEngine {
    int n = 0;
    vector<vector<int>> adj;
    
    vector<vector<int>> neigh;
    vector<vector<vector<uint64_t>>> compBits; 

    explicit FixedAnswerEngine(int n_, const vector<pair<int,int>>& edges) {
        n = n_;
        adj.assign(n + 1, {});
        for (auto [u, v] : edges) {
            adj[u].push_back(v);
            adj[v].push_back(u);
        }
        neigh.assign(n + 1, {});
        compBits.assign(n + 1, {});
        for (int v = 1; v <= n; v++) {
            neigh[v] = adj[v];
            compBits[v].resize(neigh[v].size());
            for (size_t i = 0; i < neigh[v].size(); i++) {
                int start = neigh[v][i];
                vector<char> vis(n + 1, 0);
                vis[v] = 1;
                vector<int> st;
                st.push_back(start);
                vis[start] = 1;
                vector<uint64_t> bits = makeBits(n);
                while (!st.empty()) {
                    int x = st.back();
                    st.pop_back();
                    setBit(bits, x);
                    for (int y : adj[x]) {
                        if (!vis[y]) {
                            vis[y] = 1;
                            st.push_back(y);
                        }
                    }
                }
                compBits[v][i] = std::move(bits);
            }
        }
    }

    long long answerQuery(const vector<uint64_t>& Sbits, const vector<uint64_t>& Tbits, int Ssz, int Tsz, int v) const {
        bool inS = testBit(Sbits, v);
        bool inT = testBit(Tbits, v);

        int totalS = Ssz - (inS ? 1 : 0);
        int totalT = Tsz - (inT ? 1 : 0);

        long long res = 0;
        if (inS) res += totalT;
        if (inT) res += totalS;

        if (totalS > 0 && totalT > 0) {
            long long sumSame = 0;
            for (size_t i = 0; i < neigh[v].size(); i++) {
                int sc = popcountAnd(Sbits, compBits[v][i]);
                int tc = popcountAnd(Tbits, compBits[v][i]);
                sumSame += 1LL * sc * tc;
            }
            res += 1LL * totalS * totalT - sumSame;
        }
        return res;
    }
};

int main(int argc, char** argv) {
    registerInteraction(argc, argv);
    atexit(log_metrics);

    int n = inf.readInt(2, 500, "n");

    bool hasEdges = !inf.seekEof();
    vector<pair<int,int>> hiddenEdges;
    hiddenEdges.reserve(max(0, n - 1));
    if (hasEdges) {
        for (int i = 0; i < n - 1; i++) {
            int u = inf.readInt(1, n, "u");
            int v = inf.readInt(1, n, "v");
            if (u == v) finish(_fail, "invalid hidden edge (self-loop)");
            hiddenEdges.push_back({u, v});
        }
    }

    query_limit = 11111;
    log_metrics(); 

    cout << n << endl;

    const long long hard_cap = max(200000LL, 10LL * query_limit);

    unique_ptr<FixedAnswerEngine> fixed;
    vector<pair<int,int>> hiddenNorm;
    vector<vector<char>> hiddenHas;
    if (hasEdges) {
        fixed.reset(new FixedAnswerEngine(n, hiddenEdges));
        hiddenHas.assign(n + 1, vector<char>(n + 1, 0));
        hiddenNorm.reserve(n - 1);
        for (auto [u, v] : hiddenEdges) {
            int a = min(u, v), b = max(u, v);
            hiddenHas[a][b] = hiddenHas[b][a] = 1;
            hiddenNorm.push_back({a, b});
        }
        sort(hiddenNorm.begin(), hiddenNorm.end());
    }

    
    vector<char> possibleCenter(n + 1, 1);
    int possibleCnt = n;

    auto maybePeriodicLog = [&]() {
        if ((queries & 511LL) == 0) log_metrics();
    };

    while (true) {
        string tok;
        if (!readToken(tok)) finish(_pe, "unexpected EOF (contestant terminated without ANSWER)");

        if (tok == "ANSWER") {
            ParsedTree pt = readContestantTree(n);

            if (hasEdges) {
                
                vector<pair<int,int>> got = pt.edges;
                sort(got.begin(), got.end());
                if (got != hiddenNorm) finish(_wa, "wrong tree reported");
                finish(_ok, "correct");
            } else {
                
                for (int c = 1; c <= n; c++) if (possibleCenter[c]) {
                    if (!isStarCenteredAt(pt, c)) finish(_wa, "answer not correct for all feasible instances");
                }
                finish(_ok, "correct");
            }
        }

        long long kS;
        if (!parseNonNegLL(tok, kS)) finish(_pe, "invalid first token (expected integer |S| or ANSWER)");
        if (kS < 1 || kS > n) finish(_pe, "|S| out of range");

        int Ssz = (int)kS;
        vector<uint64_t> Sbits = makeBits(n);
        vector<uint64_t> Tbits = makeBits(n);
        vector<char> used(n + 1, 0);

        for (int i = 0; i < Ssz; i++) {
            int x = (int)readIntStrict("S[i]", 1, n, _pe);
            if (used[x]) finish(_pe, "duplicate vertex in S");
            used[x] = 1;
            setBit(Sbits, x);
        }

        int Tsz = (int)readIntStrict("|T|", 1, n, _pe);
        for (int i = 0; i < Tsz; i++) {
            int x = (int)readIntStrict("T[i]", 1, n, _pe);
            if (used[x]) finish(_pe, "S and T must be disjoint and have distinct elements");
            used[x] = 1;
            setBit(Tbits, x);
        }

        int v = (int)readIntStrict("v", 1, n, _pe);

        queries++;
        maybePeriodicLog();
        if (queries > hard_cap) finish(_pe, "too many queries (hard cap exceeded)");

        long long reply = 0;
        if (hasEdges) {
            reply = fixed->answerQuery(Sbits, Tbits, Ssz, Tsz, v);
        } else {
            long long A_all = 1LL * Ssz * Tsz;
            bool inS = testBit(Sbits, v);
            bool inT = testBit(Tbits, v);
            long long A_non = (inS ? Tsz : 0) + (inT ? Ssz : 0);

            if (A_all == A_non) {
                reply = A_all;
                
            } else {
                bool vPossible = possibleCenter[v];
                bool hasOther = (possibleCnt - (vPossible ? 1 : 0)) > 0;

                
                if (vPossible && !hasOther) {
                    reply = A_all;
                    
                    for (int c = 1; c <= n; c++) possibleCenter[c] = (c == v);
                    possibleCnt = 1;
                } else {
                    reply = A_non;
                    
                    if (vPossible) {
                        possibleCenter[v] = 0;
                        possibleCnt--;
                        if (possibleCnt <= 0) finish(_fail, "internal: feasible set became empty");
                    }
                }
            }
        }

        cout << reply << endl;
    }
}


Test generator (C++):
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

static vector<pair<int,int>> build_path(int n) {
    vector<pair<int,int>> e;
    e.reserve(max(0, n - 1));
    for (int i = 1; i + 1 <= n; i++) e.push_back({i, i + 1});
    return e;
}

static vector<pair<int,int>> build_star(int n, int center = 1) {
    vector<pair<int,int>> e;
    e.reserve(max(0, n - 1));
    for (int i = 1; i <= n; i++) if (i != center) e.push_back({center, i});
    return e;
}

static vector<pair<int,int>> build_kary_heap(int n, int k) {
    k = max(1, k);
    vector<pair<int,int>> e;
    e.reserve(max(0, n - 1));
    for (int i = 2; i <= n; i++) {
        int parent = (i - 2) / k + 1;
        e.push_back({parent, i});
    }
    return e;
}

static vector<pair<int,int>> build_double_star(int n) {
    vector<pair<int,int>> e;
    e.reserve(max(0, n - 1));
    e.push_back({1, 2});
    for (int i = 3; i <= n; i++) e.push_back({(i & 1) ? 1 : 2, i});
    return e;
}

static vector<pair<int,int>> build_broom(int n, int handle_len) {
    handle_len = max(2, min(handle_len, n));
    vector<pair<int,int>> e;
    e.reserve(max(0, n - 1));
    for (int i = 1; i + 1 <= handle_len; i++) e.push_back({i, i + 1});
    for (int i = handle_len + 1; i <= n; i++) e.push_back({handle_len, i});
    return e;
}

static vector<pair<int,int>> build_caterpillar(int n, int spine_len) {
    spine_len = max(2, min(spine_len, n));
    vector<pair<int,int>> e;
    e.reserve(max(0, n - 1));
    for (int i = 1; i + 1 <= spine_len; i++) e.push_back({i, i + 1});

    int next_node = spine_len + 1;
    if (next_node > n) return e;

    if (spine_len == 2) {
        for (int v = next_node; v <= n; v++) e.push_back({(v & 1) ? 1 : 2, v});
        return e;
    }

    int attach_from = 2, attach_to = spine_len - 1;
    int idx = 0;
    for (int v = next_node; v <= n; v++) {
        int spine = attach_from + (idx % (attach_to - attach_from + 1));
        e.push_back({spine, v});
        idx++;
    }
    return e;
}

static vector<pair<int,int>> build_prufer_random(int n) {
    if (n == 2) return {{1, 2}};
    vector<int> prufer(n - 2);
    for (int i = 0; i < n - 2; i++) prufer[i] = rnd.next(1, n);

    vector<int> deg(n + 1, 1);
    for (int x : prufer) deg[x]++;

    set<int> leaves;
    for (int i = 1; i <= n; i++) if (deg[i] == 1) leaves.insert(i);

    vector<pair<int,int>> e;
    e.reserve(n - 1);
    for (int x : prufer) {
        int leaf = *leaves.begin();
        leaves.erase(leaves.begin());
        e.push_back({leaf, x});
        deg[leaf]--;
        deg[x]--;
        if (deg[x] == 1) leaves.insert(x);
    }

    int a = *leaves.begin();
    leaves.erase(leaves.begin());
    int b = *leaves.begin();
    e.push_back({a, b});
    return e;
}

static void shuffle_edges(vector<pair<int,int>>& edges) {
    for (int i = (int)edges.size() - 1; i >= 1; i--) {
        int j = rnd.next(0, i);
        swap(edges[i], edges[j]);
    }
}

static void apply_permutation(vector<pair<int,int>>& edges, int n) {
    vector<int> p(n + 1);
    for (int i = 1; i <= n; i++) p[i] = i;
    for (int i = n; i >= 2; i--) {
        int j = rnd.next(1, i);
        swap(p[i], p[j]);
    }
    for (auto &ed : edges) {
        ed.first = p[ed.first];
        ed.second = p[ed.second];
        if (ed.first == ed.second) ed.second = (ed.second % n) + 1;
    }
}

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

    string mode = opt<string>("mode", "non");
    int nOpt = opt<int>("n", 500);
    int typeOpt = opt<int>("type", -1);
    int kOpt = opt<int>("k", 3);
    int permOpt = opt<int>("perm", 1);

    long long seedValue = 0;
    if (argc >= 2) seedValue = atoll(argv[1]);

    int n = max(2, min(500, nOpt));

    if (mode == "adp") {
        if (seedValue == 1) n = 2;
        else if (seedValue == 2) n = 500;
        else if (seedValue == 3) n = 499;
        else n = max(2, min(500, n));
        cout << n << "\n";
        return 0;
    }

    if (mode != "non") quitf(_fail, "Unknown --mode: %s", mode.c_str());

    vector<pair<int,int>> edges;

    if (seedValue == 1) {
        n = 2;
        edges = {{1, 2}};
    } else if (seedValue == 2) {
        n = 500;
        edges = build_path(n);
    } else if (seedValue == 3) {
        n = 500;
        edges = build_kary_heap(n, 2);
    } else {
        int type = (typeOpt != -1 ? typeOpt : (int)(seedValue % 8));
        type = ((type % 8) + 8) % 8;

        if (type == 0) edges = build_kary_heap(n, 2);                         
        else if (type == 1) edges = build_prufer_random(n);                   
        else if (type == 2) edges = build_broom(n, max(2, (3 * n) / 4));      
        else if (type == 3) edges = build_double_star(n);                     
        else if (type == 4) edges = build_caterpillar(n, max(3, n / 5));      
        else if (type == 5) edges = build_kary_heap(n, max(2, min(8, kOpt))); 
        else if (type == 6) edges = build_path(n);                            
        else edges = build_star(n, 1);                                        

        if (permOpt) apply_permutation(edges, n);
        shuffle_edges(edges);
    }

    if ((int)edges.size() != n - 1) quitf(_fail, "Generated edges != n-1");

    cout << n << "\n";
    for (auto &e : edges) {
        int u = e.first, v = e.second;
        if (u < 1 || u > n || v < 1 || v > n || u == v) quitf(_fail, "Invalid edge");
        cout << u << " " << v << "\n";
    }
    return 0;
}