#include <iostream> #include <vector> #include <unordered_set> #include <algorithm> struct Vertex { std::vector<int> next; int dfsnum, low, cycleIndex; bool T, inGraph, inCycle, finished, removedFromCycle; Vertex() : dfsnum(), low(), cycleIndex(-1), T(), inGraph(true), inCycle(), finished(false), removedFromCycle(false) {} }; using Graph = std::vector<Vertex>; using StronglyCC = std::vector<int>; using StronglyCCs = std::vector<StronglyCC>; void findStronglyConnectedComponents(Graph &graph, StronglyCCs &stronglyConnectedComponents); void findCycle(Graph &graph, StronglyCC &component, std::vector<int> &cycle); bool visit(Graph &graph, int p, std::vector<int> &cycle, int cycleIndex); int nodesNotRemovedFromCycle = -1; int main() { std::ios_base::sync_with_stdio(false); int n, m; std::cin >> n >> m; Graph graph; graph.resize(n + 1); graph[0].next.resize(n); // fake node for (int i = 0; i < n; ++i) { graph[0].next[i] = i + 1; } int b, e; for (int i = 0; i < m; ++i) { std::cin >> b >> e; graph[b].next.push_back(e); } StronglyCCs stronglyConnectedComponents; findStronglyConnectedComponents(graph, stronglyConnectedComponents); // there are at least two components because of fake node int maxComponentNumber = stronglyConnectedComponents[0].size() > stronglyConnectedComponents[1].size() ? 0 : 1; int secondMaxComponentNumber = 1 - maxComponentNumber; for (int i = 2; i < int(stronglyConnectedComponents.size()); ++i) { if (stronglyConnectedComponents[maxComponentNumber].size() < stronglyConnectedComponents[i].size()) { secondMaxComponentNumber = maxComponentNumber; maxComponentNumber = i; } else if (stronglyConnectedComponents[secondMaxComponentNumber].size() < stronglyConnectedComponents[i].size()) { secondMaxComponentNumber = i; } } if (stronglyConnectedComponents[maxComponentNumber].size() == 1) { std::cout << "NIE"; return 0; } if (stronglyConnectedComponents[secondMaxComponentNumber].size() > 1) { std::cout << 0 << "\n"; return 0; } // remove from graph everything except single component for (int i = 0; i < int(stronglyConnectedComponents.size()); ++i) { if (i != maxComponentNumber) { for (auto &v : stronglyConnectedComponents[i]) { graph[v].inGraph = false; } } } for (auto &v : graph) { if (v.inGraph) { v.next.erase(std::remove_if(v.next.begin(), v.next.end(), [graph](int v) { return !graph[v].inGraph; }), v.next.end()); } } StronglyCC &component = stronglyConnectedComponents[maxComponentNumber]; std::vector<int> cycle; for (auto v : component) { // reset dfs marker graph[v].T = false; } findCycle(graph, component, cycle); nodesNotRemovedFromCycle = cycle.size(); for (int i = 0; i < int(cycle.size()); ++i) { // dfs from each node in cycle graph[cycle[i]].T = true; if (visit(graph, cycle[i], cycle, i)) { // answer contains 0 nodes std::cout << 0 << "\n"; return 0; } } std::vector<int> answer; for (auto v : cycle) { if (!graph[v].removedFromCycle) { answer.push_back(v); } } std::sort(begin(answer), end(answer)); std::cout << answer.size() << "\n"; for (auto x : answer) { std::cout << x << " "; } } void visitSSC(Graph &graph, int p, StronglyCCs &stronglyConnectedComponents, int &N, std::vector<int> &L); void findStronglyConnectedComponents(Graph &graph, StronglyCCs &stronglyConnectedComponents) { int N = 0; std::vector<int> L; L.reserve(graph.size()); graph[0].T = true; visitSSC(graph, 0, stronglyConnectedComponents, N, L); for (auto &v : graph) { v.inGraph = true; } } void visitSSC(Graph &graph, int p, StronglyCCs &stronglyConnectedComponents, int &N, std::vector<int> &L) { L.push_back(p); graph[p].dfsnum = N++; graph[p].low = graph[p].dfsnum; for (auto &q : graph[p].next) { if (!graph[q].inGraph) { continue; } if (!graph[q].T) { graph[q].T = true; visitSSC(graph, q, stronglyConnectedComponents, N, L); graph[p].low = std::min(graph[p].low, graph[q].low); } else { graph[p].low = std::min(graph[p].low, graph[q].dfsnum); } } if (graph[p].low == graph[p].dfsnum) { stronglyConnectedComponents.emplace_back(); int last = 0; do { last = L.back(); L.pop_back(); stronglyConnectedComponents.back().push_back(last); graph[last].inGraph = false; } while (last != p); } } void findCycle(Graph &graph, StronglyCC &component, std::vector<int> &cycle) { int current = component[0]; while (!graph[current].T) { graph[current].T = true; current = graph[current].next[0]; } int start = current; int index = 0; do { cycle.push_back(current); graph[current].inCycle = true; graph[current].cycleIndex = index++; current = graph[current].next[0]; } while (current != start); current = component[0]; while (graph[current].T) { // reset dfs marker graph[current].T = false; current = graph[current].next[0]; } } int lastRemovalStart = 0, lastRemovalEnd = 1; bool removeFromCycle(Graph &graph, std::vector<int> &cycle, int from, int to) { if (to <= from) { to += cycle.size(); } int removeStart = graph[cycle[from]].removedFromCycle ? lastRemovalEnd - 1 : from; int removeStop = to < lastRemovalEnd ? lastRemovalEnd : to; if (removeStop - removeStart == 1) { // nothing to be done return false; } lastRemovalEnd = removeStop; int firstStop = std::min(removeStop, int(cycle.size())); for (int i = removeStart + 1; i < firstStop; ++i) { if (!graph[cycle[i]].removedFromCycle) { graph[cycle[i]].removedFromCycle = true; --nodesNotRemovedFromCycle; } } for (int i = 0; i < removeStop - int(cycle.size()); ++i) { if (!graph[cycle[i]].removedFromCycle) { graph[cycle[i]].removedFromCycle = true; --nodesNotRemovedFromCycle; } } return nodesNotRemovedFromCycle == 0; } bool visit(Graph &graph, int p, std::vector<int> &cycle, int cycleIndex) { for (int q : graph[p].next) { if (graph[q].inCycle) { if (removeFromCycle(graph, cycle, cycleIndex, graph[q].cycleIndex)) { // everything removed from cycle return true; } continue; } else if (graph[q].T) { if (!graph[q].finished) { // we have found second disjoint cycle return true; } } graph[q].T = true; if (visit(graph, q, cycle, cycleIndex)) { return true; } } graph[p].finished = true; return false; }
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | #include <iostream> #include <vector> #include <unordered_set> #include <algorithm> struct Vertex { std::vector<int> next; int dfsnum, low, cycleIndex; bool T, inGraph, inCycle, finished, removedFromCycle; Vertex() : dfsnum(), low(), cycleIndex(-1), T(), inGraph(true), inCycle(), finished(false), removedFromCycle(false) {} }; using Graph = std::vector<Vertex>; using StronglyCC = std::vector<int>; using StronglyCCs = std::vector<StronglyCC>; void findStronglyConnectedComponents(Graph &graph, StronglyCCs &stronglyConnectedComponents); void findCycle(Graph &graph, StronglyCC &component, std::vector<int> &cycle); bool visit(Graph &graph, int p, std::vector<int> &cycle, int cycleIndex); int nodesNotRemovedFromCycle = -1; int main() { std::ios_base::sync_with_stdio(false); int n, m; std::cin >> n >> m; Graph graph; graph.resize(n + 1); graph[0].next.resize(n); // fake node for (int i = 0; i < n; ++i) { graph[0].next[i] = i + 1; } int b, e; for (int i = 0; i < m; ++i) { std::cin >> b >> e; graph[b].next.push_back(e); } StronglyCCs stronglyConnectedComponents; findStronglyConnectedComponents(graph, stronglyConnectedComponents); // there are at least two components because of fake node int maxComponentNumber = stronglyConnectedComponents[0].size() > stronglyConnectedComponents[1].size() ? 0 : 1; int secondMaxComponentNumber = 1 - maxComponentNumber; for (int i = 2; i < int(stronglyConnectedComponents.size()); ++i) { if (stronglyConnectedComponents[maxComponentNumber].size() < stronglyConnectedComponents[i].size()) { secondMaxComponentNumber = maxComponentNumber; maxComponentNumber = i; } else if (stronglyConnectedComponents[secondMaxComponentNumber].size() < stronglyConnectedComponents[i].size()) { secondMaxComponentNumber = i; } } if (stronglyConnectedComponents[maxComponentNumber].size() == 1) { std::cout << "NIE"; return 0; } if (stronglyConnectedComponents[secondMaxComponentNumber].size() > 1) { std::cout << 0 << "\n"; return 0; } // remove from graph everything except single component for (int i = 0; i < int(stronglyConnectedComponents.size()); ++i) { if (i != maxComponentNumber) { for (auto &v : stronglyConnectedComponents[i]) { graph[v].inGraph = false; } } } for (auto &v : graph) { if (v.inGraph) { v.next.erase(std::remove_if(v.next.begin(), v.next.end(), [graph](int v) { return !graph[v].inGraph; }), v.next.end()); } } StronglyCC &component = stronglyConnectedComponents[maxComponentNumber]; std::vector<int> cycle; for (auto v : component) { // reset dfs marker graph[v].T = false; } findCycle(graph, component, cycle); nodesNotRemovedFromCycle = cycle.size(); for (int i = 0; i < int(cycle.size()); ++i) { // dfs from each node in cycle graph[cycle[i]].T = true; if (visit(graph, cycle[i], cycle, i)) { // answer contains 0 nodes std::cout << 0 << "\n"; return 0; } } std::vector<int> answer; for (auto v : cycle) { if (!graph[v].removedFromCycle) { answer.push_back(v); } } std::sort(begin(answer), end(answer)); std::cout << answer.size() << "\n"; for (auto x : answer) { std::cout << x << " "; } } void visitSSC(Graph &graph, int p, StronglyCCs &stronglyConnectedComponents, int &N, std::vector<int> &L); void findStronglyConnectedComponents(Graph &graph, StronglyCCs &stronglyConnectedComponents) { int N = 0; std::vector<int> L; L.reserve(graph.size()); graph[0].T = true; visitSSC(graph, 0, stronglyConnectedComponents, N, L); for (auto &v : graph) { v.inGraph = true; } } void visitSSC(Graph &graph, int p, StronglyCCs &stronglyConnectedComponents, int &N, std::vector<int> &L) { L.push_back(p); graph[p].dfsnum = N++; graph[p].low = graph[p].dfsnum; for (auto &q : graph[p].next) { if (!graph[q].inGraph) { continue; } if (!graph[q].T) { graph[q].T = true; visitSSC(graph, q, stronglyConnectedComponents, N, L); graph[p].low = std::min(graph[p].low, graph[q].low); } else { graph[p].low = std::min(graph[p].low, graph[q].dfsnum); } } if (graph[p].low == graph[p].dfsnum) { stronglyConnectedComponents.emplace_back(); int last = 0; do { last = L.back(); L.pop_back(); stronglyConnectedComponents.back().push_back(last); graph[last].inGraph = false; } while (last != p); } } void findCycle(Graph &graph, StronglyCC &component, std::vector<int> &cycle) { int current = component[0]; while (!graph[current].T) { graph[current].T = true; current = graph[current].next[0]; } int start = current; int index = 0; do { cycle.push_back(current); graph[current].inCycle = true; graph[current].cycleIndex = index++; current = graph[current].next[0]; } while (current != start); current = component[0]; while (graph[current].T) { // reset dfs marker graph[current].T = false; current = graph[current].next[0]; } } int lastRemovalStart = 0, lastRemovalEnd = 1; bool removeFromCycle(Graph &graph, std::vector<int> &cycle, int from, int to) { if (to <= from) { to += cycle.size(); } int removeStart = graph[cycle[from]].removedFromCycle ? lastRemovalEnd - 1 : from; int removeStop = to < lastRemovalEnd ? lastRemovalEnd : to; if (removeStop - removeStart == 1) { // nothing to be done return false; } lastRemovalEnd = removeStop; int firstStop = std::min(removeStop, int(cycle.size())); for (int i = removeStart + 1; i < firstStop; ++i) { if (!graph[cycle[i]].removedFromCycle) { graph[cycle[i]].removedFromCycle = true; --nodesNotRemovedFromCycle; } } for (int i = 0; i < removeStop - int(cycle.size()); ++i) { if (!graph[cycle[i]].removedFromCycle) { graph[cycle[i]].removedFromCycle = true; --nodesNotRemovedFromCycle; } } return nodesNotRemovedFromCycle == 0; } bool visit(Graph &graph, int p, std::vector<int> &cycle, int cycleIndex) { for (int q : graph[p].next) { if (graph[q].inCycle) { if (removeFromCycle(graph, cycle, cycleIndex, graph[q].cycleIndex)) { // everything removed from cycle return true; } continue; } else if (graph[q].T) { if (!graph[q].finished) { // we have found second disjoint cycle return true; } } graph[q].T = true; if (visit(graph, q, cycle, cycleIndex)) { return true; } } graph[p].finished = true; return false; } |