#include <bits/stdc++.h> using namespace std; using LL = long long; #define e1 first #define e2 second #define pb push_back #define OUT(x) {cout << x << "\n"; exit(0); } #define TCOUT(x) {cout << x << "\n"; return; } #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define rep(i, l, r) for(int i = (l); i < (r); ++i) #define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } #define sz(x) int(x.size()) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; #include <algorithm> #include <algorithm> #include <utility> #include <vector> namespace atcoder { namespace internal { template <class E> struct csr { std::vector<int> start; std::vector<E> elist; csr(int n, const std::vector<std::pair<int, E>>& edges) : start(n + 1), elist(edges.size()) { for (auto e : edges) { start[e.first + 1]++; } for (int i = 1; i <= n; i++) { start[i] += start[i - 1]; } auto counter = start; for (auto e : edges) { elist[counter[e.first]++] = e.second; } } }; // Reference: // R. Tarjan, // Depth-First Search and Linear Graph Algorithms struct scc_graph { public: scc_graph(int n) : _n(n) {} int num_vertices() { return _n; } // @return pair of (# of scc, scc id) std::pair<int, std::vector<int>> scc_ids() { auto g = csr<int>(_n, edges); int now_ord = 0, group_num = 0; std::vector<int> visited, low(_n), ord(_n, -1), ids(_n); visited.reserve(_n); auto dfs = [&](auto self, int v) -> void { low[v] = ord[v] = now_ord++; visited.push_back(v); for (int i = g.start[v]; i < g.start[v + 1]; i++) { auto to = g.elist[i]; if (ord[to] == -1) { self(self, to); low[v] = std::min(low[v], low[to]); } else { low[v] = std::min(low[v], ord[to]); } } if (low[v] == ord[v]) { while (true) { int u = visited.back(); visited.pop_back(); ord[u] = _n; ids[u] = group_num; if (u == v) break; } group_num++; } }; for (int i = 0; i < _n; i++) { if (ord[i] == -1) dfs(dfs, i); } for (auto& x : ids) { x = group_num - 1 - x; } return {group_num, ids}; } std::vector<std::vector<int>> scc() { auto ids = scc_ids(); int group_num = ids.first; std::vector<int> counts(group_num); for (auto x : ids.second) counts[x]++; std::vector<std::vector<int>> groups(ids.first); for (int i = 0; i < group_num; i++) { groups[i].reserve(counts[i]); } for (int i = 0; i < _n; i++) { groups[ids.second[i]].push_back(i); } return groups; } public: int _n; std::vector<std::pair<int, int>> edges; }; } // namespace internal } // namespace atcoder #include <cassert> #include <vector> namespace atcoder { struct scc_graph { public: scc_graph() : internal(0) {} scc_graph(int n) : internal(n) {} scc_graph(int _n, const vector<pii> &graf) : internal(_n) { internal.edges = graf; } std::vector<std::vector<int>> scc() { return internal.scc(); } private: internal::scc_graph internal; }; } // namespace atcoder using namespace atcoder; mt19937_64 rng(time(0)); int random(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } #ifdef DEBUG template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #endif #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif vi petla; //Did you REAALLY consider sample tests? vector<vi> graf_without_node_x(vector<vi> &graf, int x) { vector<vi> copy(graf); copy[x].clear(); return copy; } int no_edges = 0; vector <vi> get_scc(const vector <vi> &graf, int x) { vector <pii> new_edges(no_edges - sz(graf[x])); int n = sz(graf); int DL = 0; rep(i, 0, n) { if (i == x) continue; trav(u, graf[i]) new_edges[DL++] = {i, u}; } scc_graph solver(n, new_edges); return solver.scc(); } vector <ll> brut(vector<vi> &graf) { int n = sz(graf); vector <ll> result(n + 1, 0); rep(x, 0, n) { auto v = graf_without_node_x(graf, x); auto scc = get_scc(graf, x); vector <int> min_nonavoidable(n); //what is the smallest k such that you CANNOT avoid node x? vector <bool> reachable(n, 0); reachable[x] = 1; for (int comp = sz(scc) - 1; comp >= 0; --comp) { bool reach_x = false; // find reachability trav(node_in_scc, scc[comp]) { trav(neighbor, v[node_in_scc]) { if (reachable[neighbor]) reach_x = 1; } if (reachable[node_in_scc]) reach_x = 1; } if (reach_x) { trav(node_in_scc, scc[comp]) reachable[node_in_scc] = 1; } if (sz(scc[comp]) > 1) { trav(node, scc[comp]) { min_nonavoidable[node] = n; //you can always avoid node x } } else { int the_only_node = scc[comp][0]; min_nonavoidable[the_only_node] = the_only_node; //you can always avoid when you are dead if (petla[the_only_node]) { min_nonavoidable[the_only_node] = n; //you can always avoid x by going through the loop } trav(neighbor, v[the_only_node]) { min_nonavoidable[the_only_node] = max(min_nonavoidable[the_only_node], min_nonavoidable[neighbor]); } if (the_only_node == x) { min_nonavoidable[x] = x; //if k < x, you can avoid x because you can never go there } } } rep(i, 0, n) { if (reachable[i] && x != i) { int g = min_nonavoidable[i]; result[g]++; //cerr << i + 1 << " always goes to " << x + 1 << " if k >= " << g + 1 << "\n"; } } } rep(i, 0, n) result[i + 1] += result[i]; result.pop_back(); return result; } bool TEST = 0; int n; int main() { if (!TEST) { cin >> n; vector<vi> graf(n); petla.resize(n, 0); rep(i, 0, n) { int k; cin >> k; no_edges += k; graf[i].resize(k); rep(j, 0, k) { cin >> graf[i][j]; graf[i][j] -= 1; if (graf[i][j] == i) petla[i] = 1; } } rep(i, 0, n) sort(all(graf[i])); auto vec = brut(graf); rep(k, 0, n) cout << vec[k] << ' '; } else { /*FOR(step, 1, 100) { int n, k; cin >> n >> k; cout << wzo(n, k).val() << "\n"; } */ } }
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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | #include <bits/stdc++.h> using namespace std; using LL = long long; #define e1 first #define e2 second #define pb push_back #define OUT(x) {cout << x << "\n"; exit(0); } #define TCOUT(x) {cout << x << "\n"; return; } #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define rep(i, l, r) for(int i = (l); i < (r); ++i) #define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } #define sz(x) int(x.size()) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; #include <algorithm> #include <algorithm> #include <utility> #include <vector> namespace atcoder { namespace internal { template <class E> struct csr { std::vector<int> start; std::vector<E> elist; csr(int n, const std::vector<std::pair<int, E>>& edges) : start(n + 1), elist(edges.size()) { for (auto e : edges) { start[e.first + 1]++; } for (int i = 1; i <= n; i++) { start[i] += start[i - 1]; } auto counter = start; for (auto e : edges) { elist[counter[e.first]++] = e.second; } } }; // Reference: // R. Tarjan, // Depth-First Search and Linear Graph Algorithms struct scc_graph { public: scc_graph(int n) : _n(n) {} int num_vertices() { return _n; } // @return pair of (# of scc, scc id) std::pair<int, std::vector<int>> scc_ids() { auto g = csr<int>(_n, edges); int now_ord = 0, group_num = 0; std::vector<int> visited, low(_n), ord(_n, -1), ids(_n); visited.reserve(_n); auto dfs = [&](auto self, int v) -> void { low[v] = ord[v] = now_ord++; visited.push_back(v); for (int i = g.start[v]; i < g.start[v + 1]; i++) { auto to = g.elist[i]; if (ord[to] == -1) { self(self, to); low[v] = std::min(low[v], low[to]); } else { low[v] = std::min(low[v], ord[to]); } } if (low[v] == ord[v]) { while (true) { int u = visited.back(); visited.pop_back(); ord[u] = _n; ids[u] = group_num; if (u == v) break; } group_num++; } }; for (int i = 0; i < _n; i++) { if (ord[i] == -1) dfs(dfs, i); } for (auto& x : ids) { x = group_num - 1 - x; } return {group_num, ids}; } std::vector<std::vector<int>> scc() { auto ids = scc_ids(); int group_num = ids.first; std::vector<int> counts(group_num); for (auto x : ids.second) counts[x]++; std::vector<std::vector<int>> groups(ids.first); for (int i = 0; i < group_num; i++) { groups[i].reserve(counts[i]); } for (int i = 0; i < _n; i++) { groups[ids.second[i]].push_back(i); } return groups; } public: int _n; std::vector<std::pair<int, int>> edges; }; } // namespace internal } // namespace atcoder #include <cassert> #include <vector> namespace atcoder { struct scc_graph { public: scc_graph() : internal(0) {} scc_graph(int n) : internal(n) {} scc_graph(int _n, const vector<pii> &graf) : internal(_n) { internal.edges = graf; } std::vector<std::vector<int>> scc() { return internal.scc(); } private: internal::scc_graph internal; }; } // namespace atcoder using namespace atcoder; mt19937_64 rng(time(0)); int random(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } #ifdef DEBUG template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #endif #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif vi petla; //Did you REAALLY consider sample tests? vector<vi> graf_without_node_x(vector<vi> &graf, int x) { vector<vi> copy(graf); copy[x].clear(); return copy; } int no_edges = 0; vector <vi> get_scc(const vector <vi> &graf, int x) { vector <pii> new_edges(no_edges - sz(graf[x])); int n = sz(graf); int DL = 0; rep(i, 0, n) { if (i == x) continue; trav(u, graf[i]) new_edges[DL++] = {i, u}; } scc_graph solver(n, new_edges); return solver.scc(); } vector <ll> brut(vector<vi> &graf) { int n = sz(graf); vector <ll> result(n + 1, 0); rep(x, 0, n) { auto v = graf_without_node_x(graf, x); auto scc = get_scc(graf, x); vector <int> min_nonavoidable(n); //what is the smallest k such that you CANNOT avoid node x? vector <bool> reachable(n, 0); reachable[x] = 1; for (int comp = sz(scc) - 1; comp >= 0; --comp) { bool reach_x = false; // find reachability trav(node_in_scc, scc[comp]) { trav(neighbor, v[node_in_scc]) { if (reachable[neighbor]) reach_x = 1; } if (reachable[node_in_scc]) reach_x = 1; } if (reach_x) { trav(node_in_scc, scc[comp]) reachable[node_in_scc] = 1; } if (sz(scc[comp]) > 1) { trav(node, scc[comp]) { min_nonavoidable[node] = n; //you can always avoid node x } } else { int the_only_node = scc[comp][0]; min_nonavoidable[the_only_node] = the_only_node; //you can always avoid when you are dead if (petla[the_only_node]) { min_nonavoidable[the_only_node] = n; //you can always avoid x by going through the loop } trav(neighbor, v[the_only_node]) { min_nonavoidable[the_only_node] = max(min_nonavoidable[the_only_node], min_nonavoidable[neighbor]); } if (the_only_node == x) { min_nonavoidable[x] = x; //if k < x, you can avoid x because you can never go there } } } rep(i, 0, n) { if (reachable[i] && x != i) { int g = min_nonavoidable[i]; result[g]++; //cerr << i + 1 << " always goes to " << x + 1 << " if k >= " << g + 1 << "\n"; } } } rep(i, 0, n) result[i + 1] += result[i]; result.pop_back(); return result; } bool TEST = 0; int n; int main() { if (!TEST) { cin >> n; vector<vi> graf(n); petla.resize(n, 0); rep(i, 0, n) { int k; cin >> k; no_edges += k; graf[i].resize(k); rep(j, 0, k) { cin >> graf[i][j]; graf[i][j] -= 1; if (graf[i][j] == i) petla[i] = 1; } } rep(i, 0, n) sort(all(graf[i])); auto vec = brut(graf); rep(k, 0, n) cout << vec[k] << ' '; } else { /*FOR(step, 1, 100) { int n, k; cin >> n >> k; cout << wzo(n, k).val() << "\n"; } */ } } |