#include <bits/stdc++.h>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
#define ar array
#define ll long long
#define ld long double
#define sze(x) ((ll)x.size())
#define all(a) (a).begin(), (a).end()
#define sui cout.tie(NULL); cin.tie(NULL); ios_base::sync_with_stdio(false)
#define mset(a, x) memset(a, x, sizeof(a))
typedef priority_queue <ll, vector<ll>, greater<ll> > max_heap;
typedef priority_queue <ll> min_heap;
const ll MAX_N = 1e5 + 1;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const ld EPS = 1e-9;
ll par[2][MAX_N];
vector<pair<ll, ll>> adj[MAX_N];
ll getpar(ll u, ll k)
{
return (par[k][u] == u ? u : par[k][u] = getpar(par[k][u], k));
}
bool merge(ll u, ll v, ll k)
{
u = getpar(u, k), v = getpar(v, k);
if (u == v) return false;
par[k][v] = u;
return true;
}
ll dp[MAX_N];
ll he[MAX_N];
void dfs(ll u, ll h, ll p, ll p2)
{
he[u] = h;
dp[u] = INF;
for (pair<ll, ll> v : adj[u])
{
if (v.second == p) continue;
if (he[v.first]==-1) dfs(v.first, h + 1, v.second, u), dp[u] = min(dp[v.first], dp[u]);
else dp[u] = min(dp[u], min(he[v.first], h));
}
if (dp[u] >= h && p2 != -1) cout << u << " " << p2 << endl;
}
void solve() {
ll n, m;
cin >> n >> m;
for (ll i = 1; i <= n; i++)
{
par[0][i] = par[1][i] = i;
dp[i] = INF;
he[i] = -1;
adj[i].clear();
}
ll cnt = 0;
for (ll i = 0; i < m; i++)
{
ll a, b;
cin >> a >> b;
if (merge(a, b, 0)) cnt++, adj[a].push_back(make_pair(b, cnt)), adj[b].push_back(make_pair(a, cnt));
else if (merge(a, b, 1)) cnt++, adj[a].push_back(make_pair(b, cnt)), adj[b].push_back(make_pair(a, cnt));
}
for (ll i = 1; i <= n; i++)
{
if (he[i]==-1) dfs(i, 0, -1, -1);
}
}
int main() {
sui;
ll tc = 1;
// cin >> tc;
for (ll t = 1; t <= tc; t++) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5212 KB |
Output is correct |
2 |
Correct |
4 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
4960 KB |
Output is correct |
2 |
Correct |
62 ms |
4952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
6184 KB |
Output is correct |
2 |
Correct |
125 ms |
5652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
186 ms |
8836 KB |
Output is correct |
2 |
Correct |
173 ms |
8280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
259 ms |
17024 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
433 ms |
19240 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
529 ms |
22448 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
688 ms |
22444 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
801 ms |
21356 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |