Submission #542775

# Submission time Handle Problem Language Result Execution time Memory
542775 2022-03-28T00:55:17 Z chonka Making Friends on Joitter is Fun (JOI20_joitter2) C++
0 / 100
30 ms 56772 KB
#include<bits/stdc++.h>
using namespace std ;

#define MAXN 300007

int n , m ;
pair < int , int > a[ MAXN ] ;
set < int > v[ MAXN ] ;

map < int , int > entering[ MAXN ] ;
map < int , int > leaving[ MAXN ] ;
set < int > all_entering[ MAXN ] ;

int sm_entering[ MAXN ] ;

int prv[ MAXN ] ;
int cnt[ MAXN ] , sz[ MAXN ] ;

long long ans = 0 ;

int get ( int x ) {
    if ( prv[ x ] == -1 ) { return x ; }
    int y = get ( prv[ x ] ) ;
    prv[ x ] = y ;
    return y ;
}
void unite ( int x , int y ) {
    int k1 = get ( x ) ;
    int k2 = get ( y ) ;
    vector < int > to_unite ;
    if ( k1 != k2 ) {
        ans -= 1LL * sz[ k1 ] * ( sz[ k1 ] - 1 ) ;
        ans -= 1LL * sz[ k1 ] * sm_entering[ k1 ] ;
        ans -= 1LL * sz[ k2 ] * ( sz[ k2 ] - 1 ) ;
        ans -= 1LL * sz[ k2 ] * sm_entering[ k2 ] ;
        
        if ( sz[ k1 ] >= sz[ k2 ] ) {
            for ( auto [ aux , cnt ] : entering[ k2 ] ) {
                leaving[ aux ][ k2 ] -= cnt ;
                if ( aux != k1 ) {
                    sm_entering[ k1 ] += cnt ;
                    entering[ k1 ][ aux ] += cnt ;
                    leaving[ aux ][ k1 ] += cnt ;

                    if ( leaving[ k1 ].find ( aux ) != leaving[ k1 ].end ( ) ) {
                        to_unite.push_back ( aux ) ;
                    }
                }
            }
            for ( auto [ aux , cnt ] : leaving[ k2 ] ) {
                entering[ aux ][ k2 ] -= cnt ;
                sm_entering[ aux ] -= cnt ;
                if ( aux != k1 ) {
                    sm_entering[ aux ] += cnt ;
                    entering[ aux ][ k1 ] += cnt ;
                    leaving[ k1 ][ aux ] += cnt ;

                    if ( entering[ k1 ].find ( aux ) != entering[ k1 ].end ( ) ) {
                        to_unite.push_back ( aux ) ;
                    }
                }
            }
            for ( auto aux : all_entering[ k2 ] ) {
                if ( get ( aux ) != k1 && get ( aux ) != k2 && all_entering[ k1 ].find ( aux ) != all_entering[ k1 ].end ( ) ) {
                    -- sm_entering[ k1 ] ;
                    -- entering[ k1 ][ get ( aux ) ] ;
                    -- leaving[ get ( aux ) ][ k1 ] ;
                }
                all_entering[ k1 ].insert ( aux ) ;
            }
            prv[ k2 ] = k1 ;
            sz[ k1 ] += sz[ k2 ] ;
            ans += 1LL * sz[ k1 ] * ( sz[ k1 ] - 1 ) ;
            ans += 1LL * sz[ k1 ] * sm_entering[ k1 ] ;
        }
        else {
            for ( auto [ aux , cnt ] : entering[ k1 ] ) {
                leaving[ aux ][ k1 ] -= cnt ;
                if ( aux != k2 ) {
                    sm_entering[ k2 ] += cnt ;
                    entering[ k2 ][ aux ] += cnt ;
                    leaving[ aux ][ k2 ] += cnt ;

                    if ( leaving[ k2 ].find ( aux ) != leaving[ k2 ].end ( ) ) {
                        to_unite.push_back ( aux ) ;
                    }
                }
            }
            for ( auto [ aux , cnt ] : leaving[ k1 ] ) {
                entering[ aux ][ k1 ] -= cnt ;
                sm_entering[ aux ] -= cnt ;
                if ( aux != k2 ) {
                    sm_entering[ aux ] += cnt ;
                    entering[ aux ][ k2 ] += cnt ;
                    leaving[ k2 ][ aux ] += cnt ;

                    if ( entering[ k2 ].find ( aux ) != entering[ k2 ].end ( ) ) {
                        to_unite.push_back ( aux ) ;
                    }
                }
            }
            for ( auto aux : all_entering[ k1 ] ) {
                if ( get ( aux ) != k1 && get ( aux ) != k2 && all_entering[ k2 ].find ( aux ) != all_entering[ k2 ].end ( ) ) {
                    -- sm_entering[ k2 ] ;
                    -- entering[ k2 ][ get ( aux ) ] ;
                    -- leaving[ get ( aux ) ][ k2 ] ;
                }
                all_entering[ k2 ].insert ( aux ) ;
            }
            prv[ k1 ] = k2 ;
            sz[ k2 ] += sz[ k1 ] ;
            ans += 1LL * sz[ k2 ] * ( sz[ k2 ] - 1 ) ;
            ans += 1LL * sz[ k2 ] * sm_entering[ k2 ] ;
        }
    }
    for ( auto nw : to_unite ) {
        if ( get ( x ) != get ( nw ) ) {
            unite ( x , nw ) ;
        }
    }
}

