This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |