#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
vector<vector<int>> dsu;
vector<vector<int>> muchii;
int comp[100005],myroot[100005],dp[19][100005],val[100005],par[100005],niv[100005];
set<pii> used;
vector<pii> edges;
int n,m;
int LCA(int a,int b)
{
if(niv[a]<niv[b])
swap(a,b);
bool ok=0;
if(b==10)
ok=1;
int dif=niv[a]-niv[b];
for(int i=0;i<=17;i++)
if((dif>>i)&1)
a=dp[i][a];
ok=0;
if(niv[a]!=niv[b])
ok=1;
assert(niv[a]==niv[b]);
if(a==b)
return a;
for(int i=17;i>=0;i--)
if(dp[i][a]!=0&&dp[i][b]!=0)
if(dp[i][a]!=dp[i][b])
{
a=dp[i][a];
b=dp[i][b];
}
assert(par[a]==par[b]&&a!=b);
return par[a];
}
void dfscalc(int nod)
{
for(int i:muchii[nod])
if(i!=par[nod])
{
dfscalc(i);
val[nod]+=val[i];
}
if(val[nod])
{
int a=min(nod,par[nod]);
int b=max(nod,par[nod]);
used.insert({a,b});
}
}
void dfsbuild(int nod)
{
dp[0][nod]=par[nod];
for(int i=1;i<=17;i++)
dp[i][nod]=dp[i-1][dp[i-1][nod]];
for(int i:muchii[nod])
if(i!=par[nod])
{
par[i]=nod;
niv[i]=niv[nod]+1;
dfsbuild(i);
}
}
void dsumerge(int a, int b)
{
int c1=comp[a],c2=comp[b];
if(dsu[c1].size()<dsu[c2].size())
{
swap(a,b);
swap(c1,c2);
}
dfscalc(myroot[c2]);
for(int i:dsu[c2])
val[i]=0;
par[b]=a;
niv[b]=niv[a]+1;
muchii[a].push_back(b);
muchii[b].push_back(a);
dfsbuild(b);
for(int i:dsu[c2])
{
comp[i]=c1;
dsu[c1].push_back(i);
}
dsu[c2].clear();
}
int main()
{
cin>>n>>m;
dsu.resize(n+1);
muchii.resize(n+1);
for(int i=1;i<=n;i++)
{
dsu[i].push_back(i);
niv[i]=1;
comp[i]=i;
myroot[i]=i;
par[i]=0;
val[i]=0;
}
for(int z=1;z<=m;z++)
{
int a,b;
cin>>a>>b;
if(comp[a]!=comp[b])
{
dsumerge(a,b);
edges.push_back({min(a,b),max(a,b)});
}
else
{
int lca=LCA(a,b);
val[a]++;
val[b]++;
val[lca]-=2;
}
}
for(int i=1;i<=n;i++)
if(dsu[i].size())
dfscalc(myroot[i]);
for(auto i:edges)
if(used.find(i)==used.end())
cout<<i.first<<' '<<i.second<<'\n';
return 0;
}
Compilation message
pipes.cpp: In function 'int LCA(int, int)':
pipes.cpp:15:10: warning: variable 'ok' set but not used [-Wunused-but-set-variable]
15 | bool ok=0;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
1608 KB |
Output is correct |
2 |
Correct |
10 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
319 ms |
1284 KB |
Output is correct |
2 |
Correct |
345 ms |
1228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
654 ms |
2912 KB |
Output is correct |
2 |
Correct |
870 ms |
14156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1022 ms |
6492 KB |
Output is correct |
2 |
Runtime error |
955 ms |
22060 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1578 ms |
18612 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2274 ms |
21472 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3248 ms |
26236 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
4173 ms |
26360 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
4649 ms |
49072 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |