Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
- #define ll long long
- #define ld long double
- #define pb push_back
- #define fe first
- #define se second
- #define nl "\n"
- #define pp pair < ll , ll >
- #define sz(x) (ll)x.size()
- #define st(x) sort(x.begin(),x.end())
- #define rst(x) sort(x.rbegin(), x.rend())
- #define all(x) x.begin(),x.end()
- long double pi = 3.14159265358979323;
- const double EPS = 1e-12;
- const int N = 1e6 + 5;
- //Just A Gentle Reminder about MOD
- const int mod = 1e9 + 7;
- //Articulation point
- //Two Condn
- //1 - if u is root node
- //it must have two children components
- //2 - not a root
- //there should not a back edge from subgraph of v to ancestor of u
- vector < ll > v[N];
- //vector < ll > ans;
- int tin[N];
- int low[N]; //earliest node which is back edge
- int vis[N];
- vector < pair < int , int > > ans;
- ll par[N];
- ll timer;
- void dfs(ll s , ll timer){
- int cnt = 0 ;
- timer++;
- tin[s] = timer;
- low[s] = timer;
- vis[s] = 1;
- for(auto it : v[s]){
- if(!vis[it]){
- par[it] = s;
- cnt++;
- dfs(it,timer);
- low[s] = min(low[it] , low[s]);
- //if low[it] < tin[s] back edge is present!!
- //condn for no back edge
- if(low[it] > tin[s]){
- ans.push_back({s,it});
- }
- }
- //FOR BACK EDGE
- else if(it != par[s]){
- low[s] = min(tin[it] , low[s]);
- }
- }
- //vis[s] = 2;
- }
- int main()
- {
- fast;
- ll t;
- cin >> t ;
- ll n , m;
- while(t--){
- cin >> n >> m ;
- for(int i = 0 ; i <= n ;i++ ){
- v[i].clear();
- vis[i] = tin[i] = low[i] = par[i] = 0;
- }
- ans.clear();
- timer = 0;
- for(int i = 0 ; i < m ;i++ ){
- ll a , b;
- cin >> a >> b ;
- //cout << a << " " << b << "\n";
- v[a].pb(b);
- v[b].pb(a);
- }
- // for(int i = 1 ; i <= n; i++ ){
- // if(!vis[i]){
- // dfs(i,0);
- // }
- // }
- dfs(1,0);
- for(auto it : ans){
- cout << it.first << " " << it.second << " ";
- }
- }
- return 0;
- //It's not end it yet
- }
Advertisement
Add Comment
Please, Sign In to add comment