#include #include #include #include #include #include #include #include #include using namespace std; set nodeset; vector > a; // set> m; // edge map mm; unordered_map visited; void AddDependency(unsigned int ModuleId, unsigned int DependModuleId) { m.insert(make_pair(ModuleId, DependModuleId)); } bool ModulesCycularDependency(unsigned int ModuleId) { int n = nodeset.size(); vector d; d.push_back((int)ModuleId); visited[ModuleId] = 1; while (!d.empty()) { int i = d.back(); d.pop_back(); for (int k = 0; k < a[i].size(); k++) { if (!visited.count(a[i][k])) { visited[a[i][k]] = 1; d.push_back(a[i][k]); } else { if (a[i][k] == ModuleId) { string output = (string)mm[ModuleId]; std::cout << output << " is false" << std::endl; visited.clear(); return true; } } } } string output = (string)mm[ModuleId]; std::cout << output << " is true" << std::endl; visited.clear(); return false; } void clear(void) { } int main() { string in; int count = 0; while(getline(cin, in), in != "") { size_t pos = in.find_first_of(','); if (pos != std::string::npos) { if (in[in.size() - 1] == ',') { int x = std::stoul(in.substr(1, pos - 1), nullptr, 16); int y = std::stoul(in.substr(pos+2, in.size() - 4 - pos), nullptr, 16); nodeset.insert(x); nodeset.insert(y); mm.insert(make_pair(x, in.substr(1, pos - 1))); mm.insert(make_pair(y, in.substr(pos + 2, in.size() - 4 - pos))); m.insert(make_pair(x, y)); //node.push_back(in.substr(1, pos - 1)); //node.push_back(in.substr(pos + 2, in.size() - 4 - pos)); } else { int x = std::stoul(in.substr(1, pos - 1), nullptr, 16); int y = std::stoul(in.substr(pos+2, in.size() - 3 - pos), nullptr, 16); nodeset.insert(x); nodeset.insert(y); mm.insert(make_pair(x, in.substr(1, pos - 1))); mm.insert(make_pair(y ,in.substr(pos + 2, in.size() - 3 - pos))); m.insert(make_pair(x, y)); } } if (in[in.size() - 1] != ',') break; } a.assign((int)nodeset.size(), vector()); for (auto& x : m) { a[x.first].push_back(x.second); } for (int i = 0; i < nodeset.size(); i++) { ModulesCycularDependency(i); } return 0; }
点赞 评论

相关推荐

点赞 评论 收藏
分享
大摆哥:刚好要做个聊天软件,直接让你帮他干活了
点赞 评论 收藏
分享
牛客网
牛客企业服务