#include <bits/stdc++.h>
using namespace std;
struct Dsu
{
vector<int> p;
Dsu(size_t n) { p = vector<int>(n, -1); }
int repr(int u) { return p[u] < 0 ? u : p[u] = repr(p[u]); }
bool merge(int i, int j)
{
i = repr(i);
j = repr(j);
if (i == j)
return 0;
if (p[i] > p[j])
swap(i, j);
p[i] += p[j];
p[j] = i;
return 1;
}
bool same_set(int i, int j) { return repr(i) == repr(j); }
int set_size(int i) { return -p[repr(i)]; }
};
constexpr size_t N = 100000;
vector<unsigned> g[N];
unsigned l[N], y[N];
bitset<N> visited;
unsigned find_bridges(unsigned u, unsigned p, unsigned i)
{
l[u] = y[u] = i++;
visited[u] = 1;
for (unsigned const &v : g[u])
{
if (!visited[v])
{
i = find_bridges(v, u, i);
l[u] = min(l[u], l[v]);
if (l[v] > y[u])
printf("%u %u\n", u + 1, v + 1);
}
else if (v != p)
l[u] = min(l[u], y[v]);
}
return i;
}
int main()
{
size_t n, m;
scanf("%zu %zu", &n, &m);
Dsu d1(n), d2(n);
while (m--)
{
unsigned u, v;
scanf("%u %u", &u, &v);
u--, v--;
if (!d1.same_set(u, v))
d1.merge(u, v), g[u].push_back(v), g[v].push_back(u);
else if (!d2.same_set(u, v))
d2.merge(u, v), g[u].push_back(v), g[v].push_back(u);
}
find_bridges(0, UINT_MAX, 0);
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:61:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | scanf("%zu %zu", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~~~
pipes.cpp:68:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | scanf("%u %u", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
3108 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
3800 KB |
Output is correct |
2 |
Incorrect |
109 ms |
3588 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
194 ms |
4428 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
306 ms |
5948 KB |
Output is correct |
2 |
Correct |
269 ms |
8384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
398 ms |
10180 KB |
Output is correct |
2 |
Incorrect |
353 ms |
7572 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
711 ms |
11764 KB |
Output is correct |
2 |
Incorrect |
597 ms |
9488 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
830 ms |
13668 KB |
Output is correct |
2 |
Incorrect |
762 ms |
10032 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1028 ms |
13928 KB |
Output is correct |
2 |
Incorrect |
1014 ms |
10424 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1282 ms |
19388 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |