題目鏈接
題意:給定一個滑雪場,每個點能向周圍4個點高度小於等於這個點的點滑,現在要建電纜,使得任意兩點都有路徑互相可達,問最少需要幾條電纜
思路:強連通縮點,每個點就是一個點,能走的建邊,縮點後找入度出度為0的個數的最大值就是答案,注意一開始就強連通了答案應該是0
代碼:
#include#include #include #include using namespace std; const int N = 250005; const int M = 1000005; const int d[4][2] = {0, 1, 0, -1, 1, 0, -1, 0}; int n, m; struct Edge { int u, v; Edge() {} Edge(int u, int v) { this->u = u; this->v = v; } } edge[M]; int en, first[N], next[M]; void add(int u, int v) { edge[en] = Edge(u, v); next[en] = first[u]; first[u] = en++; } int h[505][505]; int pre[N], dfn[N], sccn, sccno[N], dfs_clock; stack S; void dfs_scc(int u) { pre[u] = dfn[u] = ++dfs_clock; S.push(u); for (int i = first[u]; i + 1; i = next[i]) { int v = edge[i].v; if (!pre[v]) { dfs_scc(v); dfn[u] = min(dfn[u], dfn[v]); } else if (!sccno[v]) dfn[u] = min(dfn[u], pre[v]); } if (dfn[u] == pre[u]) { sccn++; while (1) { int x = S.top(); S.pop(); sccno[x] = sccn; if (x == u) break; } } } void find_scc() { sccn = dfs_clock = 0; memset(pre, 0, sizeof(pre)); memset(sccno, 0, sizeof(sccno)); for (int i = 0; i < n * m; i++) if (!pre[i]) dfs_scc(i); } int in[N], out[N]; int main() { while (~scanf("%d%d", &m, &n)) { en = 0; memset(first, -1, sizeof(first)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) scanf("%d", &h[i][j]); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { for (int k = 0; k < 4; k++) { int x = i + d[k][0]; int y = j + d[k][1]; if (x < 0 || x >= n || y < 0 || y >= m) continue; if (h[i][j] >= h[x][y]) add(i * m + j, x * m + y); } } } find_scc(); if (sccn == 1) { printf("0\n"); continue; } memset(in, 0, sizeof(in)); memset(out, 0, sizeof(out)); for (int u = 0; u < n * m; u++) { for (int j = first[u]; j + 1; j = next[j]) { int v = edge[j].v; if (sccno[u] != sccno[v]) { in[sccno[v]]++; out[sccno[u]]++; } } } int ins = 0, outs = 0; for (int i = 1; i <= sccn; i++) { if (!in[i]) ins++; if (!out[i]) outs++; } printf("%d\n", max(ins, outs)); } return 0; }