9115 {
9116 vector<node *> nodes(n);
9117 for(int i = 1; i <= n; i++) {
9118 nodes[i - 1] = new node(i, 0);
9119 }
9120 for(const auto &relation: relations) {
9121 nodes[relation[1] - 1]->pred.insert(nodes[relation[0] - 1]);
9122 nodes[relation[0] - 1]->out++;
9123 }
9124 bool flag = true;
9125 unordered_set<int> vis;
9126 while(flag) {
9127 flag = false;
9128 for(int i = 0; i < n; i++) {
9129 if(nodes[i]->out == 0) {
9130 if(vis.contains(i)) {
9131 continue;
9132 }
9133 vis.insert(i);
9134 flag = true;
9135 for(node *next: nodes[i]->pred) {
9136 next->out--;
9137 next->len = max(next->len, nodes[i]->len + 1);
9138 }
9139 }
9140 }
9141 }
9143 for(int i = 0; i < n; i++) {
9144 if(nodes[i]->out != 0) {
9145 return -1;
9146 }
9147 ans = max(ans, nodes[i]->len);
9148 }
9150 }
vector< vector< int > > ans