// CEOI15_pipes
// 2nd solution
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e9 + 100;
const ll LINF = 1e18 + 100;
#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define endl '\n'
#define MAXN 100010
int n, m;
vector<int> adj[MAXN];
bool mark[MAXN];
int val[MAXN], h[MAXN];
int tree[MAXN], be[MAXN];
int getroot(int x, int *dpar)
{
return dpar[x] == x ? x : dpar[x] = getroot(dpar[x], dpar);
}
bool merge(int x, int y, int *dpar)
{
x = getroot(x, dpar);
y = getroot(y, dpar);
if (x == y) return false;
dpar[x] = y;
return true;
}
void dfs1(int v, int par = 0)
{
mark[v] = true;
bool vispar = false;
for (int u : adj[v])
{
if (mark[u])
{
if (h[u] < h[v] - 1 || (u == par && vispar)) val[u]--, val[v]++;
if (u == par) vispar = true;
continue;
}
h[u] = h[v] + 1;
dfs1(u, v);
}
}
void dfs2(int v, int par = 0)
{
mark[v] = true;
for (int u : adj[v])
if (!mark[u]) dfs2(u, v), val[v] += val[u];
if (par && val[v] == 0) cout << v << ' ' << par << endl;
}
int32_t main(void)
{
fast_io;
cin >> n >> m;
iota(tree, tree + n + 5, 0);
iota(be, be + n + 5, 0);
FOR(i, 0, m)
{
int x, y;
cin >> x >> y;
if (merge(x, y, tree) || merge(x, y, be))
{
adj[x].pb(y);
adj[y].pb(x);
}
}
FOR(i, 1, n + 1) if (!mark[i]) dfs1(i);
memset(mark, 0, sizeof(mark));
FOR(i, 1, n + 1) if (!mark[i]) dfs2(i);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2764 KB |
Output is correct |
2 |
Correct |
2 ms |
2764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3276 KB |
Output is correct |
2 |
Correct |
6 ms |
3020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
3088 KB |
Output is correct |
2 |
Correct |
122 ms |
2960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
202 ms |
3764 KB |
Output is correct |
2 |
Correct |
243 ms |
3364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
342 ms |
5336 KB |
Output is correct |
2 |
Correct |
281 ms |
5036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
455 ms |
10676 KB |
Output is correct |
2 |
Correct |
412 ms |
7164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
714 ms |
11908 KB |
Output is correct |
2 |
Correct |
677 ms |
8912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
965 ms |
14048 KB |
Output is correct |
2 |
Correct |
879 ms |
9232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1156 ms |
13960 KB |
Output is correct |
2 |
Correct |
1083 ms |
9112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1387 ms |
13448 KB |
Output is correct |
2 |
Correct |
1298 ms |
10728 KB |
Output is correct |