# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
546559 |
2022-04-07T19:44:58 Z |
Soumya1 |
Pipes (CEOI15_pipes) |
C++17 |
|
1435 ms |
16616 KB |
#include <bits/stdc++.h>
using namespace std;
#ifdef __LOCAL__
#include <debug_local.h>
#endif
const int mxN = 1e5 + 1;
const int lg = 18;
int s[mxN];
int par[mxN][lg];
int p[mxN];
vector<int> ad[mxN];
int d[mxN], sz[mxN];
void dfs(int u, int pp) {
for (int j = 1; j < lg; j++) par[u][j] = par[par[u][j - 1]][j - 1];
for (int v : ad[u]) {
if (v == pp) continue;
d[v] = d[u] + 1;
par[v][0] = u;
dfs(v, u);
}
}
void unite_node(int u, int v) {
if (sz[par[u][lg - 1]] > sz[par[v][lg - 1]]) swap(u, v);
sz[par[v][lg - 1]] += sz[par[u][lg - 1]];
par[u][0] = v;
d[u] = d[v] + 1;
ad[u].push_back(v);
ad[v].push_back(u);
dfs(u, v);
}
int find(int u) {
return p[u] = (u == p[u] ? u : find(p[u]));
}
void unite_component(int u, int v) {
u = find(u);
v = find(v);
if (u == v) return;
if (s[u] > s[v]) swap(u, v);
p[u] = v;
s[v] += s[u];
}
int find_next(int u) {
for (int j = lg - 1; j >= 0; j--) {
if (find(par[u][j]) == find(u)) {
u = par[u][j];
}
}
if (find(u) == find(par[u][0])) return -1;
return par[u][0];
}
int lca(int u, int v) {
if (d[u] > d[v]) swap(u, v);
for (int j = lg - 1; j >= 0; j--) {
if (d[u] <= d[par[v][j]]) v = par[v][j];
}
if (u == v) return u;
for (int j = lg - 1; j >= 0; j--) {
if (par[u][j] != par[v][j]) u = par[u][j], v = par[v][j];
}
return par[u][0];
}
void unite_path(int u, int v) {
int l = lca(u, v);
vector<int> vv;
while (u != l && u != -1) {
vv.push_back(u);
u = find_next(u);
}
if (u != -1) vv.push_back(u);
while (v != -1 && v != l) {
vv.push_back(v);
v = find_next(v);
}
for (int x : vv) unite_component(x, u);
}
void testCase() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
p[i] = i;
s[i] = 1;
for (int j = 0; j < lg; j++) par[i][j] = i;
sz[i] = 1;
}
while (m--) {
int u, v;
cin >> u >> v;
if (par[u][lg - 1] != par[v][lg - 1]) {
unite_node(u, v);
} else if (find(u) != find(v)) {
unite_path(u, v);
}
}
set<pair<int, int>> st;
for (int i = 1; i <= n; i++) {
for (int j : ad[i]) {
if (find(i) != find(j)) {
st.insert({min(i, j), max(i, j)});
}
}
}
for (auto [a, b] : st) cout << a << " " << b << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
testCase();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
3284 KB |
Output is correct |
2 |
Incorrect |
8 ms |
3260 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
98 ms |
5564 KB |
Output is correct |
2 |
Incorrect |
107 ms |
5676 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
172 ms |
5864 KB |
Output is correct |
2 |
Incorrect |
185 ms |
5780 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
295 ms |
7396 KB |
Output is correct |
2 |
Correct |
279 ms |
8392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
488 ms |
11136 KB |
Output is correct |
2 |
Incorrect |
393 ms |
11008 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
808 ms |
14604 KB |
Output is correct |
2 |
Correct |
604 ms |
12492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
996 ms |
15024 KB |
Output is correct |
2 |
Incorrect |
901 ms |
14872 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1237 ms |
15116 KB |
Output is correct |
2 |
Incorrect |
1263 ms |
15096 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1435 ms |
14660 KB |
Output is correct |
2 |
Runtime error |
1204 ms |
16616 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |