求割点模板题
#include <bits/stdc++.h> using namespace std; const int N = 10004; int dfn[N], low[N]; bool ok[N]; vector<int>V[N]; int n, num, root; void dfs(int s, int f) { low[s] = dfn[s] = ++num; int child = 0; for(unsigned int i = 0; i < V[s].size(); i++) { int v = V[s][i]; if(!dfn[v]) { child++; dfs(v, s); low[s] = min(low[s], low[v]);//更新s节点能访问到最早的时间戳 if(low[v] >= dfn[s] && s != root) ok[s] = 1; if(s == root && child >= 2) ok[s] = 1; } else if(v != f) low[s] = min(low[s], dfn[v]); } } int main() { int t, n, cas = 0, m, a, b; //freopen("1.txt", "w", stdout); cin>>t; while(t--) { memset(dfn, 0, sizeof dfn); memset(low, 0, sizeof low); memset(ok, 0, sizeof ok); scanf("%d%d", &n, &m); for(int i = 0 ; i <= n; i++) V[i].clear(); num = 0; for(int i = 1; i <= m; i++) { scanf("%d%d", &a, &b); V[a].push_back(b); V[b].push_back(a); } for(int i = 1; i <= n; i++)//图不一定连通 { if(!dfn[i]) { root = i; dfs(i, i); } } int ans = 0; for(int i = 1; i <= n; i++) if(ok[i]) ans++; printf("Case %d: %d\n", ++cas, ans); } return 0; }
原文地址:https://www.cnblogs.com/lesroad/p/8794888.html
时间: 2024-11-05 22:53:47