답안 #1026398

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1026398 2024-07-18T04:40:32 Z vjudge1 Pipes (CEOI15_pipes) C++17
0 / 100
5000 ms 7004 KB
#include <bits/stdc++.h>

#define ll long long
using namespace std;
const int N = 1e5 + 2;
int par1[N + 2] , par2[N + 2];
int nxt[N + 2] , depth[N + 2];
vector <int > adj[N + 2];
int find_par1(int u){
    if(par1[u] < 0) return u;
    par1[u] = find_par1(par1[u]);
    return par1[u];
}

int find_par2(int u){
    if(par2[u] < 0) return u;
    par2[u] = find_par2(par2[u]);
    return par2[u];
}
void union_set2(int u , int v){
    u = find_par2(u);
    v = find_par2(v);
    if(u != v){
        if(par2[u] > par2[v])swap(u , v);
        par2[u] += par2[v];
        par2[v] = u;
        depth[u] = min(depth[u] , depth[v]);
        if(find_par2(nxt[u]) == u){
            nxt[u] = nxt[v];
        }
    }
}
void dfs(int u , int p ,int h ){
    depth[find_par2(u)] = h;
    nxt[find_par2(u)] = find_par2(p);
    for(auto v: adj[u]){
        if(v == p)continue;
        if(find_par2(v) == find_par2(u))dfs(v , u , h);
        else dfs(v , u , h + 1);
    }
}
bool union_set1(int u , int v){ 
    int u_old = u;
    int v_old = v;
    u = find_par1(u);
    v = find_par1(v);
    if(u != v){
        if(par1[u] > par1[v]){
            swap(u , v);
            swap(v_old , u_old);
        }
        par1[u] += par1[v];
        par1[v] = u;
        dfs(v_old , u_old , depth[u_old] + 1);
        return true;
    }
    return false;
}
void lca(int u , int v){
    u = find_par2(u);
    v =  find_par2(v);
    while(u != v){
        if(depth[u] > depth[v])swap(u , v);
        int v_new = nxt[v];
        union_set2(v_new , v);
        v =  v_new;
    }
}
void dfs2(int u){
    par1[u] = 1;
    for(auto v: adj[u]){
        if(par1[v])continue;
        if(find_par2(v) != find_par2(u)){
            cout << u << ' ' << v  << '\n';
        }
        dfs2(v);
    }
}
void solve() {
    int n , m;
    cin >> n >> m;
    memset(par1 , -1 , sizeof(par1));
    memset(par2 , -1 , sizeof(par2));
    for(int i = 1; i <= m; i ++){
        int u , v;
        cin >>u >>v ;
        if(union_set1(u , v)){
            adj[u].push_back(v);
            adj[v].push_back(u);
        }
        else{
            lca(u , v);
        }
    }
    memset(par1 , 0 , sizeof(par1));
    for(int i = 1 ; i <= n ; i++){
        if(!par1[i])dfs2(i);
    }
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0);
    
    #define _ "maxseq."
    if (fopen(_ "inp", "r")) {
        freopen(_ "inp", "r", stdin);
        freopen(_ "out", "w", stdout);
    }
    
    solve();
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |         freopen(_ "inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
pipes.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |         freopen(_ "out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5017 ms 3420 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5065 ms 3676 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5014 ms 3672 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5017 ms 3928 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5045 ms 4440 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5035 ms 5984 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5039 ms 6264 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5034 ms 7000 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5058 ms 7004 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5039 ms 6772 KB Time limit exceeded
2 Halted 0 ms 0 KB -