# |
제출 시각 |
아이디 |
문제 |
언어 |
결과 |
실행 시간 |
메모리 |
54841 |
2018-07-05T08:08:12 Z |
노영훈(#1508) |
Pipes (CEOI15_pipes) |
C++11 |
|
5000 ms |
65536 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;
list<pii> G[MX];
int U[MX];
pii par[MX];
int dep[MX];
int sz[MX];
int root[MX];
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;
U[U[x]]=U[y];
}
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]];
dfs(b, a, dep[a]+1);
par[b]={u,v};
}
void lca(int u, int v){
u=find(u), v=find(v);
if(dep[u]<dep[v]) swap(u,v);
list<int> A;
while(dep[u]>dep[v]){
A.push_back(u);
u=find(par[u].first);
}
while(u!=v){
A.push_back(u);
A.push_back(v);
u=find(par[u].first);
v=find(par[v].first);
}
A.push_back(u);
int p=find(u); pii ppar=par[u];
list<pii> adj;
for(int v:A) unite(v,p);
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 ???
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;
}
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
63 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
3036 KB |
Output is correct |
2 |
Execution timed out |
5078 ms |
34660 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
124 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4866 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
103 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
129 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
122 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
134 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
139 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
150 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |