// CEOI15_pipes
// 1st solution (reading input twice)
#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 100100
#define LOGN 22
int n, m;
unordered_set<int> intree;
vector<int> adj[MAXN];
int lpar[LOGN][MAXN], h[MAXN];;
vector<int> dfs_ord;
bool mark[MAXN];
int val[MAXN];
int dpar[MAXN];
int getroot(int x)
{
return dpar[x] == x ? x : dpar[x] = getroot(dpar[x]);
}
bool merge(int x, int y)
{
x = getroot(x);
y = getroot(y);
if (x == y) return false;
dpar[x] = y;
return true;
}
void dfs(int v)
{
mark[v] = true;
for (int u : adj[v]) if (!mark[u])
{
lpar[0][u] = v;
h[u] = h[v] + 1;
dfs(u);
}
dfs_ord.pb(v);
}
int getlca(int x, int y)
{
if (h[y] < h[x]) swap(x, y);
FOR(i, 0, LOGN) if ((h[y] - h[x]) >> i & 1) y = lpar[i][y];
if (x == y) return x;
for (int i = LOGN - 1; i >= 0; i--) if (lpar[i][x] != lpar[i][y])
{
x = lpar[i][x];
y = lpar[i][y];
}
return lpar[0][x];
}
int32_t main(void)
{
fast_io;
cin >> n >> m;
iota(dpar, dpar + n + 10, 0);
FOR(i, 0, m)
{
int x, y;
cin >> x >> y;
if (merge(x, y))
{
intree.insert(i);
adj[x].pb(y);
adj[y].pb(x);
}
}
FOR(i, 1, n + 1)
{
if (!mark[i])
{
dfs(i);
}
}
FOR(i, 1, LOGN) FOR(j, 1, n + 1) lpar[i][j] = lpar[i - 1][lpar[i - 1][j]];
cin.seekg(0);
cin >> n >> m;
FOR(i, 0, m)
{
int x, y;
cin >> x >> y;
if (intree.find(i) != intree.end()) continue;
val[x]++;
val[y]++;
val[getlca(x, y)] -= 2;
}
for (int v : dfs_ord)
{
for (int u : adj[v]) if (u != lpar[0][v]) val[v] += val[u];
if (val[v] == 0 && lpar[0][v]) cout << v << ' ' << lpar[0][v] << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2764 KB |
Output is correct |
2 |
Correct |
3 ms |
2764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
3720 KB |
Output is correct |
2 |
Correct |
10 ms |
3692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
388 ms |
8812 KB |
Output is correct |
2 |
Correct |
330 ms |
8744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
707 ms |
14164 KB |
Output is correct |
2 |
Correct |
748 ms |
15836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1154 ms |
23292 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2194 ms |
36240 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3732 ms |
50452 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5050 ms |
64820 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5025 ms |
65536 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5032 ms |
65536 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |