记录编号 | 42850 | 评测结果 | AAAAAAAAAA | ||
---|---|---|---|---|---|
题目名称 | 备用交换机 | 最终得分 | 100 | ||
用户昵称 | 是否通过 | 通过 | |||
代码语言 | C++ | 运行时间 | 0.002 s | ||
提交时间 | 2012-10-01 10:56:19 | 内存使用 | 0.32 MiB | ||
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <vector> #include <stack> using namespace std; const int kMaxN = 100 + 10; int n; vector<int> graph[kMaxN]; int tarjan_count; int tarjan_dfn[kMaxN]; int tarjan_low[kMaxN]; stack<int> tarjan_stack; bool tarjan_instack[kMaxN]; int root; int cut_vertex[kMaxN]; void Tarjan(int v, int from) { tarjan_count++; tarjan_dfn[v] = tarjan_count; tarjan_low[v] = tarjan_count; tarjan_instack[v] = true; tarjan_stack.push(v); for (int i = 0; i < (int)graph[v].size(); i++) { int w = graph[v][i]; if (w == from) continue; if (!tarjan_dfn[w]) { Tarjan(w, v); tarjan_low[v] = min(tarjan_low[v], tarjan_low[w]); if (tarjan_dfn[v] <= tarjan_low[w]) { if (v == root) { if ((int)graph[v].size() >= 2 && tarjan_dfn[v] < tarjan_low[w]) { cut_vertex[v] = true; } } else { cut_vertex[v] = true; } } } else if (tarjan_instack[w]) { tarjan_low[v] = min(tarjan_low[v], tarjan_dfn[w]); } } if (tarjan_dfn[v] == tarjan_low[v]) { int t; do { t = tarjan_stack.top(); tarjan_stack.pop(); tarjan_instack[t] = false; } while (t != v); } } void Work() { tarjan_count = 0; for (int i = 1; i <= 1; i++) { if (!tarjan_dfn[i]) { root = i; Tarjan(i, -1); } } int cnt = 0; for (int i = 1; i <= n; i++) { if (cut_vertex[i]) { cnt++; } } if (cnt == 0) { printf("0\n"); } else { printf("%d\n", cnt); for (int i = 1; i <= n; i++) { if (cut_vertex[i]) { printf("%d\n", i); } } } } int main() { freopen("gd.in", "r", stdin); freopen("gd.out", "w", stdout); scanf("%d", &n); int a, b; while (scanf("%d%d", &a, &b) != EOF) { graph[a].push_back(b); graph[b].push_back(a); } Work(); return 0; }