我goole tree的圖片拿來當測資,並沒有發現問題,一直無法ac
我goole tree的圖片拿來當測資,並沒有發現問題,一直無法ac
更:犯蠢 記得由小印到大
#include#includeusing namespace std;
const int maxn = 80;
typedef struct node{vector child;
}node;
node Node[maxn];
void dfs(int i,vector &path){if(Node[i].child.size() == 0){if (path.size() ==1 ){printf("%d:N\n",i);}else{printf("%d:{",i);for (int y = path.size()-1; y > 1; y--){printf("%d,",path[y]);}printf("%d}\n",path[1]);}return;}path.push_back(i);int x;for(int x = 0; x < Node[i].child.size(); x++){dfs(Node[i].child[x],path);}path.pop_back();}
void clearchild(void){for (size_t i = 0; i < maxn; i++){Node[i].child.clear();}}
int main(){int d;while (scanf("%d",&d) != EOF){while(d--){clearchild();int edges;int a, b,root;vector path;clearchild();scanf("%d",&edges);for (size_t i = 0; i < edges; i++){scanf("%d,%d",&a,&b);if(b == 99){root = a;}else{Node[b].child.push_back(a);}}if (edges == 1){printf("%d:N\n",a);}else{dfs(root,path);puts("");}}}return 0;}