bool validTree(int n, vector<pair<int, int>>& edges) { int sz = edges.size(); if(sz != n-1) return false; vector<int> inC(n, 0); iota(inC.begin(), inC.end(), 0); for(int i = 0 ; i < sz; i ++){ int ac = findU(edges[i].first, inC), bc = findU(edges[i].second, inC); if(ac == bc) return false; inC[ac] = bc; } return true; }
int run(struct bf_machine *machine) { long mem_size = MEM_SIZE; for(;machine->times > 0; machine->times--){ switch(*machine->b & 0x70){ // 01110000b case 0x00: // , #ifdef VERBOSE printf(","); #endif inC(machine->mem_p); break; case 0x10: // + #ifdef VERBOSE printf("+"); #endif if(machine->times > 1){ *machine->mem_p += machine->times; machine->times = 0; } else *machine->mem_p++; break; case 0x20: // > #ifdef VERBOSE printf(">"); #endif if(*machine->mem_p >= mem_size){ printf("Fatal programer stupid!\nStop! traing to acess mem after end of BF machine\n"); exit(1); } machine->mem_p++; break; case 0x30: // [ #ifdef VERBOSE printf("["); #endif if(*machine->mem_p){ machine->stack_p++; *machine->stack_p = machine->b; } else while(*machine->b != ']') machine->b++; break; case 0x40: // . #ifdef VERBOSE printf("."); #endif outC(machine->mem_p); break; case 0x50: // - #ifdef VERBOSE printf("-"); #endif if(machine->times > 1){ *machine->mem_p -= machine->times; machine->times = 0; } else *machine->mem_p--; break; case 0x60: // < #ifdef VERBOSE printf("<"); #endif if(machine->mem_p < machine->mem){ printf("Fatal programer stupid!\nStop! traing to acess mem before star of BF machine\n"); exit(1); } machine->mem_p--; break; case 0x70: // ] #ifdef VERBOSE printf("]"); #endif machine->b = *machine->stack_p--; break; } } }
int main(int argc, char *argv[]) { MACRO_VARS // :. int times,f_name_i = 1; byte b,nible,*loaded_file,stack_i; struct space *f; uint stack[256]; /* BF VM vars */ byte *mem; uint pos = 0; /* end */ if(argc == 3){ f_name_i = 2; if(argv[1][0] == '-' && argv[1][1] == 'v') v = 1; } f = loadfile(argv[f_name_i]); loaded_file = f->p; mem = alloc_mem(MEM_SIZE); nible = 0; // starting by nible 0 times = 1; // run a command, at least once b = GETB(); if(v) printf("b = 0x%x\n", b); while(b){ if(b & BIT7){ if(v) printf("Isn't a char\n"); if(nible){ if(v) printf("Second nible\n"); if(b & BIT3){ // it is XXXX1XXXb? if(v) printf("Is a number of times to repeat\n"); switch(b & 0x07){ // 00000111b case 0: times = 0; break; case 1: times = 2; break; case 2: times = 4; break; case 3: times = 8; break; case 4: times = 16; break; case 5: times = 32; break; case 6: times = 64; break; case 7: times = 128; break; } if(times == 0){ } if(v) printf("Repeat %i\n", times); } else{ if(v) printf("Is a command\n"); times = 1; b <<= 4; b |= 0x80; // set first bt } } else{ if(v) printf("First nible\n"); times = 1; } for(; times >= 1; times--){ if(v) printf("Executando a %i_a vez\n", times); switch(b & 0x70){ // 01110000b case 0x00: // , //if(v) printf("Digite um caracter:\n"); mem[pos] = inC(); break; case 0x10: // + if(times > 1){ mem[pos] += times; if(v) printf("%i x +\n", times); times = 0; } else{ if(v) printf("+\n"); mem[pos]++; } break; case 0x20: // > if(v) printf(">\n"); if(pos >= MEM_SIZE){ printf("Fatal programer stupid!\nStop! traing to acess mem after end of BF machine\n"); exit(1); } pos++; break; case 0x30: // [ if(v) printf("[\n"); if(mem[pos]) stack[stack_i++] = MACRO_i; else while(b != ']') b = GETB(); break; case 0x40: // . outC(mem[pos]); if(v) printf(" = 0x%x\n", mem[pos]); break; case 0x50: // - if(times > 1){ mem[pos] -= times; if(v) printf("%i x -\n", times); times = 0; } else{ if(v) printf("-\n"); mem[pos]--; } break; case 0x60: // < if(v) printf("<\n"); if(pos <= 0){ printf("Fatal programer stupid!\nStop! traing to acess mem before star of BF machine\n"); exit(1); } pos--; break; case 0x70: // ] if(v) printf("]\n"); MACRO_i = stack[stack_i--]; break; } } if(!nible) nible = 1; else{ b = GETB(); if(v) printf("b = 0x%x\n", b); nible = 0; } } else{ if(v) printf("%c", b); //printf("Is a char: %c\n", b); nible = 0; b = GETB(); } } //printf("%s", mem); printf("\n"); return(0); }