#include <bits/stdc++.h>
#define X first
#define Y second
using namespace std;
typedef long long llint;
const int maxn = 1e5+10;
const int base = 31337;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const int logo = 18;
const int off = 1 << logo;
const int treesiz = off << 1;
struct union_find {
int parr[maxn];
union_find() {
for (int i = 0; i < maxn; i++) parr[i] = i;
}
int fin(int x) {
if (x == parr[x]) return x;
return parr[x] = fin(parr[x]);
}
void merg(int a, int b) {
a = fin(a);
b = fin(b);
if (a == b) return;
int ra = rand() % 2;
if (ra == 0) parr[b] = a;
else parr[a] = b;
}
};
int n, m;
vector< int > graph[maxn];
union_find p, q;
int t = 0;
int dis[maxn], lo[maxn];
void dfs(int x, int parr) {
dis[x] = t++;
lo[x] = dis[x];
//printf("start %d %d\n", x, dis[x]);
bool flag = false;
for (int tren : graph[x]) {
if (tren == parr && !flag) {
flag = true;
continue;
}
if (dis[tren] == -1) {
dfs(tren, x);
if (lo[tren] > dis[x]) printf("%d %d\n", x, tren);
lo[x] = min(lo[x], lo[tren]);
} else {
lo[x] = min(lo[x], dis[tren]);
}
}
//printf("end %d: %d %d\n", x, dis[x], lo[x]);
}
int main() {
srand(time(0));
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
if (p.fin(a) != p.fin(b)) {
graph[a].push_back(b);
graph[b].push_back(a);
p.merg(a, b);
} else if (q.fin(a) != q.fin(b)) {
graph[a].push_back(b);
graph[b].push_back(a);
q.merg(a, b);
}
}
memset(dis, -1, sizeof dis);
for (int i = 1; i <= n; i++) {
if (dis[i] == -1) dfs(i, 0);
}
return 0;
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:69:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:72:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3788 KB |
Output is correct |
2 |
Correct |
2 ms |
3788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4172 KB |
Output is correct |
2 |
Correct |
7 ms |
4044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
4068 KB |
Output is correct |
2 |
Correct |
133 ms |
3912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
228 ms |
4700 KB |
Output is correct |
2 |
Correct |
270 ms |
4176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
378 ms |
6168 KB |
Output is correct |
2 |
Runtime error |
340 ms |
19624 KB |
Memory limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
526 ms |
10848 KB |
Output is correct |
2 |
Runtime error |
465 ms |
25636 KB |
Memory limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
769 ms |
11888 KB |
Output is correct |
2 |
Correct |
819 ms |
15508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1057 ms |
13948 KB |
Output is correct |
2 |
Runtime error |
1006 ms |
50456 KB |
Memory limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1314 ms |
13952 KB |
Output is correct |
2 |
Runtime error |
1296 ms |
61992 KB |
Memory limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1554 ms |
13408 KB |
Output is correct |
2 |
Runtime error |
1611 ms |
65536 KB |
Memory limit exceeded |