//designed by Thomas wu #include <iostream> using namespace std; int n=7; int **d; int *v; int getDeep(int); int main(){ d=new int*[n]; d[0]=new int[1]{0}; d[1]=new int[3]{2,6,7}; d[2]=new int[3]{2,1,4}; d[3]=new int[1]{0}; d[4]=new int[3]{2,3,2}; d[5]=new int[1]{0}; d[6]=new int[1]{0}; /* d[0]=new int[2]{1,6}; d[1]=new int[4]{3,5,3,8}; d[2]=new int[1]{0}; d[3]=new int[3]{2,1,7}; d[4]=new int[2]{1,9}; d[5]=new int[1]{0}; d[6]=new int[2]{1,2}; d[7]=new int[1]{0}; d[8]=new int[1]{0}; */ v=new int[n]{0}; for (int i=0;i<n;i++){ if(d[i][0]!=0){ for (int j=1;j<=d[i][0];j++){ v[d[i][j]-1]=i+1; } } } printf("父子關係\n"); for (int i=0;i<n;i++){ printf("%d ", v[i]); } printf("\n"); for (int i=0;i<n;i++){ if(v[i]==0){ printf("根節點為 %d ", i+1); break; } } printf("\n"); int *deep=new int[7]; for (int i=0;i<n;i++){ deep[i]=getDeep(i+1); printf("%d ", deep[i]); } printf("\n"); int total=0; for (int i=0;i<n;i++){ total+=deep[i]; } printf("深度總和 : %d\n", total); } int getDeep(int x){ int max=0; int tmp=0; for (int i=0;i<n;i++){ if(v[i]==x){ tmp=getDeep(i+1)+1; if(tmp>max)max=tmp; } } return max; }