# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
542751 | 2022-03-27T21:55:28 Z | chonka | Making Friends on Joitter is Fun (JOI20_joitter2) | C++ | 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
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 25 ms | 42572 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 25 ms | 42572 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 25 ms | 42572 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |