답안 #573449

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
573449 2022-06-06T16:14:57 Z vladislav11 Pipes (CEOI15_pipes) C++14
0 / 100
2242 ms 65536 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5+10;

int leader[N];
int ssize[N];

int get_leader ( int v )
{
    if ( leader[v] == v )
        return v;

    leader[v] = get_leader( leader[v] );

    return leader[v];
}

vector< pair<int,int> > adj[N];
int a[N];

int jmp[N];
int par[N];
int depth[N];

int lca ( int u, int v )
{
    //cout << "IN_LCA\n";

    if ( u == v )
        return u;

    if ( depth[u] > depth[v] )
        swap( u, v );

    //cout << "u v: " << u << ' ' << v << '\n';
    //cout << "depth: " << depth[u] << ' ' << depth[v] << '\n';

    while ( depth[u] < depth[v] )
    {
        if ( depth[u] <= depth[ jmp[v] ] )
            v = jmp[v];
        else
            v = par[v];
    }

    //cout << "jmp: " << jmp[u] << ' ' << jmp[v] << '\n';
    //cout << "par: " << par[u] << ' ' << par[v] << '\n';

    while ( u != v )
    {
        if ( jmp[u] != jmp[v] )
            u = jmp[u], v = jmp[v];
        else
            u = par[u], v = par[v];
    }

    return v;
}

void add_leaf ( int v, int p )
{
    par[v] = p;
    depth[v] = depth[p] + 1;

    if ( jmp[p] == -1 || jmp[ jmp[p] ] == -1 )
        jmp[v] = p;

    else if ( depth[p]-depth[ jmp[p] ] == depth[ jmp[p] ]-depth[ jmp[ jmp[p] ] ] )
        jmp[v] = jmp[ jmp[p] ];

    else
        jmp[v] = p;
}

void dfs_build_jmp ( int v, int p )
{
    add_leaf( v, p );

    for ( auto& [to,w] : adj[v] )
    if ( to != p )
        dfs_build_jmp( to, v );
}

int dfs ( int v, int p )
{
    for ( auto& [to,w] : adj[v] )
    if ( to != p )
    {
        int res = dfs( to, v );

        a[v] += res;

        if ( res )
            w = 1;
    }

    int res = a[v];
    a[v] = 0;

    return res;
}

int main ()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n, m;
    cin >> n >> m;

    for ( int i=1; i<=n; i++ )
    {
        leader[i] = i;
        ssize[i] = 1;

        a[i] = 0;

        jmp[i] = par[i] = -1;
        depth[i] = 1;
    }

    for ( int i=0; i<m; i++ )
    {
        int u, v;
        cin >> u >> v;

        if ( get_leader( u ) == get_leader( v ) )
        {
            //cout << "same\n";

            int w = lca( u, v );

            //cout << "lca: " << w << '\n';

            a[u]++;
            a[v]++;
            a[w] -= 2;

            continue;
        }

        int lu = get_leader( u );
        int lv = get_leader( v );

        //cout << "different\n";

        if ( ssize[lu] < ssize[lv] )
        {
            swap( lu, lv );
            swap( u, v );
        }

        //cout << "u: " << u << ' ' << lu << '\n';
        //cout << "v: " << v << ' ' << lv << '\n';

        dfs( lv, -1 );

        //cout << "here1 ";

        dfs_build_jmp( v, u );

        adj[u].push_back({ v, 0 });
        adj[v].push_back({ u, 0 });

        //cout << "here2\n";

        leader[lv] = lu;
        ssize[lu] += ssize[lv];

        /*cout << "leader: ";
        for ( int i=1; i<=n; i++ )
            cout << leader[i] << ' ';
        cout << '\n';*/
    }

    //cout << "OK 1\n";

    set<int> leaders;

    for ( int i=1; i<=n; i++ )
        leaders.insert( get_leader(i) );

    //cout << "OK 2\n";

    /*cout << "leaders: ";

    for ( auto& el : leaders )
        cout << el << ' ';
    cout << '\n';*/

    for ( auto& v : leaders )
        dfs( v, -1 );

    //cout << "OK 3\n";

    for ( int i=1; i<=n; i++ )
    for ( auto& [to,w] : adj[i] )
    if ( !w && i < to )
        cout << i << ' ' << to << '\n';

    return 0;
}

Compilation message

pipes.cpp: In function 'void dfs_build_jmp(int, int)':
pipes.cpp:81:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   81 |     for ( auto& [to,w] : adj[v] )
      |                 ^
pipes.cpp: In function 'int dfs(int, int)':
pipes.cpp:88:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   88 |     for ( auto& [to,w] : adj[v] )
      |                 ^
pipes.cpp: In function 'int main()':
pipes.cpp:199:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  199 |     for ( auto& [to,w] : adj[i] )
      |                 ^
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2680 KB Wrong number of edges
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 3080 KB Wrong number of edges
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 127 ms 8376 KB Wrong number of edges
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 233 ms 12840 KB Wrong number of edges
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 430 ms 17300 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 639 ms 7728 KB Wrong number of edges
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1139 ms 40364 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1410 ms 52972 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1757 ms 64844 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2242 ms 65536 KB Memory limit exceeded
2 Halted 0 ms 0 KB -