输出图中的边,这条边两边的点只能通过这条边到达。
也就是说,所有在环里的边都不是答案。
所以我们只要把环缩成点,剩下的边就是答案。
无向图环的缩点,求它的边-双连通分量就行。
注意最后的答案要排序输出。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#pragma comment(linker, "/STACK:102400000,102400000")
#include
#include
#include
//#include
//#include
using namespace std;
//using namespace __gnu_pbds;
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define X first
#define Y second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define FOR(i, a, b) for (int i=(a); (i) < (b); (i)++)
#define FOOR(i, a, b) for (int i = (a); (i)<=(b); (i)++)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-4;
const int MAXN = 1e4+10;
const int MOD = 8e4+7;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int seed = 131;
int cases = 0;
typedef pair<int, int> pii;
struct EDGE
{
int v, used, nxt;
};
vector ans;
vector<int> G[MAXN];
vector edge;
set mp;
int dfs_clock, bcc_cnt, n;
int bccno[MAXN], time_stamp[MAXN], low[MAXN], head[MAXN];
stack<int> stk;
void add_edge(int u, int v)
{
edge.PB((EDGE){v, 0, head[u]});
head[u] = SZ(edge)-1;
}
void dfs(int u)
{
time_stamp[u] = low[u] = ++dfs_clock;
stk.push(u);
for (int i = head[u]; i != -1; i = edge[i].nxt)
{
EDGE &e = edge[i];
int v = e.v;
if (e.used) continue;
edge[i].used = edge[i^1].used = 1;
if (!time_stamp[v])
{
dfs(v);
low[u] = min(low[u], low[v]);
}
else if (!bccno[v]) low[u] = min(low[u], time_stamp[v]);
}
if (time_stamp[u] == low[u])
{
bcc_cnt++;
while (true)
{
int x = stk.top(); stk.pop();
bccno[x] = bcc_cnt;
if (x == u) break;
}
}
}
void Solve()
{
MS(time_stamp, 0); MS(bccno, 0);
bcc_cnt = dfs_clock = 0;
for (int i = 0; i < n; i++) if (!bccno[i])
dfs(i);
for (int i = 0; i < n; i++)
for (int j = head[i]; j != -1; j = edge[j].nxt)
{
int v = edge[j].v;
if (bccno[i] != bccno[v] && !mp.count(MP(v, i)))
{
ans.PB(MP(i, v));
mp.insert(MP(i, v));
}
}
printf("Case %d:\n", ++cases);
printf("%d critical links\n", SZ(ans));
sort(ans.begin(), ans.end());
for (int i = 0; i < SZ(ans); i++) printf("%d - %d\n", ans[i].X, ans[i].Y);
}
int main()
{
//ROP;
int T;
scanf("%d", &T);
while (T--)
{
ans.clear(); MS(head, -1); edge.clear(); mp.clear();
scanf("%d", &n);
for (int i = 0; i <= n; i++) G[i].clear();
for (int i = 0; i < n; i++)
{
char m[10];
int a, num;
scanf("%d", &a);
scanf("%s", m);
sscanf(m+1, "%d", &num);
while (num--)
{
int b;
scanf("%d", &b);
if (b > a)
{
add_edge(a, b);
add_edge(b, a);
}
}
}
Solve();
}
return 0;
}