# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
716991 |
2023-03-31T17:25:48 Z |
stevancv |
Pipes (CEOI15_pipes) |
C++14 |
|
2377 ms |
13612 KB |
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
const int inf = 1e9;
struct Dsu {
int link[N], sz[N];
void Reset(int n) {
for (int i = 1; i <= n; i++) {
link[i] = i;
sz[i] = 1;
}
}
int Find(int x) {
if (x == link[x]) return x;
return link[x] = Find(link[x]);
}
int GetSz(int x) {
return sz[Find(x)];
}
void Unite(int u, int v) {
u = Find(u); v = Find(v);
if (u == v) return;
if (sz[u] < sz[v]) swap(u, v);
link[v] = u;
sz[u] += sz[v];
}
}dsu;
vector<int> g[N];
int par[N][16], dep[N], su[N];
void Dfs(int s, int e) {
par[s][0] = e;
dep[s] = dep[e] + 1;
for (int i = 1; i < 16; i++) par[s][i] = par[par[s][i - 1]][i - 1];
for (int u : g[s]) {
if (u == e) continue;
Dfs(u, s);
}
}
int Lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
int r = dep[u] - dep[v];
for (int i = 0; i < 16; i++) {
if ((1 << i) & r) u = par[u][i];
}
if (u == v) return u;
for (int i = 15; i >= 0; i--) {
if (par[u][i] != par[v][i]) {
u = par[u][i];
v = par[v][i];
}
}
return par[u][0];
}
int Solve(int s, int e) {
for (int u : g[s]) {
if (u == e) continue;
int x = Solve(u, s);
if (x == 0) cout << s << sp << u << en;
su[s] += x;
}
return su[s];
}
void Subtask1(int n, int m) {
vector<int> u(m), v(m);
for (int i = 0; i < m; i++) {
cin >> u[i] >> v[i];
}
for (int i = 0; i < m; i++) {
dsu.Reset(n);
for (int j = 0; j < m; j++) {
if (i == j) continue;
if (dsu.Find(u[j]) != dsu.Find(v[j])) dsu.Unite(u[j], v[j]);
}
if (dsu.Find(u[i]) != dsu.Find(v[i])) cout << u[i] << sp << v[i] << en;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
if (n <= 100 && m <= 200) {
Subtask1(n, m);
return 0;
}
dsu.Reset(n);
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
if (dsu.Find(u) == dsu.Find(v)) {
su[u] += 1;
su[v] += 1;
su[Lca(u, v)] -= 2;
continue;
}
if (dsu.GetSz(u) < dsu.GetSz(v)) swap(u, v);
dsu.Unite(u, v);
Dfs(v, u);
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
if (dep[i] == 0) Solve(i, 0);
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3156 KB |
Output is correct |
2 |
Correct |
6 ms |
3156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
151 ms |
3060 KB |
Output is correct |
2 |
Correct |
138 ms |
3048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
251 ms |
3724 KB |
Output is correct |
2 |
Correct |
333 ms |
3788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
440 ms |
5268 KB |
Output is correct |
2 |
Correct |
452 ms |
5888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
675 ms |
10324 KB |
Output is correct |
2 |
Correct |
567 ms |
10304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1110 ms |
11472 KB |
Output is correct |
2 |
Correct |
1040 ms |
11460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1576 ms |
13568 KB |
Output is correct |
2 |
Correct |
1619 ms |
13564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2023 ms |
13512 KB |
Output is correct |
2 |
Correct |
1985 ms |
13568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2377 ms |
12968 KB |
Output is correct |
2 |
Correct |
2107 ms |
13612 KB |
Output is correct |