- 生成树结构 (O(n))
#include<cstdio> #include<ctime> #include<algorithm> #include<cstdlib> #include<iostream> using namespace std; const int N = 10000005;//数据范围 int T,l,r,b;//数据组数 点数在[l,r]中 是否边有权值 int n,w; int a[N]; int GetRand(){ return (rand()%w+w)%w; } int main(){ ios::sync_with_stdio(false); srand(time(0)); cin>>T>>l>>r>>b;w=r-l+1; while(T--){ n=GetRand()+l; cout<<n<<" "<<n-1<<endl; for(int i=1;i<=n;i++) a[i]=i; random_shuffle(a+1,a+n+1); for(int i=2;i<=n;i++){ int u=rand()%(i-1)+1; int v=rand()%(n-i+1)+i; int w=rand(); cout<<a[u]<<" "<<a[v]<<" "; swap(a[v],a[i]); if(b) cout<<w; cout<<endl; } } return 0; }
Update:20161207,修改了一下,变得方便了一下...maybe...
#include <cstdio> #include <utility> #include <ctime> #include <cstdlib> #include <vector> #include <algorithm> #include <iostream> using namespace std; #define debug(a) cout<<#a<<"="<<a<<endl #define mpr make_pair typedef long long LL; typedef pair< LL,LL > pr; /* */ LL GetRand(LL x){ return ((rand()*rand())%x+x)%x; } struct Tree{ LL n,num,b,vl,vr,vw; vector< int > a; void make() { cout<<n<<endl; random_shuffle(a.begin(),a.end()); for(int i=1;i<n;i++) { int x=GetRand(i); cout<<a[x]<<" "<<a[i]; if(b) cout<<" "<<vl+GetRand(vw); cout<<endl; } } void init(int _n,int _num,bool _v,pr _va) { n=_n, num=_num, b=_v, vl=_va.first, vr=_va.second, vw=vr-vl+1; a.clear(); for(int i=0;i<n;i++) a.push_back(i+num); } }_tree; //const of Tree void Tree_init() { //节点数 int _n = 1000; //编号起始数 int _num = 1; //是否有权值 bool _value = false; //权值区间 LL _value_l = 1; LL _value_r = 1000; _tree.init(_n,_num,_value,mpr(_value_l,_value_r)); } int main(){ //初始化 ios::sync_with_stdio(false); srand(time(0)); //数据处理 int T = 1;//数据组数 int Tb = false;//是否输出数据组数 Tree_init(); if(Tb) cout<<T<<endl; for(int i=1;i<=T;i++) { _tree.make(); } return 0; }