首页 > 代码库 > UESTC 2016 Summer Training #1 J - Objects Panel (A) 按条件遍历树

UESTC 2016 Summer Training #1 J - Objects Panel (A) 按条件遍历树

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
typedef long long LL;

char s[108];
vector<int> num[108];

void dfs(int u, int k)
{
    int d = num[u].size(), v;
    for(int i = 0; i < d; i++){
        v = num[u][i];
        for(int j = 0; j < k; j++) printf("  ");
        if(num[v].size()){
            if(s[v] == -){printf("- object%d\n", v); dfs(v, k+1);}
            else printf("+ object%d\n", v);
        }
        else printf("  object%d\n", v);
        /*
        if(s[v] == ‘-‘){
            if(num[v].size()) {printf("- object%d\n", v); dfs(v, k+1);}
            else printf("  object%d\n", v);
        }
        else if(s[v] == ‘+‘) printf("+ object%d\n", v);
            */

    }
}

int main()
{
    #ifdef LOCAL
    freopen("a.txt", "r", stdin);
    //freopen("b.txt", "w", stdout);
    int T = 1;
    while(T--){
    #endif // LOCAL
    int n, k, val;
    scanf("%d", &n);
    for(int i = 0; i <= n; i++){
        getchar();
        scanf("%c", &s[i]);
        scanf("%d", &k);
        while(k--){
            scanf("%d", &val);
            num[i].push_back(val);
        }
    }

    if(n == 0) {printf("  project\n");}
    else{
        if(s[0] == -){
            printf("- project\n");
            dfs(0, 1);
        }
        else printf("+ project\n");
    }




    #ifdef LOCAL
    printf("\n");
    }
    #endif // LOCAL
    return 0;
}

 

UESTC 2016 Summer Training #1 J - Objects Panel (A) 按条件遍历树