Submission #542776

#TimeUsernameProblemLanguageResultExecution timeMemory
542776chonkaMaking Friends on Joitter is Fun (JOI20_joitter2)C++98
100 / 100
1979 ms170612 KiB
#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 ] ) { swap ( k1 , 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 ] ; } 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[ get ( 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 (stderr)

joitter2.cpp: In function 'void unite(int, int)':
joitter2.cpp:40:20: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   40 |         for ( auto [ aux , cnt ] : entering[ k2 ] ) {
      |                    ^
joitter2.cpp:52:20: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   52 |         for ( auto [ aux , cnt ] : leaving[ k2 ] ) {
      |                    ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...