跳到正文

个人记录

Data Maker

Argon_Cube约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年2月9日
篇幅
约 7 分钟

版本

  1. v1当前
C++
#include <windows.h>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <string>
#include <random>
#include <limits>
#include <ctime>
#include <cmath>
#include <array>

#ifndef Data_Maker
    #define Data_Maker

namespace Data
{
    using std::string;
    using std::vector;
    using std::pair;
    using std::swap;

	std::default_random_engine  rdeng(time(nullptr));
    const string                preffolder="Data\\";
    std::ofstream               creator,configer;
    string                      filename;

    void call_std(int tcase,const string& dataname=preffolder+filename,std::ofstream& outer=creator)
    {
        if(outer.is_open())
            outer.close();
        system(("Standard.exe<"+dataname+std::to_string(tcase)+".in>"+dataname+std::to_string(tcase)+".out").c_str());
    }
    void data_open(int tcase,const string& dataname=preffolder+filename,std::ofstream& outer=creator)
    {
        if(outer.is_open())
            outer.close();
        outer.open(dataname+std::to_string(tcase)+".in");
    }
    void set_config(int testcase,int subtask,int score=-1,int timelim=-1,int memorylim=-1)
    {
        if(!configer.is_open())
            configer.open(preffolder+"config.yml");
        configer<<filename<<testcase<<".in: \n";
        if(~subtask)
            configer<<"    subtaskId: "<<subtask<<'\n';
        if(~score)
            configer<<"    score: "<<score<<'\n';
        if(~timelim)
            configer<<"    timeLimit: "<<timelim<<'\n';
        if(~memorylim)
            configer<<"    memoryLimit: "<<memorylim<<'\n';
        configer<<'\n';
    }

    template<typename type>
    type rand(type a,type b)
    {
        return std::uniform_int_distribution<type>(a,b)(rdeng);
    }
    template<typename type>
    type rand_f(type a,type b)
    {
        return std::uniform_real_distribution<type>(a,b)(rdeng);
    }
    double round(double x,int cnt)
    {
        double tmp=1;
        while(cnt--)
            tmp*=10;
        return std::ceil(x*tmp+0.5)/tmp;
    }

    vector<int> prufer_to_tree(vector<int> prufer)
    {
        int cnt=prufer.size()+2;
        vector<int> result(cnt),degs(cnt+1);
        for(const int i:prufer)
            ++degs[i];
        prufer.push_back(cnt);
        for(int i=0,ptr=1;i<cnt-1;i++,ptr++)
        {
            while(degs[ptr])
                ++ptr;
            result[ptr]=prufer[i];
            while(i<cnt-1&&!--degs[prufer[i]]&&prufer[i]<ptr)
                result[prufer[i]]=prufer[i+1],++i;
        }
        return result;
    }
    vector<int> __rand_tree(int cnt)
    {
        if(cnt<=1)
            return vector<int>();
        vector<int> prufer(cnt-2);
        for(int& i:prufer)
            i=rand(1,cnt);
        return prufer_to_tree(prufer);
    }
    vector<pair<int,int>> rand_tree(int cnt)
    {
        if(cnt<=1)
            return vector<pair<int,int>>();
        vector<pair<int,int>> result;
        vector<int> tree(__rand_tree(cnt));
        for(int i=1;i<tree.size();i++)
            result.emplace_back(i,tree[i]);
        for(auto& i:result)
            if(rand(0,1))
                swap(i.first,i.second);
        std::shuffle(result.begin(),result.end(),rdeng);
        return result;
    }
}

#endif
原文