P3225 [HNOI2012]矿场搭建 割点 tarjan 双联通分量
题意
煤矿工地可以看成是由隧道连接挖煤点组成的无向图。为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处。于是矿主决定在某些挖煤点设立救援出口,使得无论哪一个挖煤点坍塌之后,其他挖煤点的工人都有一条道路通向救援出口。
请写一个程序,用来计算至少需要设置几个救援出口,以及不同最少救援出口的设置方案总数。
思路
找出图中的割点,利用tarjan, 然后对非割点的联通块dfs。
找出一个块中直接相邻的割点个数。
如果个数为0,说明不能通过割点出逃,所以要在这个联通块中选两个点作为出口。
如果个数为1,要另找一个出口,以防割点被堵住。
如果个数>=2, 就不用了,从割点出逃就行了。
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> using namespace std; #define lson (l, mid, rt << 1) #define rson (mid + 1, r, rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef pair<int, pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl '\n' #define boost \ ios::sync_with_stdio(false); \ cin.tie(0) #define rep(a, b, c) for (int a = (b); a <= (c); ++a) #define max3(a, b, c) max(max(a, b), c); #define min3(a, b, c) min(min(a, b), c); const ll oo = 1ll << 17; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9 + 7; const double esp = 1e-8; const double PI = acos(-1.0); const double PHI = 0.61803399; //黄金分割点 const double tPHI = 0.38196601; template <typename T> inline T read(T &x) { x = 0; int f = 0; char ch = getchar(); while (ch < '0' || ch > '9') f |= (ch == '-'), ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x = f ? -x : x; } inline void cmax(int &x, int y) { if (x < y) x = y; } inline void cmax(ll &x, ll y) { if (x < y) x = y; } inline void cmin(int &x, int y) { if (x > y) x = y; } inline void cmin(ll &x, ll y) { if (x > y) x = y; } /*-----------------------showtime----------------------*/ const int maxn = 509; struct E { int v, nxt; } edge[maxn * maxn]; int head[maxn], gtot = 0; void addedge(int u, int v) { edge[gtot].v = v; edge[gtot].nxt = head[u]; head[u] = gtot++; } int dfn[maxn], low[maxn], cnt = 0; int cut[maxn]; int rt, rs; void tarjan(int u, int fa) { dfn[u] = low[u] = ++cnt; for (int i = head[u]; ~i; i = edge[i].nxt) { int v = edge[i].v; if (!dfn[v]) { tarjan(v, u); low[u] = min(low[u], low[v]); if (low[v] >= dfn[u]) { if (u == rt) { rs++; } else cut[u] = 1; } } else if (v != fa) { low[u] = min(low[u], dfn[v]); } } } int vis[maxn], mark = 0; int num, ge_num; void dfs(int u) { vis[u] = mark; num++; for (int i = head[u]; ~i; i = edge[i].nxt) { int v = edge[i].v; if (cut[v] && vis[v] < mark) { vis[v] = mark; ge_num++; } else if (!vis[v]) { dfs(v); } } } int main() { int m, n, cas = 0; while (~scanf("%d", &m) && m) { memset(head, -1, sizeof(head)); gtot = 0; cnt = 0; n = 0; for (int i = 1; i <= m; i++) { int x, y; scanf("%d%d", &x, &y); addedge(x, y); addedge(y, x); n = max(n, x); n = max(n, y); } memset(dfn, 0, sizeof(dfn)); memset(low, 0, sizeof(low)); memset(cut, 0, sizeof(cut)); for (int i = 1; i <= n; i++) { if (dfn[i] == 0) { rt = i; rs = 0; tarjan(i, -1); if (rs >= 2) cut[rt] = true; } } int ans1 = 0; ll ans2 = 1; mark = 0; memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) { if (!vis[i] && !cut[i]) { num = 0; ge_num = 0; ++mark; dfs(i); if (ge_num == 0) { ans1 += 2; ans2 *= 1ll * (num - 1) * num / 2; } else if (ge_num == 1) { ans1 += 1; ans2 *= num; } } } printf("Case %d: %d %lld\n", ++cas, ans1, ans2); } return 0; }
skr