#include <iostream>
#include <algorithm>
#include <climits>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <vector>
using namespace std;
const int MAXN=1e5+1;
const int LG=17;
int pa[MAXN][LG];
int par[MAXN];
int par1[MAXN];
int h[MAXN];
int Up[MAXN];
bool vis[MAXN];
vector<int> ma[MAXN];
vector<int> comp2[MAXN];
int get1(int a)
{
return ((par1[a]==a)?a:par1[a]=get1(par1[a]));
}
void join1(int a,int b)
{
a=get1(a);
b=get1(b);
if(a!=b)
par1[a]=b;
}
int get(int a)
{
return ((par[a]==a)?a:par[a]=get(par[a]));
}
bool join(int a,int b)
{
int oa=a;
int ob=b;
a=get(a);
b=get(b);
if(a!=b)
{
par[a]=b;
return 1;
}
join1(oa,ob);
return 0;
}
void dfs(int x,int p=1)
{
pa[x][0]=p;
for(int j=1;j<LG;j++)
pa[x][j]=pa[pa[x][j-1]][j-1];
h[x]=h[p]+1;
for(auto y:ma[x])
if(y!=p)
dfs(y,x);
}
int lca(int x,int y)
{
if(h[x]>h[y])
swap(x,y);
for(int j=LG-1;j>=0;j--)
if(h[x]<=h[pa[y][j]])
y=pa[y][j];
if(x==y)
return x;
for(int j=LG-1;j>=0;j--)
if(pa[x][j]!=pa[y][j])
{
x=pa[x][j];
y=pa[y][j];
}
return pa[x][0];
}
void solve(int x,int p=-1)
{
vis[x]=1;
for(auto y:ma[x])
{
if(y!=p)
{
solve(y,x);
}
}
for(auto y:ma[x])
{
if(y!=p)
{
if(h[Up[x]]>h[Up[y]])
{
Up[x]=Up[y];
}
if(Up[y]==y)
{
cout<<x<<' '<<y<<endl;
}
}
}
for(auto y:comp2[x])
{
int c=lca(x,y);
if(h[Up[x]]>h[c])
{
Up[x]=c;
}
if(h[Up[y]]>h[c])
{
Up[y]=c;
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
par[i]=par1[i]=Up[i]=i;
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
if(join(x,y))
{
ma[x].push_back(y);
ma[y].push_back(x);
}
}
for(int i=1;i<=n;i++)
if(pa[i][0]==0)
dfs(i);
for(int i=1;i<=n;i++)
if(h[i]>h[get1(i)])
par1[get1(i)]=par1[i]=i;
for(int i=1;i<=n;i++)
comp2[get1(i)].push_back(i);
for(int i=1;i<=n;i++)
if(!vis[i])
solve(i);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
6748 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
7420 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
70 ms |
12500 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
120 ms |
17424 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
217 ms |
25936 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
331 ms |
38416 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
478 ms |
50512 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
643 ms |
64108 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
757 ms |
65536 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
871 ms |
65536 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |