#include <algorithm> #include <iostream> #include <fstream> #include <sstream> #include <vector> #include <string.h> using namespace std; vector<vector<int>>G; int n, m; int *V; bool dfs(int start, int excl) { vector<int>S; S.push_back(start); while (!S.empty()){ int p = S.back(); if (V[p] == 0) { V[p] = 1; for (int q : G[p]) { if (V[q] == 1) { return true; } else if (!V[q] && q != excl) { S.push_back(q); } } } else { V[p] = 2; S.pop_back(); } } return false; } int solveBrut() { int res = 0; for (int excl = 0; excl < n; excl++) { memset(V, 0, n*sizeof(int)); bool found = false; for (int start = 0; start < n; start++){ if (V[start] == 0 && start != excl) { found |= dfs(start, excl); } } if (!found) { //cout << excl + 1 << " on every\n"; res++; } } return res; } int dfsCount(int start,int offset, vector<int>& backOpen) { vector<int>S; vector<int> count; vector<int> index; vector<int> open; vector<int> backEnd; index.resize(n); open.resize(n); count.resize(n); backEnd.resize(n); S.push_back(start); while (!S.empty()){ int p = S.back(); if (V[p] == 0) { count[p] = 0; V[p] = 1; open[p] = -1; index[p] == 0; backEnd[p] = 0; backOpen[p] = 0; } else { if (open[p] != -1) { int q = open[p]; count[p] += count[q]; index[p] = index[p] + 1; backOpen[p] += backOpen[q] - backEnd[q]; open[p] = -1; } else { if (index[p] == G[p].size()) { V[p] = 2; S.pop_back(); continue; } else { int edges = G[p].size(); int q = G[p][(index[p]+start)%edges]; if (V[q] == 0) { // cout << "drzewna: " << p + 1 << " " << q + 1 << "\n"; S.push_back(q); open[p] = q; } else if (V[q] == 1) { // cout << "wsteczna " << p + 1 << " " << q + 1 << "\n"; count[p]++; index[p]++; backEnd[q] += 1; backOpen[p] += 1; } else { // cout << "poprzeczna: " << p + 1 << " " << q + 1 << "\n"; index[p]++; } } } } } return count[start]; } bool *candidate; int findCand(int offset) { memset(V, 0, n*sizeof(int)); vector<int> open; open.resize(n,0); int count = 0; for (int start = 0; start < n; start++){ int p = (offset + start) % n; if (V[p] == 0) { count += dfsCount(p, offset,open); } } if (count == 0) { cout << "NIE\n"; return -1; } vector<int> candidates; for (int i = 0; i < n; i++) { if (open[i] != count) candidate[i] = false; } } int solve() { candidate = new bool[n]; for (int i = 0; i < n; i++)candidate[i] = true; int runs = 0; for (int i = 0; i < n; i++) { if (!candidate[i])continue; int r = findCand(i); if (++runs == 75)break; if (r == -1)return -1; } int res = 0; for (int i = 0; i < n; i++)if (candidate[i])res++; cout << res << "\n"; for (int i = 0; i < n;i++) { if(candidate[i])cout << i+1 << " "; } cout << "\n"; return res; } void testFiles(){ for (int i = 3; i <= 3; i++) { ifstream file; file.open("Medium/kon" + to_string(i) + ".in"); ifstream outfile; outfile.open("Medium/kon" + to_string(i) + ".out"); file >> n >> m; G.clear(); G.resize(n); V = new int[n]; for (int i = 0; i < m; i++) { int a, b; file >> a >> b; a--; b--; G[a].push_back(b); } cout << i << ":\n"; //int res = solveBrut(); int res = solve(); int outres=0; outfile >> outres; cout <<"correct: " << outres << "\n"; file.close(); } } int main() { std::ios_base::sync_with_stdio(false); cin >> n >> m; G.resize(n); V = new int[n]; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; //a=i;b=(i+1)%n; G[a].push_back(b); } int res =solve(); // testFiles(); 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 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 | #include <algorithm> #include <iostream> #include <fstream> #include <sstream> #include <vector> #include <string.h> using namespace std; vector<vector<int>>G; int n, m; int *V; bool dfs(int start, int excl) { vector<int>S; S.push_back(start); while (!S.empty()){ int p = S.back(); if (V[p] == 0) { V[p] = 1; for (int q : G[p]) { if (V[q] == 1) { return true; } else if (!V[q] && q != excl) { S.push_back(q); } } } else { V[p] = 2; S.pop_back(); } } return false; } int solveBrut() { int res = 0; for (int excl = 0; excl < n; excl++) { memset(V, 0, n*sizeof(int)); bool found = false; for (int start = 0; start < n; start++){ if (V[start] == 0 && start != excl) { found |= dfs(start, excl); } } if (!found) { //cout << excl + 1 << " on every\n"; res++; } } return res; } int dfsCount(int start,int offset, vector<int>& backOpen) { vector<int>S; vector<int> count; vector<int> index; vector<int> open; vector<int> backEnd; index.resize(n); open.resize(n); count.resize(n); backEnd.resize(n); S.push_back(start); while (!S.empty()){ int p = S.back(); if (V[p] == 0) { count[p] = 0; V[p] = 1; open[p] = -1; index[p] == 0; backEnd[p] = 0; backOpen[p] = 0; } else { if (open[p] != -1) { int q = open[p]; count[p] += count[q]; index[p] = index[p] + 1; backOpen[p] += backOpen[q] - backEnd[q]; open[p] = -1; } else { if (index[p] == G[p].size()) { V[p] = 2; S.pop_back(); continue; } else { int edges = G[p].size(); int q = G[p][(index[p]+start)%edges]; if (V[q] == 0) { // cout << "drzewna: " << p + 1 << " " << q + 1 << "\n"; S.push_back(q); open[p] = q; } else if (V[q] == 1) { // cout << "wsteczna " << p + 1 << " " << q + 1 << "\n"; count[p]++; index[p]++; backEnd[q] += 1; backOpen[p] += 1; } else { // cout << "poprzeczna: " << p + 1 << " " << q + 1 << "\n"; index[p]++; } } } } } return count[start]; } bool *candidate; int findCand(int offset) { memset(V, 0, n*sizeof(int)); vector<int> open; open.resize(n,0); int count = 0; for (int start = 0; start < n; start++){ int p = (offset + start) % n; if (V[p] == 0) { count += dfsCount(p, offset,open); } } if (count == 0) { cout << "NIE\n"; return -1; } vector<int> candidates; for (int i = 0; i < n; i++) { if (open[i] != count) candidate[i] = false; } } int solve() { candidate = new bool[n]; for (int i = 0; i < n; i++)candidate[i] = true; int runs = 0; for (int i = 0; i < n; i++) { if (!candidate[i])continue; int r = findCand(i); if (++runs == 75)break; if (r == -1)return -1; } int res = 0; for (int i = 0; i < n; i++)if (candidate[i])res++; cout << res << "\n"; for (int i = 0; i < n;i++) { if(candidate[i])cout << i+1 << " "; } cout << "\n"; return res; } void testFiles(){ for (int i = 3; i <= 3; i++) { ifstream file; file.open("Medium/kon" + to_string(i) + ".in"); ifstream outfile; outfile.open("Medium/kon" + to_string(i) + ".out"); file >> n >> m; G.clear(); G.resize(n); V = new int[n]; for (int i = 0; i < m; i++) { int a, b; file >> a >> b; a--; b--; G[a].push_back(b); } cout << i << ":\n"; //int res = solveBrut(); int res = solve(); int outres=0; outfile >> outres; cout <<"correct: " << outres << "\n"; file.close(); } } int main() { std::ios_base::sync_with_stdio(false); cin >> n >> m; G.resize(n); V = new int[n]; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; //a=i;b=(i+1)%n; G[a].push_back(b); } int res =solve(); // testFiles(); return 0; } |