#include <bits/stdc++.h>
using namespace std;
const int nmax = 1e5;
int n,m;
int td[nmax + 5], rang[nmax + 5];
int lvl[nmax + 5], t[nmax + 5][17];
int sum[nmax + 5];
//map<pair<int,int>,bool> ok;
vector<pair<int,int>> e;
vector<int> G[nmax + 5];
int rep(int x)
{
if(x==td[x])
{
return x;
}
return (td[x] = rep(td[x]));
}
int lca(int x, int y)
{
if(lvl[x] > lvl[y])
{
swap(x,y);
}
int dif = lvl[y] - lvl[x];
for(int p=0; p<=16; p++)
{
if((dif & (1<<p))!=0)
{
y = t[y][p];
}
}
if(x==y)
{
return x;
}
for(int p=16; p>=0; p--)
{
if(t[x][p] && t[y][p] && t[x][p]!=t[y][p])
{
x = t[x][p];
y = t[y][p];
}
}
return t[x][0];
}
void update(int x, int y)
{
int l = lca(x,y);
++sum[x];
--sum[l];
++sum[y];
--sum[l];
}
void get_ans(int nod, int dad = 0)
{
for(auto it : G[nod])
{
if(it==dad)
{
continue;
}
get_ans(it,nod);
sum[nod] += sum[it];
}
/*if(sum[nod] > 0 && dad)
{
ok[ {nod,dad}] = true;
}*/
}
void dfs_dad(int nod, int dad, int level)
{
lvl[nod] = level;
t[nod][0] = dad;
for(int p=1; p<=16; p++)
{
t[nod][p] = t[t[nod][p-1]][p-1];
}
sum[nod] = 0;
for(auto it : G[nod])
{
if(it==dad)
{
continue;
}
dfs_dad(it,nod,level + 1);
}
}
int get_root(int nod)
{
while(t[nod][0])
{
nod = t[nod][0];
}
return nod;
}
void Add(int x, int y)
{
if(rep(x) == rep(y))
{
update(x,y);
return;
}
e.push_back({x,y});
int rx = rep(x);
int ry = rep(y);
if(rang[ry] > rang[rx])
{
swap(rx,ry);
swap(x,y);
}
td[ry] = rx;
rang[rx] += rang[ry];
int r = get_root(y);
get_ans(r);
G[x].push_back(y);
G[y].push_back(x);
dfs_dad(y,x,lvl[x] + 1);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef home
freopen("nr.in","r",stdin);
freopen("nr.out","w",stdout);
#endif // home
cin>>n>>m;
for(int i=1;i<=n;i++)
{
rang[i] = 1;
td[i] = i;
}
for(int i=1; i<=m; i++)
{
int x,y;
cin>>x>>y;
Add(x,y);
}
get_ans(get_root(1));
for(auto it : e)
{
int x = it.first;
int y = it.second;
/* if(!ok[ {x,y}] && !ok[ {y,x}])
{
cout<<x<<' '<<y<<'\n';
} */
}
return 0;
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:159:13: warning: unused variable 'x' [-Wunused-variable]
159 | int x = it.first;
| ^
pipes.cpp:160:13: warning: unused variable 'y' [-Wunused-variable]
160 | int y = it.second;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
6748 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
7000 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
6748 KB |
Output is correct |
2 |
Incorrect |
145 ms |
6748 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
248 ms |
7004 KB |
Output is correct |
2 |
Incorrect |
276 ms |
7156 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
368 ms |
7744 KB |
Output is correct |
2 |
Incorrect |
320 ms |
8556 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
515 ms |
12996 KB |
Output is correct |
2 |
Incorrect |
463 ms |
12844 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
858 ms |
13812 KB |
Output is correct |
2 |
Incorrect |
773 ms |
13828 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1114 ms |
14924 KB |
Output is correct |
2 |
Runtime error |
1010 ms |
56348 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1373 ms |
14920 KB |
Output is correct |
2 |
Runtime error |
1308 ms |
65536 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1662 ms |
14704 KB |
Output is correct |
2 |
Runtime error |
1627 ms |
65536 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |