답안 #542751

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
542751 2022-03-27T21:55:28 Z chonka 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++
0 / 100
25 ms 42572 KB
#include<bits/stdc++.h>
using namespace std ;

#define MAXN 300007

int n , m ;
pair < int , int > a[ MAXN ] ;
set < int > v[ MAXN ] ;
set < int > entering[ MAXN ] ;
set < pair < int , int > > leaving[ 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 ) ;
    if ( k1 != k2 ) {
        ans -= 1LL * sz[ k1 ] * ( sz[ k1 ] - 1 ) ;
        ans -= 1LL * sz[ k1 ] * entering[ k1 ].size ( ) ;
        ans -= 1LL * sz[ k2 ] * ( sz[ k2 ] - 1 ) ;
        ans -= 1LL * sz[ k2 ] * entering[ k2 ].size ( ) ;
        
        if ( sz[ k1 ] >= sz[ k2 ] ) {
            for ( auto aux : entering[ k2 ] ) {
                if ( get ( aux ) != k1 ) {
                    entering[ k1 ].insert ( aux ) ;
                }
            }
            for ( auto [ from , to ] : leaving[ k2 ] ) {
                if ( get ( to ) == k1 ) {
                    entering[ k1 ].erase ( from ) ;
                }
                else {
                    leaving[ k1 ].insert ( { from , to } ) ;
                }
            }
            sz[ k1 ] += sz[ k2 ] ;
            prv[ k2 ] = k1 ;

            ans += 1LL * sz[ k1 ] * ( sz[ k1 ] - 1 ) ;
            ans += 1LL * sz[ k1 ] * entering[ k1 ].size ( ) ;
        }
        else {
            for ( auto aux : entering[ k1 ] ) {
                if ( get ( aux ) != k2 ) {
                    entering[ k2 ].insert ( aux ) ;
                }
            }
            for ( auto [ from , to ] : leaving[ k1 ] ) {
                if ( get ( to ) == k2 ) {
                    entering[ k2 ].erase ( from ) ;
                }
                else {
                    leaving[ k2 ].insert ( { from , to } ) ;
                }
            }
            sz[ k2 ] += sz[ k1 ] ;
            prv[ k1 ] = k2 ;

            ans += 1LL * sz[ k2 ] * ( sz[ k2 ] - 1 ) ;
            ans += 1LL * sz[ k2 ] * entering[ k2 ].size ( ) ;
        }
    }
}

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 ;
        v[ x ].insert ( y ) ;
        if ( v[ y ].find ( x ) != v[ y ].end ( ) ) {
            unite ( x , y ) ;
        }
        else {
            int root = get ( y ) ;
            if ( entering[ root ].find ( x ) == entering[ root ].end ( ) ) {
                ans += sz[ root ] ;
            }
            entering[ get ( y ) ].insert ( x ) ;
            leaving[ get ( x ) ].insert ( { x , y } ) ;
        }
        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 [ from , to ] : leaving[ k2 ] ) {
      |                        ^
joitter2.cpp:58:24: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   58 |             for ( auto [ from , to ] : leaving[ k1 ] ) {
      |                        ^
# 결과 실행 시간 메모리 Grader output
1 Incorrect 25 ms 42572 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 25 ms 42572 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 25 ms 42572 KB Output isn't correct
2 Halted 0 ms 0 KB -