#include <bits/stdc++.h>
using namespace std;
//#define FILE_IO
typedef pair<int, int> pii;
int N, M;
int h[100005], low[100005], vis[100005];
vector<int> edg[100005];
class UnionFind
{
public:
int N;
int f[100005];
void init(int _N)
{
N = _N;
for(int i = 1; i <= N; i++) f[i] = i;
}
int F(int x)
{
if(f[x] == x) return x;
return f[x] = F(f[x]);
}
void unite(int x, int y)
{
if(F(x) == F(y)) return;
f[F(y)] = F(x);
}
};
UnionFind f[2];
void DFS(int nod, int fth)
{
h[nod] = h[fth] + 1;
low[nod] = h[nod];
vis[nod] = 1;
int father = 0;
for(auto nxt: edg[nod])
{
if(nxt == fth)
{
father++;
if(father == 1) continue;
}
if(vis[nxt])
{
low[nod] = min(low[nod], low[nxt]);
continue;
}
DFS(nxt, nod);
low[nod] = min(low[nod], low[nxt]);
if(low[nxt] > h[nod])
printf("%d %d\n", nod, nxt);
}
}
int gint()
{
char ch = getchar();
while(ch < '0' || '9' < ch) ch = getchar();
int x = 0;
while('0' <= ch && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x;
}
int main()
{
#ifdef FILE_IO
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
//scanf("%d%d", &N, &M);
N = gint(); M = gint();
f[0].init(N), f[1].init(N);
int cnt = 0;
for(int i = 1; i <= M; i++)
{
int x, y;
//scanf("%d%d", &x, &y);
x = gint(); y = gint();
if(f[1].F(x) == f[1].F(y)) continue;
edg[x].push_back(y);
edg[y].push_back(x);
if(f[0].F(x) != f[0].F(y)) f[0].unite(x, y);
else f[1].unite(x, y);
}
for(int i = 1; i <= N; i++)
if(!vis[i])
DFS(i, 0);
return 0;
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:92:9: warning: unused variable 'cnt' [-Wunused-variable]
int cnt = 0;
^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2688 KB |
Output is correct |
2 |
Correct |
4 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3200 KB |
Output is correct |
2 |
Correct |
6 ms |
3060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
3020 KB |
Output is correct |
2 |
Correct |
49 ms |
2940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
3704 KB |
Output is correct |
2 |
Correct |
102 ms |
3420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
154 ms |
5276 KB |
Output is correct |
2 |
Correct |
138 ms |
4984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
263 ms |
10168 KB |
Output is correct |
2 |
Correct |
211 ms |
7372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
365 ms |
11432 KB |
Output is correct |
2 |
Correct |
342 ms |
8892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
475 ms |
13432 KB |
Output is correct |
2 |
Correct |
433 ms |
9336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
561 ms |
13508 KB |
Output is correct |
2 |
Correct |
521 ms |
9336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
651 ms |
12824 KB |
Output is correct |
2 |
Correct |
606 ms |
10632 KB |
Output is correct |