This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Giorgi Kldiashvili
#include <bits/stdc++.h>
#define ll long long
#define M 1000000007ll
using namespace std;
const int N = 100001;
int n, m, x, y, timer;
int p[N], P[N], c[N], d[N], in[N], out[N];
vector < int > G[N];
bool used[N];
int Parent(int x)
{
if(P[x] == x)
return x;
return P[x] = Parent(P[x]);
}
void go(int v)
{
c[v] = d[v] = in[v] = ++ timer;
used[v] = true;
for(int i = 0; i < G[v].size(); ++ i)
{
if(used[G[v][i]])
continue;
p[G[v][i]] = v;
go(G[v][i]);
}
out[v] = timer;
}
void dfs(int v)
{
used[v] = true;
for(int i = 0; i < G[v].size(); ++ i)
{
if(used[G[v][i]])
continue;
dfs(G[v][i]);
c[v] = min(c[v], c[G[v][i]]);
d[v] = max(d[v], d[G[v][i]]);
}
if(p[v] == 0)
return;
if(c[v] >= in[v] && d[v] <= out[v])
printf("%d %d\n", v, p[v]);
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; ++ i)
P[i] = i;
while(m --)
{
char ch = getchar();
x = y = 0;
while(ch > '9' || ch < '0')
ch = getchar();
while(ch >= '0' && ch <= '9')
x = x * 10 + (ch - '0'), ch = getchar();
ch = getchar();
while(ch > '9' || ch < '0')
ch = getchar();
while(ch >= '0' && ch <= '9')
y = y * 10 + (ch - '0'), ch = getchar();
if(Parent(x) == Parent(y))
continue;
P[Parent(x)] = y;
G[x].push_back(y);
G[y].push_back(x);
}
for(int i = 1; i <= n; ++ i)
{
if(used[i]) continue;
go(i);
}
rewind(stdin);
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; ++ i)
P[i] = i, used[i] = 0;
while(m --)
{
char ch = getchar();
x = y = 0;
while(ch > '9' || ch < '0')
ch = getchar();
while(ch >= '0' && ch <= '9')
x = x * 10 + (ch - '0'), ch = getchar();
ch = getchar();
while(ch > '9' || ch < '0')
ch = getchar();
while(ch >= '0' && ch <= '9')
y = y * 10 + (ch - '0'), ch = getchar();
if(Parent(x) == Parent(y))
{
c[x] = min(c[x], in[y]);
d[x] = max(d[x], in[y]);
d[y] = max(d[y], in[x]);
c[y] = min(c[y], in[x]);
continue;
}
P[Parent(x)] = y;
}
for(int i = 1; i <= n; ++ i)
{
if(used[i]) continue;
dfs(i);
}
}
Compilation message (stderr)
pipes.cpp: In function 'void go(int)':
pipes.cpp:28:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < G[v].size(); ++ i)
~~^~~~~~~~~~~~~
pipes.cpp: In function 'void dfs(int)':
pipes.cpp:40:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < G[v].size(); ++ i)
~~^~~~~~~~~~~~~
pipes.cpp: In function 'int main()':
pipes.cpp:59:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
pipes.cpp:91:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
# | 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... |