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
| #include <bits/stdc++.h> #define ios ios::sync_with_stdio(0),cin.tie(0); #define int long long #define N 1000001 using namespace std; int n,m,lv[N],low[N],timestamp = 1; bool visit[N]; vector<int> edge[N]; vector<pair<int, int>> ans; set<pair<int, int>>s;
void DFS(int now,int father){ lv[now] = low[now] = timestamp++; visit[now] = 1; int len = edge[now].size(); for(int i=0;i<len;i++){ int next = edge[now][i]; if(!visit[next]){ DFS(next, now); if(low[next] > lv[now]){ if(next<now)s.insert(make_pair(next,now)); else s.insert(make_pair(now, next)); } } if(next!=father)low[now] = min(low[now],low[next]); } }
signed main(){ ios; cin>>n>>m; for(int i=0;i<m;i++){ int x,y;cin>>x>>y; edge[x].push_back(y); edge[y].push_back(x); ans.push_back(make_pair(x, y)); } memset(visit, 0, sizeof(visit)); for(int i=1;i<=n;i++){ if(!visit[i]){ DFS(i, i); } } for(int i=0;i<m;i++){ pair<int, int> temp = ans[i]; if(s.find(temp)!=s.end()){ cout<<temp.first<<" "<<temp.second<<endl; } } }
|