#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define pb push_back
#define mp make_pair
#define taskname "A"
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef tree <int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const int maxn = 1e5 + 5;
const int logn = log2(maxn) + 1;
int n , m , nEdge = 0;
struct Edge{
int u , v , col;
}e[maxn];
int par[maxn] , path[maxn] , P[maxn][logn];
int lab[maxn] , h[maxn];
vector<int> adj[maxn];
int FindLab(int u){
return lab[u] < 0 ? u : lab[u] = FindLab(lab[u]);
}
void BuildLCA(int u , int par){
P[u][0] = par;
for(int i = 1 ; i < logn && P[u][i - 1] ; ++i){
P[u][i] = P[P[u][i - 1]][i - 1];
}
}
void dfs(int u , int par){
path[u] = par;
for(int c : adj[u]){
if(par != e[c].u + e[c].v - u){
h[e[c].u + e[c].v - u] = h[u] + 1;
::par[e[c].u + e[c].v - u] = c;
BuildLCA(e[c].u + e[c].v - u , u);
dfs(e[c].u + e[c].v - u , u);
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
if(fopen(taskname".INP","r")){
freopen(taskname".INP", "r",stdin);
freopen(taskname".OUT", "w",stdout);
}
fill_n(lab,maxn,-1);
fill_n(par,maxn,-1);
cin >> n >> m;
for(int i = 1 ; i <= m ; ++i){
int u , v;cin >> u >> v;
int s , t;
s = FindLab(u);
t = FindLab(v);
if(s != t){
e[nEdge].u = u;e[nEdge].v = v;
if(lab[s] > lab[t])swap(s , t) , swap(u , v);
lab[s] += lab[t];
lab[t] = s;
h[v] = h[u] + 1;
BuildLCA(v , u);
par[v] = nEdge;
dfs(v , u);
adj[u].pb(nEdge);
adj[v].pb(nEdge);
++nEdge;
}else{
auto FindLCA = [&](int u , int v){
if(h[u] < h[v]){
swap(u , v);
}
for(int i = 0 ; i < logn ; ++i){
if((h[u] - h[v]) & (1 << i))u = P[u][i];
}
if(u == v)return u;
for(int i = logn - 1 ; i >= 0 ; --i){
if(P[u][i] != P[v][i])u = P[u][i] , v = P[v][i];
}
return P[u][0];
};
int c = FindLCA(u , v);
auto Color = [&](int u , int v){
while(h[u] > h[v]){
e[par[u]].col = 1;
function<int(int)> FindPath = [&](int u){
if(par[u] == -1){
return u;
}
if(e[par[u]].col == 0)return u;
return path[u] = FindPath(path[u]);
};
u = FindPath(u);
}
};
Color(u , c);
Color(v , c);
}
}
for(int i = 0 ; i < nEdge ; ++i){
if(e[i].col == 0)cout << e[i].u << " " << e[i].v << endl;
}
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:57:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(taskname".INP", "r",stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
pipes.cpp:58:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(taskname".OUT", "w",stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3448 KB |
Output is correct |
2 |
Correct |
6 ms |
3568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
4216 KB |
Output is correct |
2 |
Incorrect |
11 ms |
4088 KB |
Mismatched edge |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
241 ms |
9308 KB |
Output is correct |
2 |
Incorrect |
227 ms |
9164 KB |
Wrong number of edges |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
463 ms |
14300 KB |
Output is correct |
2 |
Incorrect |
539 ms |
15992 KB |
Wrong number of edges |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
840 ms |
22416 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1555 ms |
32320 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2769 ms |
46088 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3551 ms |
58968 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5012 ms |
65536 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5009 ms |
65536 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |