#include <bits/stdc++.h> #include "sabotaz.h" #include "message.h" using namespace std; const int MAXN = 2e5+10; int n, m, d[MAXN], low[MAXN], last[MAXN], p[MAXN]; vector<int> g[MAXN]; bool visited[MAXN]; void read_data() { n = NumberOfIsles(); m = NumberOfBridges(); for (int i = 0; i < m; i++) { int u = BridgeEndA(i); int v = BridgeEndB(i); u++; v++; g[u].push_back(v); g[v].push_back(u); } } /*void dfs(int u) { int t = 0; vector<pair<int, int> > Q; Q.push_back(make_pair(u, 0)); while (!Q.empty()) { int u = Q.back().first; int st = Q.back().second; Q.pop_back(); //printf("dfs(%d, %d, %d)\n", u, st, p[u]); if (!visited[u]) { d[u] = ++t; low[u] = d[u]; } visited[u] = true; if (last[u] != 0) low[u] = min(low[u], low[last[u]]); if (st < int(g[u].size())) { Q.push_back(make_pair(u, st+1)); int v = g[u][st]; if (v != p[u]) { if (!visited[v]) { p[v] = u; Q.push_back(make_pair(v, 0)); last[u] = v; } else { low[u] = min(low[u], d[v]); } } } } }*/ int t; void dfs(int u) { for (int i = 0; i < int(g[u].size()); i++) { int v = g[u][i]; if (v == p[u]) { g[u].erase(g[u].begin()+i); break; } } d[u] = ++t; low[u] = d[u]; visited[u] = true; for (int i = 0; i < int(g[u].size()); i++) { int v = g[u][i]; p[v] = u; if (!visited[v]) { dfs(v); low[u] = min(low[u], low[v]); } else { low[u] = min(low[u], d[v]); } } } bool isroot[MAXN]; int main() { if (MyNodeId() != 0) return 0; read_data(); for (int i = 1; i <= n; i++) { if (!visited[i]) { dfs(i); isroot[i] = true; } } int out = 0; for (int i = 1; i <= n; i++) { //printf("%d: %d %d %d\n", i, d[i], p[i], low[i]); if (!isroot[i] && d[i] == low[i]) out++; } printf("%d\n", out); 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 | #include <bits/stdc++.h> #include "sabotaz.h" #include "message.h" using namespace std; const int MAXN = 2e5+10; int n, m, d[MAXN], low[MAXN], last[MAXN], p[MAXN]; vector<int> g[MAXN]; bool visited[MAXN]; void read_data() { n = NumberOfIsles(); m = NumberOfBridges(); for (int i = 0; i < m; i++) { int u = BridgeEndA(i); int v = BridgeEndB(i); u++; v++; g[u].push_back(v); g[v].push_back(u); } } /*void dfs(int u) { int t = 0; vector<pair<int, int> > Q; Q.push_back(make_pair(u, 0)); while (!Q.empty()) { int u = Q.back().first; int st = Q.back().second; Q.pop_back(); //printf("dfs(%d, %d, %d)\n", u, st, p[u]); if (!visited[u]) { d[u] = ++t; low[u] = d[u]; } visited[u] = true; if (last[u] != 0) low[u] = min(low[u], low[last[u]]); if (st < int(g[u].size())) { Q.push_back(make_pair(u, st+1)); int v = g[u][st]; if (v != p[u]) { if (!visited[v]) { p[v] = u; Q.push_back(make_pair(v, 0)); last[u] = v; } else { low[u] = min(low[u], d[v]); } } } } }*/ int t; void dfs(int u) { for (int i = 0; i < int(g[u].size()); i++) { int v = g[u][i]; if (v == p[u]) { g[u].erase(g[u].begin()+i); break; } } d[u] = ++t; low[u] = d[u]; visited[u] = true; for (int i = 0; i < int(g[u].size()); i++) { int v = g[u][i]; p[v] = u; if (!visited[v]) { dfs(v); low[u] = min(low[u], low[v]); } else { low[u] = min(low[u], d[v]); } } } bool isroot[MAXN]; int main() { if (MyNodeId() != 0) return 0; read_data(); for (int i = 1; i <= n; i++) { if (!visited[i]) { dfs(i); isroot[i] = true; } } int out = 0; for (int i = 1; i <= n; i++) { //printf("%d: %d %d %d\n", i, d[i], p[i], low[i]); if (!isroot[i] && d[i] == low[i]) out++; } printf("%d\n", out); return 0; } |