#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;
}
};
constexpr size_t N = 100000;
vector<unsigned> g[N];
unsigned y[N];
pair<unsigned, unsigned> find_bridges(unsigned u, unsigned p, unsigned i)
{
unsigned lu = y[u] = ++i;
bool parent_seen = 0;
for (unsigned const &v : g[u])
{
if (!y[v])
{
unsigned lv;
tie(i, lv) = find_bridges(v, u, i);
lu = min(lu, lv);
if (lv > y[u])
printf("%u %u\n", u + 1, v + 1);
}
else if (v != p || parent_seen)
lu = min(lu, y[v]);
parent_seen = parent_seen || v == p;
}
return make_pair(i, lu);
}
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.merge(u, v) || d2.merge(u, v))
{
g[u].push_back(v);
g[v].push_back(u);
}
}
for (unsigned i = 0; i < n; i++)
if (!y[i])
find_bridges(i, UINT_MAX, 0);
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:58:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%zu %zu", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~~~
pipes.cpp:65:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | scanf("%u %u", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3164 KB |
Output is correct |
2 |
Correct |
5 ms |
3064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
3152 KB |
Output is correct |
2 |
Correct |
106 ms |
2932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
126 ms |
3760 KB |
Output is correct |
2 |
Correct |
137 ms |
3224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
210 ms |
5208 KB |
Output is correct |
2 |
Correct |
167 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
317 ms |
10328 KB |
Output is correct |
2 |
Runtime error |
246 ms |
25040 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
422 ms |
11500 KB |
Output is correct |
2 |
Runtime error |
413 ms |
42068 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
541 ms |
13716 KB |
Output is correct |
2 |
Runtime error |
576 ms |
49972 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
658 ms |
13648 KB |
Output is correct |
2 |
Runtime error |
743 ms |
61604 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
863 ms |
13140 KB |
Output is correct |
2 |
Runtime error |
878 ms |
65536 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |