# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
44868 | bogdan10bos | Pipes (CEOI15_pipes) | C++14 | 710 ms | 65536 KiB |
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 K, vis[100005], lst[100005];
vector<int> nodes, edg[100005];
vector<pii> critical;
queue<pii> q;
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];
int main()
{
#ifdef FILE_IO
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
scanf("%d%d", &N, &M);
f[0].init(N), f[1].init(N);
for(int i = 1; i <= M; i++)
{
int x, y;
scanf("%d%d", &x, &y);
if(f[0].F(x) != f[0].F(y))
{
critical.push_back({x, y});
f[0].unite(x, y);
int fx = f[1].F(x);
int fy = f[1].F(y);
edg[fx].push_back(fy);
edg[fy].push_back(fx);
continue;
}
if(f[1].F(x) == f[1].F(y)) continue;
x = f[1].F(x);
y = f[1].F(y);
nodes.clear();
nodes.push_back(x);
nodes.push_back(y);
while(!q.empty()) q.pop();
K += 2;
vis[x] = K; vis[y] = K + 1;
q.push({x, K});
q.push({y, K + 1});
while(!q.empty())
{
int nod = q.front().first;
int id = q.front().second;
q.pop();
for(auto nxt: edg[nod])
{
if(vis[nxt] == id) continue;
if(vis[nxt] == (id ^ 1))
{
int nd1 = nod;
int nd2 = nxt;
if(vis[nod] != K) swap(nd1, nd2);
while(nd1 != x)
{
nodes.push_back(nd1);
nd1 = lst[nd1];
}
while(nd2 != y)
{
nodes.push_back(nd2);
nd2 = lst[nd2];
}
while(!q.empty()) q.pop();
break;
}
vis[nxt] = vis[nod];
lst[nxt] = nod;
}
}
for(int i = 1; i < nodes.size(); i++)
{
f[1].unite(nodes[0], nodes[i]);
for(auto x: edg[ nodes[i] ])
if(f[1].F(x) != f[1].F(nodes[0]))
edg[ nodes[0] ].push_back(x);
}
}
for(auto e: critical)
if(f[1].F(e.first) != f[1].F(e.second))
printf("%d %d\n", e.first, e.second);
return 0;
}
Compilation message (stderr)
# | 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... |