RT,CSES-1756
我用的 DFS 树的解法,但不知道为什么会挂掉,求调
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#define ll long long
#define fp(a,b,c) for(ll a=b;a<=c;a++)
#define fd(a,b,c) for(ll a=b;a>=c;a--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fr first
#define sd second
#define mod 1000000007
#define inf 0x3f3f3f3f
using namespace std;
inline int rd(){
int x = 0, f = 1;char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-')f = -1;ch = getchar();}
while(ch >= '0' && ch <= '9')x = (x<<1) + (x<<3) + (ch^48),ch = getchar();
return x * f;}
inline ll lrd(){
ll x = 0, f = 1;char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-')f = -1;ch = getchar();}
while(ch >= '0' && ch <= '9')x = (x<<1) + (x<<3) + (ch^48),ch = getchar();
return x * f;}
const int maxN=2*1e5+10;
int n,m;
vector<int>g[maxN];
int dep[maxN],fa[maxN];
inline void dfs(int now,int f){
fa[now]=f,dep[now]=dep[f]+1;
for(int x:g[now]){
if(x==f)continue;
if(!dep[x])dfs(x,now),cout << now << " " << x << endl;
else if(dep[x]<=dep[now]){
cout << x << ' ' << now << endl;
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
n=rd(),m=rd();
fp(i,1,m){
int u=rd(),v=rd();
g[u].push_back(v),g[v].push_back(u);
}
fp(i,1,n)
if(!dep[i]) dfs(i,i);
return 0;
}