// trees !!!!!! DONE !!!!!!! #include #include class Node { private: int num; Node * back; public: Node (int _num) {num = _num; cnt = -1; visited = 0; back = NULL;}; void setBack (Node * _back) {back = _back;}; Node * getBack () {return back;}; int setChild (Node * child) {cnt++; if (cnt > 99) return 0; children[cnt] = child; return 1;}; int bad () {return (cnt > 99);}; int visited; // times visited; for DFS int cnt; Node * children[10]; int id () {return num;}; }; Node * links[20000]; int DFS (Node * cur, int & total) { cur->visited++; if (cur->visited > 1) return 0; // bad total++; int temp = 1; for (int i=0; i <= cur->cnt; i++) temp = temp && DFS(cur->children[i], total); return temp; } int Bad () { int node; int total = 0; // find total # of nodes & input overflow for (int i=0; i < 20000; i++) if (links[i] != NULL) { node = i; total++; if ((links[i])->bad()) return -1; } // back up to head i = 0; Node * cur = links[node]; while (cur->getBack() != NULL && i < total) { cur = cur->getBack(); i++; } if (i == total) return -2; // DFS int _total = 0; if (!DFS(cur, _total)) return -3; if (_total < total) return -4; return cur->id(); } void main() { ifstream in; in.open("fight.in"); ofstream out; out.open("fight.out"); int total; // reset total = 0; for (int i=0; i < 20000; i++) links[i] = NULL; while (1) { int a, b; in >> a >> b; if (a < 0) break; if (a == 0 && b == 0) { // analyze int temp; if ((temp = Bad()) < 0) out << "INVALID" << endl; //Bad tree; reason # " << temp << endl; else out << temp << endl; //"Good tree; head = " << temp << endl; // reset total = 0; for (i=0; i < 20000; i++) links[i] = NULL; } else { if (links[a] == NULL) links[a] = new Node(a); if (links[b] == NULL) links[b] = new Node(b); if ((links[a])->setChild(links[b])) { (links[b])->setBack(links[a]); } } } in.close(); out.close(); } Downloader failed! Response object 006~ASP 0159~Buffering Off~Buffering must be on.