# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
54870 |
2018-07-05T08:32:48 Z |
노영훈(#1508) |
Pipes (CEOI15_pipes) |
C++11 |
|
3652 ms |
9276 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MX=100010, inf=2e9;
int n, m;
vector<pii> G[MX];
int U[MX];
pii par[MX];
int dep[MX];
int sz[MX];
int root[MX];
int rnk[MX];
inline int find(int x){
return x==U[x] ? x : U[x]=find(U[x]);
}
void unite(int x, int y){
if(find(x)==find(y)) return;
x=U[x], y=U[y];
if(rnk[x]<rnk[y]) swap(x,y);
if(rnk[x]==rnk[y]) rnk[x]++;
U[y]=x;
}
void dfs(int v, int p, int d){
dep[v]=d, root[v]=root[p];
for(pii &e:G[v]){
int x=find(e.first);
if(x==p) continue;
par[x]={e.second, e.first};
dfs(x,v,d+1);
}
}
void merge(int u, int v){
if(sz[root[find(u)]]<sz[root[find(v)]]) swap(u,v);
// v(b)를 u(a)밑에 붙인다.
int a=find(u), b=find(v);
G[a].push_back({v,u});
G[b].push_back({u,v});
sz[root[a]]+=sz[root[b]];
par[b]={u,v};
dfs(b, a, dep[a]+1);
}
vector<int> A;
vector<pii> adj;
void lca(int u, int v){
A.clear(); adj.clear();
u=find(u), v=find(v);
while(u!=v){
if(dep[u]>dep[v]){
A.push_back(u);
u=find(par[u].first);
}
else{
A.push_back(v);
v=find(par[v].first);
}
}
A.push_back(u);
for(int v:A) unite(v,u);
int p=find(u), pdep=dep[u]; pii ppar=par[u];
for(int v:A){
for(pii &e:G[v]){
int x=find(e.first);
if(x==p) continue;
adj.push_back(e);
}
G[v].clear();
}
// dep ???
dep[p]=pdep;
par[p]=ppar;
G[p]=adj;
}
void init(){
for(int i=1; i<=n; i++){
U[i]=i, par[i]={0,0}, dep[i]=0, sz[i]=1, root[i]=i, rnk[i]=1;
}
}
void solve(){
for(int v=1; v<=n; v++){
if(find(v)!=v) continue;
if(par[v]==pii(0,0)) continue;
cout<<par[v].first<<' '<<par[v].second<<'\n';
}
}
void debug(){
for(int v=1; v<=n; v++){
if(find(v)==v){
cout<<v<<": ";
for(pii &e:G[v]) cout<<find(e.first)<<' ';
cout<<'\n';
}
}
cout<<'\n';
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
cin>>n>>m;
init();
for(int i=1; i<=m; i++){
int u, v; cin>>u>>v;
if(find(u)==find(v)) continue;
if(root[find(u)]==root[find(v)]){
lca(u, v);
}
else{
merge(u, v);
}
// debug();
}
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2688 KB |
Output is correct |
2 |
Correct |
3 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
2988 KB |
Output is correct |
2 |
Correct |
8 ms |
3044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
2944 KB |
Output is correct |
2 |
Correct |
92 ms |
2984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
170 ms |
3412 KB |
Output is correct |
2 |
Correct |
185 ms |
3328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
437 ms |
4276 KB |
Output is correct |
2 |
Correct |
290 ms |
4472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1618 ms |
7376 KB |
Output is correct |
2 |
Correct |
538 ms |
7224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2469 ms |
7904 KB |
Output is correct |
2 |
Correct |
967 ms |
7604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3537 ms |
9272 KB |
Output is correct |
2 |
Correct |
1087 ms |
9132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3652 ms |
9276 KB |
Output is correct |
2 |
Correct |
1207 ms |
9048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3592 ms |
8864 KB |
Output is correct |
2 |
Correct |
1736 ms |
8936 KB |
Output is correct |