#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) ((int)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 <int, vector<int>, greater<int> > max_heap;
typedef priority_queue <int> min_heap;
const int MAX_N = 1e5 + 5;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const ld EPS = 1e-9;
int par[2][MAX_N];
int sz[2][MAX_N];
vector<int> adj[MAX_N];
int getpar(int u, int k)
{
return (par[k][u] == u ? u : getpar(par[k][u], k));
}
bool merge(int u, int v, int k)
{
u = getpar(u, k), v = getpar(v, k);
if (u == v) return false;
if (sz[k][u] < sz[k][v]) swap(u, v);
par[k][v] = u;
sz[k][u] += sz[k][v];
return true;
}
bool mark[MAX_N];
int dp[MAX_N];
int he[MAX_N];
void dfs(int u, int h, int p)
{
mark[u] = true;
he[u] = h;
dp[u] = INF;
for (int v : adj[u])
{
if (!mark[v]) dfs(v, h + 1, u), dp[u] = min(dp[v], dp[u]);
else if (he[v] < h - 1) dp[u] = min(dp[u], he[v]);
}
if (dp[u] >= h && p != -1) cout << u << " " << p << endl;
}
void solve() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
par[0][i] = par[1][i] = i;
sz[0][i] = sz[1][i] = 1;
mark[i] = false;
dp[i] = INF;
he[i] = INF;
adj[i].clear();
}
for (int i = 0; i < m; i++)
{
int a, b;
cin >> a >> b;
bool ok = merge(a, b, 0);
if (ok) adj[a].push_back(b), adj[b].push_back(a);
else if (merge(a, b, 1)) adj[a].push_back(b), adj[b].push_back(a);
}
for (int i = 1; i <= n; i++)
{
if (!mark[i]) dfs(i, 0, -1);
}
}
int main() {
sui;
int tc = 1;
// cin >> tc;
for (int t = 1; t <= tc; t++) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4444 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4952 KB |
Output is correct |
2 |
Incorrect |
4 ms |
4700 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
10224 KB |
Output is correct |
2 |
Incorrect |
89 ms |
9948 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
152 ms |
14932 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
254 ms |
22908 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
389 ms |
31744 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
639 ms |
45136 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
734 ms |
57860 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
910 ms |
65536 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1121 ms |
65536 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |