#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 y[N];
bitset<N> visited;
pair<unsigned, unsigned> find_bridges(unsigned u, unsigned p, unsigned i)
{
unsigned l = y[u] = i++;
visited[u] = 1;
for (unsigned const &v : g[u])
{
if (!visited[v])
{
unsigned lv;
tie(i, lv) = find_bridges(v, u, i);
l = min(l, lv);
if (lv > y[u])
printf("%u %u\n", u + 1, v + 1);
}
else if (v != p)
l = min(l, y[v]);
}
return make_pair(i, l);
}
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);
}
for (unsigned i = 0; i < n; i++)
if (!visited[i])
find_bridges(i, UINT_MAX, 0);
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:62:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%zu %zu", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~~~
pipes.cpp:69:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%u %u", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
Correct |
4 ms |
3028 KB |
Output is correct |
2 |
Incorrect |
5 ms |
2900 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
2936 KB |
Output is correct |
2 |
Incorrect |
111 ms |
2796 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
208 ms |
3608 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
317 ms |
5188 KB |
Output is correct |
2 |
Correct |
248 ms |
4936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
383 ms |
10244 KB |
Output is correct |
2 |
Incorrect |
401 ms |
6780 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
601 ms |
11492 KB |
Output is correct |
2 |
Incorrect |
604 ms |
8588 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
812 ms |
13572 KB |
Output is correct |
2 |
Incorrect |
748 ms |
8712 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
988 ms |
13580 KB |
Output is correct |
2 |
Incorrect |
936 ms |
8592 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1220 ms |
12940 KB |
Output is correct |
2 |
Correct |
1168 ms |
10092 KB |
Output is correct |