保证数据为一棵随机树
CODE:
#include#include #include #include #include #include using namespace std;int a[10005],e[10005][2];int n = 1000;bool flag[10005];int main() { freopen("testdata.in","w",stdout); printf("%d\n",n); for(int i = 1 ; i <= n ; i++) { a[i] = rand() % n + 1; while(flag[a[i]]) a[i] = rand() % n + 1; flag[a[i]] = 1; } for(int i = 2 ; i <= n ; i++) { e[i-1][0] = a[i]; e[i-1][1] = a[rand() % (i - 1) + 1]; } memset(flag,0,sizeof(flag)); for(int i = 1 ; i < n ; i++) { int x = rand() % (n - 1) + 1; while(flag[x]) x = rand() % (n - 1) + 1; flag[x] = 1; int f = rand() % 2; if(f) printf("%d %d\n",e[x][0],e[x][1]); else printf("%d %d\n",e[x][1],e[x][0]); } return 0;}