Tree 生成器

每次对拍写树总是很麻烦,这里直接弄了个生成器,随机效果一般,但也能凑合用吧(大概

const int maxn = 25;
int e[maxn][maxn];
void make_Tree(int n){
    memset(e, 0, sizeof e);
    vector<int> vec1;
    vector<int> vec2;
    for (int i = 2; i <= n; i++) {
        vec1.push_back(i);
    }
    vec2.push_back(1);

    for (int _ = 1; _ < n; _++) {
    
        int x = rand() % vec1.size(), y = rand() % vec2.size();
        int u = vec1[x], v = vec2[y];
    
        vec2.push_back(u);
        vec1.erase(vec1.begin() + x);
        cout << u << " " << v << endl;
        e[u][v] = e[v][u] = 1;
    }
}

 

posted @ 2023-01-16 21:55  浪释归衣  阅读(60)  评论(0)    收藏  举报