#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
typedef pair<int,int> pii;
vector<vector<int>> muchii;
int comp[100005],myroot[100005],dp[19][100005],val[100005],par[100005],niv[100005],lg[100005];
vector<pii> used;
int n,m;
int what;
int LCA(int a,int b)
{
if(niv[a]<niv[b])
swap(a,b);
int dif=niv[a]-niv[b];
int loga=log2(dif);
for(int i=0;i<=loga;i++)
if((dif>>i)&1)
a=dp[i][a];
if(a==b)
return a;
loga=log2(niv[a]);
for(int i=loga;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];
}
return par[a];
}
void dfscalc(int nod)
{
for(int q:muchii[nod])
{
int i=abs(q);
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.push_back({a,b});
}
comp[nod]=what;
}
void dfsbuild(int nod)
{
val[nod]=0;
dp[0][nod]=par[nod];
for(int i=1;i<=17;i++)
dp[i][nod]=dp[i-1][dp[i-1][nod]];
for(int q:muchii[nod])
{
int i=abs(q);
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(lg[c1]<lg[c2])
{
swap(a,b);
swap(c1,c2);
}
what=c1;
dfscalc(myroot[c2]);
par[b]=a;
lg[c1]+=lg[c2];
lg[c2]=0;
niv[b]=niv[a]+1;
muchii[a].push_back(b);
muchii[b].push_back(a);
dfsbuild(b);
}
int main()
{
ios_base::sync_with_stdio(false);
cin>>n>>m;
muchii.resize(n+1);
for(int i=1;i<=n;i++)
{
niv[i]=1;
lg[i]=1;
comp[i]=i;
myroot[i]=i;
par[i]=0;
val[i]=0;
}
int cnt=0;
for(int z=1;z<=m;z++)
{
int a,b;
cin>>a>>b;
if(comp[a]!=comp[b])
{
cnt++;
dsumerge(a,b);
}
else
{
int lca=LCA(a,b);
val[a]++;
val[b]++;
val[lca]-=2;
}
}
for(int i=1;i<=n;i++)
if(lg[i])
dfscalc(myroot[i]);
sort(used.begin(),used.end());
used.erase(unique(used.begin(),used.end()),used.end());
int poz=0;
for(int i=1;i<=n;i++)
{
sort(muchii[i].begin(),muchii[i].end());
for(int j:muchii[i])
if(j>i)
{
pii p={i,j};
while(poz<used.size()&&used[poz]<p)
poz++;
if(poz<used.size()&&used[poz].first==p.first&&used[poz].second==p.second)
continue;
else
cout<<i<<' '<<j<<'\n';
}
}
return 0;
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:130:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
130 | while(poz<used.size()&&used[poz]<p)
| ~~~^~~~~~~~~~~~
pipes.cpp:132:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
132 | if(poz<used.size()&&used[poz].first==p.first&&used[poz].second==p.second)
| ~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1108 KB |
Output is correct |
2 |
Correct |
5 ms |
1184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
956 KB |
Output is correct |
2 |
Correct |
150 ms |
940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
286 ms |
1980 KB |
Output is correct |
2 |
Correct |
319 ms |
2148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
491 ms |
4324 KB |
Output is correct |
2 |
Correct |
409 ms |
5152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
748 ms |
11944 KB |
Output is correct |
2 |
Correct |
659 ms |
12936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1249 ms |
13524 KB |
Output is correct |
2 |
Correct |
1052 ms |
13436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1556 ms |
16264 KB |
Output is correct |
2 |
Runtime error |
1600 ms |
17268 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1831 ms |
16264 KB |
Output is correct |
2 |
Runtime error |
1810 ms |
17196 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2419 ms |
15520 KB |
Output is correct |
2 |
Correct |
2215 ms |
16348 KB |
Output is correct |