#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define pii pair<int, int>
#define p3i pair<pii, int>
#define pll pair<ll, ll>
#define p3l pair<pll, ll>
#define lseg L, (L+R)/2, N*2+1
#define rseg (L+R)/2+1, R, N*2+2
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define MN 1000000007
#define fox(k, x) for (int k=0; k<x; ++k)
#define fox1(k, x) for (int k=1; k<=x; ++k)
#define foxr(k, x) for (int k=x-1; k>=0; --k)
#define fox1r(k, x) for (int k=x; k>0; --k)
#define ms multiset
#define flood(x) memset(x, 0x3f3f3f3f, sizeof x)
#define drain(x) memset(x, 0, sizeof x)
#define rng() (rand() >> 3)*rand()
#define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
char _;
int n, m, t, a, b, X2, Y2, r[100005], p[100005], h[100005], d[100005];
bool u[100005];
vector<int> v[100005], com[100005];
bool mrg(int A, int B){
//cout << A << ' ' << B << ' ' ;
A=r[A]; B=r[B];
//cout << A << ' ' << B << ' ' << endl;
if (A==B) return 0;
if (com[A].size()<com[B].size()) return mrg(B, A);
for (auto i:com[B]){
com[A].pb(i);
r[i]=A;
}
return 1;
}
void dfs(int N, int P){
p[N]=P;
if (P!=-1) d[N]=d[P]+1;
fox(l, v[N].size()){
if (v[N][l]==P) continue;
dfs(v[N][l], N);
}
}
void lca(int X, int Y){
X=r[X]; Y=r[Y];
while(X!=Y){
if (d[X2=h[X]]>d[Y2=h[Y]]){
u[X2]=1;
t=h[r[p[X2]]];
mrg(X, p[X2]);
h[X]=t;
} else {
u[Y2]=1;
//Y=p[Y];
t=h[r[p[Y2]]];
mrg(Y, p[Y2]);
h[Y]=t;
}
X=r[X]; Y=r[Y];
}
}
queue<pii> q;
int main(){
scan(n); scan(m);
fox1(l, n){
r[l]=l; h[l]=l;
com[l].clear(); com[l].pb(l);
}
fox(l, m){
scan(a); scan(b);
if (mrg(a, b)){
//cout << l << endl;
v[a].pb(b);
v[b].pb(a);
q.push(mp(a, b));
}
}
fox1(l, n){
if (p[l]==0){
dfs(l, -1);
}
}
//return 00;
fox1(l, n){
r[l]=l;
com[l].clear(); com[l].pb(l);
}
rewind(stdin);
scan(n); scan(m);
fox(l, m){
scan(a); scan(b);
if (mp(a, b)==q.front()) q.pop();
else lca(a, b);
}
fox1(l, n){
if (!u[l] && p[l]!=-1){
printf("%i %i\n", l, p[l]);
}
}
return 0;
}
Compilation message
pipes.cpp: In function 'void dfs(int, int)':
pipes.cpp:18:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define fox(k, x) for (int k=0; k<x; ++k)
pipes.cpp:48:9:
fox(l, v[N].size()){
~~~~~~~~~~~~~~
pipes.cpp:48:5: note: in expansion of macro 'fox'
fox(l, v[N].size()){
^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4992 KB |
Output is correct |
2 |
Correct |
6 ms |
4992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
5476 KB |
Output is correct |
2 |
Correct |
9 ms |
5552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
5376 KB |
Output is correct |
2 |
Correct |
99 ms |
5368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
6136 KB |
Output is correct |
2 |
Correct |
195 ms |
6008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
311 ms |
7544 KB |
Output is correct |
2 |
Correct |
264 ms |
8140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
432 ms |
12416 KB |
Output is correct |
2 |
Correct |
396 ms |
12456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
657 ms |
13620 KB |
Output is correct |
2 |
Correct |
650 ms |
13676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
901 ms |
15300 KB |
Output is correct |
2 |
Correct |
844 ms |
15344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1059 ms |
15236 KB |
Output is correct |
2 |
Correct |
1071 ms |
15576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1285 ms |
14812 KB |
Output is correct |
2 |
Correct |
1285 ms |
15544 KB |
Output is correct |