【POJ】2117 Electricity
无向图求割点和连通块。
1 /* POJ2117 */ 2 #include <iostream> 3 #include <vector> 4 #include <algorithm> 5 #include <cstdio> 6 #include <cstring> 7 #include <cstdlib> 8 using namespace std; 9 10 #define MAXN 10005 11 12 vector<int> vc[MAXN]; 13 int low[MAXN], pre[MAXN]; 14 int n, m; 15 int dfs_clock, ans, link; 16 17 void init() { 18 int i; 19 20 for (i=0; i<n; ++i) 21 vc[i].clear(); 22 23 memset(pre, 0, sizeof(int)*n); 24 dfs_clock = ans = link = 0; 25 } 26 27 void tarjan(int u, int fa) { 28 int i, j, k, v; 29 int child = 0; 30 int cnt = 0; 31 32 pre[u] = low[u] = ++dfs_clock; 33 for (i=0; i<vc[u].size(); ++i) { 34 v = vc[u][i]; 35 if (!pre[v]) { 36 ++child; 37 tarjan(v, u); 38 low[u] = min(low[u], low[v]); 39 if (low[v] >= pre[u]) { 40 ++cnt; 41 } 42 } else if (pre[v]<pre[u] && v!=fa){ 43 low[u] = min(low[u], pre[v]); 44 } 45 } 46 if (fa == -1) { 47 if (child > 1) 48 ans = max(ans, child-1); 49 } else { 50 ans = max(ans, cnt); 51 } 52 } 53 54 int main() { 55 int i, j, k; 56 57 #ifndef ONLINE_JUDGE 58 freopen("data.in", "r", stdin); 59 freopen("data.out", "w", stdout); 60 #endif 61 62 while (scanf("%d %d",&n,&m)!=EOF && (n||m)) { 63 if (m == 0) { 64 printf("%d\n", n-1); 65 continue; 66 } 67 init(); 68 for (i=0; i<m; ++i) { 69 scanf("%d %d", &j, &k); 70 vc[j].push_back(k); 71 vc[k].push_back(j); 72 } 73 for (i=0; i<n; ++i) { 74 if (!pre[i]) { 75 ++link; 76 tarjan(i, -1); 77 } 78 } 79 printf("%d\n", ans+link); 80 } 81 82 return 0; 83 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 为DeepSeek添加本地知识库
· 精选4款基于.NET开源、功能强大的通讯调试工具
· DeepSeek智能编程
· 大模型工具KTransformer的安装
· [计算机/硬件/GPU] 显卡
2014-03-22 【HDOJ】1225 Football Score
2014-03-22 【HDOJ】1222 Wolf and Rabbit