-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tarjan-割边(有重边)-编号.cpp
57 lines (55 loc) · 1.39 KB
/
Tarjan-割边(有重边)-编号.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4;
struct node{
int v,id;
};
vector<node> g[N+10];
int dfn[N+10],dfs_clock,cntCut;
int n,m,i,a,b,t;
int dfs(int u, int id){
int lowu = dfn[u] = ++dfs_clock;
int lowv,i,v;
for(i=0; i<g[u].size();++i){
v = g[u][i].v;
if(!dfn[v]){
lowv = dfs(v,g[u][i].id);
lowu = min(lowu,lowv);
if(lowv > dfn[u])
cntCut++;
}
else if(id/2 != g[u][i].id/2 &&dfn[v]<lowu)//如果刚才走过来的边的id/2和这个边的id/2不相等,那么说明这两条有向边不是从一条边分离出来的
lowu = dfn[v];
}
return lowu;
}
void init(){
for(int i = 1;i <= n;++i){
g[i].clear();
}
memset(dfn,0,sizeof(dfn)); dfs_clock = 0;cntCut = 0;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
init();
for(i=0; i<m; ++i){
scanf("%d%d",&a,&b);
node t;
t.v = a;
t.id = i * 2;//给边编号
g[b].push_back(t);
t.v = b;
t.id = i * 2 + 1;
g[a].push_back(t);
}
for(int i = 1;i <= n;++i){
if(dfn[i] == 0){
dfs(i,-2);
}
}
printf("%d\n",cntCut);
}
return 0;
}