#include <iostream> #include <vector> #include <algorithm> #include <set> #include <math.h> #include <stdio.h> #include <deque> #include <unordered_map> #include <cassert> #include <numeric> #include <queue> #include <bitset> #include <string> #include <stack> #define LL long long using namespace std; struct Vertex { vector<int> adj; int state; Vertex() : state(0) {} void incr() { state = (state + 1) % adj.size(); } }; bitset<100> G_STATE; void go(vector<Vertex> &G, int v_idx) { Vertex& v = G[v_idx]; //printf("v_idx:%d, state=%d\n", v_idx, v.state); if (v.adj.size() == 0) { //printf("end at %d\n", v_idx); return; } go(G, v.adj[v.state]); v.incr(); G_STATE[v_idx] = v.state == 0 ? 0 : 1; } bool check(vector<Vertex> &G) { for (int i=0; i < G.size(); i++) { if (G[i].state != 0) return false; } return true; } bool check2() { return !G_STATE.any(); } int main() { int n; scanf("%d",&n); vector<Vertex> G(n+1); for (int i=1; i <= n; i++) { int len; scanf("%d", &len); for (int j=0; j < len; j++) { int y; scanf("%d", &y); G[i].adj.push_back(y); } } int counter = 1; while (true) { go(G, 1); if (check2()) { break; } counter++; } printf("%d\n", counter); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | #include <iostream> #include <vector> #include <algorithm> #include <set> #include <math.h> #include <stdio.h> #include <deque> #include <unordered_map> #include <cassert> #include <numeric> #include <queue> #include <bitset> #include <string> #include <stack> #define LL long long using namespace std; struct Vertex { vector<int> adj; int state; Vertex() : state(0) {} void incr() { state = (state + 1) % adj.size(); } }; bitset<100> G_STATE; void go(vector<Vertex> &G, int v_idx) { Vertex& v = G[v_idx]; //printf("v_idx:%d, state=%d\n", v_idx, v.state); if (v.adj.size() == 0) { //printf("end at %d\n", v_idx); return; } go(G, v.adj[v.state]); v.incr(); G_STATE[v_idx] = v.state == 0 ? 0 : 1; } bool check(vector<Vertex> &G) { for (int i=0; i < G.size(); i++) { if (G[i].state != 0) return false; } return true; } bool check2() { return !G_STATE.any(); } int main() { int n; scanf("%d",&n); vector<Vertex> G(n+1); for (int i=1; i <= n; i++) { int len; scanf("%d", &len); for (int j=0; j < len; j++) { int y; scanf("%d", &y); G[i].adj.push_back(y); } } int counter = 1; while (true) { go(G, 1); if (check2()) { break; } counter++; } printf("%d\n", counter); return 0; } |