-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbridge.cpp
More file actions
98 lines (80 loc) · 1.78 KB
/
Copy pathbridge.cpp
File metadata and controls
98 lines (80 loc) · 1.78 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
Problem: Real Life Traffic (Light Oj)
Problem Link: http://lightoj.com/volume_showproblem.php?problem=1291
*/
#include<bits/stdc++.h>
using namespace std;
const int MX=10005;
vector<int> g[MX];
vector<pair<int, int> > E;
int vis[MX], low[MX], dis[MX];
int f[MX], component[MX], Time;
map<pair<int, int>, int>mp;
void CLEAR(int n)
{
Time=0; mp.clear(); E.clear();
for(int i=0;i<=n;i++)
{
vis[i]=low[i]=dis[i]=f[i]=0;
g[i].clear();
}
}
void dfs(int u, int p)
{
low[u]=dis[u]=++Time;
for(auto v:g[u])
{
if(v==p) continue;
if(low[v]==0)
{
dfs(v,u);
if(dis[u]<low[v])
{
mp[{u,v}]=mp[{v,u}]=1; E.push_back({u,v});
}
low[u]=min(low[u], low[v]);
}
else
{
low[u]=min(low[u], dis[v]);
}
}
}
void dfs2(int u, int id)
{
vis[u]=1; component[u]=id;
for(auto v:g[u])
{
if(vis[v] ||mp.count({u, v}) )continue;
dfs2(v, id);
}
}
int main()
{
int tc, cs=1; scanf("%d", &tc);
while(tc--)
{
int n, m; scanf("%d%d", &n, &m); CLEAR(n);
for(int i=1; i<=m; i++)
{
int x, y; scanf("%d%d", &x, &y);
g[x].push_back(y), g[y].push_back(x);
}
dfs(0, 0);
int id=0;
for(int i=0; i<n; i++)
{
if(!vis[i]) dfs2(i, id++);
}
for(auto p:E)
{
f[component[p.first]]++;
f[component[p.second]]++;
}
int ans=0;
for(int i=0;i<n;i++) if(f[i]==1) ans++;
ans=(ans+1)/2;
printf("Case %d: %d\n",cs++,ans);
}
return 0;
}