void input ( ) {
    cin >> n >> m ;
    for ( int i = 1 ; i <= m ; ++ i ) {
        cin >> a[ i ].first >> a[ i ].second ;
    }
}

void solve ( ) {
    for ( int i = 1 ; i <= n ; ++ i ) {
        prv[ i ] = -1 , sz[ i ] = 1 ;
    }
    for ( int i = 1 ; i <= m ; ++ i ) {
        int x = a[ i ].first , y = a[ i ].second ;
        if ( get ( x ) != get ( y ) ) {
            if ( leaving[ get ( y ) ].find ( get ( x ) ) != leaving[ get ( y ) ].end ( ) ) {
                unite ( x , y ) ;
            }
            else {
                if ( all_entering[ get ( y ) ].find ( x ) == all_entering[ get ( y ) ].end ( ) ) {
                    ans += sz[ y ] ;
                    ++ leaving[ get ( x ) ][ get ( y ) ] ;
                    ++ entering[ get ( y ) ][ get ( x ) ] ;
                    ++ sm_entering[ get ( y ) ] ;
                    all_entering[ get ( y ) ].insert ( x ) ;
                }
            }
        }
        cout << ans << "\n" ;
    }
}


int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    int t = 1 ;
    // cin >> t ;
    while ( t -- ) {
        input ( ) ;
        solve ( ) ;
    }
    return 0 ;
}

Compilation message

joitter2.cpp: In function 'void unite(int, int)':
joitter2.cpp:38:24: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   38 |             for ( auto [ aux , cnt ] : entering[ k2 ] ) {
      |                        ^
joitter2.cpp:50:24: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   50 |             for ( auto [ aux , cnt ] : leaving[ k2 ] ) {
      |                        ^
joitter2.cpp:77:24: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   77 |             for ( auto [ aux , cnt ] : entering[ k1 ] ) {
      |                        ^
joitter2.cpp:89:24: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   89 |             for ( auto [ aux , cnt ] : leaving[ k1 ] ) {
      |                        ^
# Verdict Execution time Memory Grader output
1 Correct 26 ms 56568 KB Output is correct
2 Correct 28 ms 56600 KB Output is correct
3 Correct 29 ms 56676 KB Output is correct
4 Correct 30 ms 56612 KB Output is correct
5 Correct 27 ms 56668 KB Output is correct
6 Incorrect 26 ms 56772 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 26 ms 56568 KB Output is correct
2 Correct 28 ms 56600 KB Output is correct
3 Correct 29 ms 56676 KB Output is correct
4 Correct 30 ms 56612 KB Output is correct
5 Correct 27 ms 56668 KB Output is correct
6 Incorrect 26 ms 56772 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 26 ms 56568 KB Output is correct
2 Correct 28 ms 56600 KB Output is correct
3 Correct 29 ms 56676 KB Output is correct
4 Correct 30 ms 56612 KB Output is correct
5 Correct 27 ms 56668 KB Output is correct
6 Incorrect 26 ms 56772 KB Output isn't correct
7 Halted 0 ms 0 KB -