#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
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 time |
Memory |
Grader output |
1 |
Correct |
30 ms |
56680 KB |
Output is correct |
2 |
Correct |
25 ms |
56660 KB |
Output is correct |
3 |
Correct |
26 ms |
56676 KB |
Output is correct |
4 |
Correct |
25 ms |
56660 KB |
Output is correct |
5 |
Correct |
25 ms |
56648 KB |
Output is correct |
6 |
Correct |
25 ms |
56692 KB |
Output is correct |
7 |
Correct |
25 ms |
56708 KB |
Output is correct |
8 |
Correct |
25 ms |
56784 KB |
Output is correct |
9 |
Correct |
26 ms |
56708 KB |
Output is correct |
10 |
Correct |
25 ms |
56724 KB |
Output is correct |
11 |
Correct |
26 ms |
56704 KB |
Output is correct |
12 |
Correct |
30 ms |
56700 KB |
Output is correct |
13 |
Correct |
27 ms |
56680 KB |
Output is correct |
14 |
Correct |
25 ms |
56768 KB |
Output is correct |
15 |
Correct |
25 ms |
56644 KB |
Output is correct |
16 |
Correct |
26 ms |
56660 KB |
Output is correct |
17 |
Correct |
26 ms |
56712 KB |
Output is correct |
18 |
Correct |
26 ms |
56632 KB |
Output is correct |
19 |
Correct |
26 ms |
56660 KB |
Output is correct |
20 |
Correct |
27 ms |
56712 KB |
Output is correct |
21 |
Correct |
27 ms |
56756 KB |
Output is correct |
22 |
Correct |
27 ms |
56640 KB |
Output is correct |
23 |
Correct |
27 ms |
56672 KB |
Output is correct |
24 |
Correct |
31 ms |
56660 KB |
Output is correct |
25 |
Correct |
26 ms |
56740 KB |
Output is correct |
26 |
Correct |
26 ms |
56660 KB |
Output is correct |
27 |
Correct |
25 ms |
56660 KB |
Output is correct |
28 |
Correct |
28 ms |
56660 KB |
Output is correct |
29 |
Correct |
27 ms |
56652 KB |
Output is correct |
30 |
Correct |
28 ms |
56660 KB |
Output is correct |
31 |
Correct |
28 ms |
56768 KB |
Output is correct |
32 |
Correct |
28 ms |
56716 KB |
Output is correct |
33 |
Correct |
33 ms |
56732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
56680 KB |
Output is correct |
2 |
Correct |
25 ms |
56660 KB |
Output is correct |
3 |
Correct |
26 ms |
56676 KB |
Output is correct |
4 |
Correct |
25 ms |
56660 KB |
Output is correct |
5 |
Correct |
25 ms |
56648 KB |
Output is correct |
6 |
Correct |
25 ms |
56692 KB |
Output is correct |
7 |
Correct |
25 ms |
56708 KB |
Output is correct |
8 |
Correct |
25 ms |
56784 KB |
Output is correct |
9 |
Correct |
26 ms |
56708 KB |
Output is correct |
10 |
Correct |
25 ms |
56724 KB |
Output is correct |
11 |
Correct |
26 ms |
56704 KB |
Output is correct |
12 |
Correct |
30 ms |
56700 KB |
Output is correct |
13 |
Correct |
27 ms |
56680 KB |
Output is correct |
14 |
Correct |
25 ms |
56768 KB |
Output is correct |
15 |
Correct |
25 ms |
56644 KB |
Output is correct |
16 |
Correct |
26 ms |
56660 KB |
Output is correct |
17 |
Correct |
26 ms |
56712 KB |
Output is correct |
18 |
Correct |
26 ms |
56632 KB |
Output is correct |
19 |
Correct |
26 ms |
56660 KB |
Output is correct |
20 |
Correct |
27 ms |
56712 KB |
Output is correct |
21 |
Correct |
27 ms |
56756 KB |
Output is correct |
22 |
Correct |
27 ms |
56640 KB |
Output is correct |
23 |
Correct |
27 ms |
56672 KB |
Output is correct |
24 |
Correct |
31 ms |
56660 KB |
Output is correct |
25 |
Correct |
26 ms |
56740 KB |
Output is correct |
26 |
Correct |
26 ms |
56660 KB |
Output is correct |
27 |
Correct |
25 ms |
56660 KB |
Output is correct |
28 |
Correct |
28 ms |
56660 KB |
Output is correct |
29 |
Correct |
27 ms |
56652 KB |
Output is correct |
30 |
Correct |
28 ms |
56660 KB |
Output is correct |
31 |
Correct |
28 ms |
56768 KB |
Output is correct |
32 |
Correct |
28 ms |
56716 KB |
Output is correct |
33 |
Correct |
33 ms |
56732 KB |
Output is correct |
34 |
Correct |
29 ms |
56852 KB |
Output is correct |
35 |
Correct |
97 ms |
65232 KB |
Output is correct |
36 |
Correct |
144 ms |
68552 KB |
Output is correct |
37 |
Correct |
130 ms |
68672 KB |
Output is correct |
38 |
Correct |
127 ms |
68152 KB |
Output is correct |
39 |
Correct |
38 ms |
57100 KB |
Output is correct |
40 |
Correct |
43 ms |
58372 KB |
Output is correct |
41 |
Correct |
39 ms |
58188 KB |
Output is correct |
42 |
Correct |
29 ms |
57036 KB |
Output is correct |
43 |
Correct |
32 ms |
57068 KB |
Output is correct |
44 |
Correct |
30 ms |
56992 KB |
Output is correct |
45 |
Correct |
29 ms |
57120 KB |
Output is correct |
46 |
Correct |
28 ms |
57040 KB |
Output is correct |
47 |
Correct |
31 ms |
57640 KB |
Output is correct |
48 |
Correct |
35 ms |
57728 KB |
Output is correct |
49 |
Correct |
52 ms |
58740 KB |
Output is correct |
50 |
Correct |
141 ms |
68732 KB |
Output is correct |
51 |
Correct |
34 ms |
57688 KB |
Output is correct |
52 |
Correct |
110 ms |
66792 KB |
Output is correct |
53 |
Correct |
49 ms |
58572 KB |
Output is correct |
54 |
Correct |
153 ms |
67728 KB |
Output is correct |
55 |
Correct |
35 ms |
57828 KB |
Output is correct |
56 |
Correct |
36 ms |
57808 KB |
Output is correct |
57 |
Correct |
44 ms |
58196 KB |
Output is correct |
58 |
Correct |
38 ms |
58236 KB |
Output is correct |
59 |
Correct |
34 ms |
58060 KB |
Output is correct |
60 |
Correct |
91 ms |
65360 KB |
Output is correct |
61 |
Correct |
33 ms |
57684 KB |
Output is correct |
62 |
Correct |
130 ms |
67964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
56680 KB |
Output is correct |
2 |
Correct |
25 ms |
56660 KB |
Output is correct |
3 |
Correct |
26 ms |
56676 KB |
Output is correct |
4 |
Correct |
25 ms |
56660 KB |
Output is correct |
5 |
Correct |
25 ms |
56648 KB |
Output is correct |
6 |
Correct |
25 ms |
56692 KB |
Output is correct |
7 |
Correct |
25 ms |
56708 KB |
Output is correct |
8 |
Correct |
25 ms |
56784 KB |
Output is correct |
9 |
Correct |
26 ms |
56708 KB |
Output is correct |
10 |
Correct |
25 ms |
56724 KB |
Output is correct |
11 |
Correct |
26 ms |
56704 KB |
Output is correct |
12 |
Correct |
30 ms |
56700 KB |
Output is correct |
13 |
Correct |
27 ms |
56680 KB |
Output is correct |
14 |
Correct |
25 ms |
56768 KB |
Output is correct |
15 |
Correct |
25 ms |
56644 KB |
Output is correct |
16 |
Correct |
26 ms |
56660 KB |
Output is correct |
17 |
Correct |
26 ms |
56712 KB |
Output is correct |
18 |
Correct |
26 ms |
56632 KB |
Output is correct |
19 |
Correct |
26 ms |
56660 KB |
Output is correct |
20 |
Correct |
27 ms |
56712 KB |
Output is correct |
21 |
Correct |
27 ms |
56756 KB |
Output is correct |
22 |
Correct |
27 ms |
56640 KB |
Output is correct |
23 |
Correct |
27 ms |
56672 KB |
Output is correct |
24 |
Correct |
31 ms |
56660 KB |
Output is correct |
25 |
Correct |
26 ms |
56740 KB |
Output is correct |
26 |
Correct |
26 ms |
56660 KB |
Output is correct |
27 |
Correct |
25 ms |
56660 KB |
Output is correct |
28 |
Correct |
28 ms |
56660 KB |
Output is correct |
29 |
Correct |
27 ms |
56652 KB |
Output is correct |
30 |
Correct |
28 ms |
56660 KB |
Output is correct |
31 |
Correct |
28 ms |
56768 KB |
Output is correct |
32 |
Correct |
28 ms |
56716 KB |
Output is correct |
33 |
Correct |
33 ms |
56732 KB |
Output is correct |
34 |
Correct |
29 ms |
56852 KB |
Output is correct |
35 |
Correct |
97 ms |
65232 KB |
Output is correct |
36 |
Correct |
144 ms |
68552 KB |
Output is correct |
37 |
Correct |
130 ms |
68672 KB |
Output is correct |
38 |
Correct |
127 ms |
68152 KB |
Output is correct |
39 |
Correct |
38 ms |
57100 KB |
Output is correct |
40 |
Correct |
43 ms |
58372 KB |
Output is correct |
41 |
Correct |
39 ms |
58188 KB |
Output is correct |
42 |
Correct |
29 ms |
57036 KB |
Output is correct |
43 |
Correct |
32 ms |
57068 KB |
Output is correct |
44 |
Correct |
30 ms |
56992 KB |
Output is correct |
45 |
Correct |
29 ms |
57120 KB |
Output is correct |
46 |
Correct |
28 ms |
57040 KB |
Output is correct |
47 |
Correct |
31 ms |
57640 KB |
Output is correct |
48 |
Correct |
35 ms |
57728 KB |
Output is correct |
49 |
Correct |
52 ms |
58740 KB |
Output is correct |
50 |
Correct |
141 ms |
68732 KB |
Output is correct |
51 |
Correct |
34 ms |
57688 KB |
Output is correct |
52 |
Correct |
110 ms |
66792 KB |
Output is correct |
53 |
Correct |
49 ms |
58572 KB |
Output is correct |
54 |
Correct |
153 ms |
67728 KB |
Output is correct |
55 |
Correct |
35 ms |
57828 KB |
Output is correct |
56 |
Correct |
36 ms |
57808 KB |
Output is correct |
57 |
Correct |
44 ms |
58196 KB |
Output is correct |
58 |
Correct |
38 ms |
58236 KB |
Output is correct |
59 |
Correct |
34 ms |
58060 KB |
Output is correct |
60 |
Correct |
91 ms |
65360 KB |
Output is correct |
61 |
Correct |
33 ms |
57684 KB |
Output is correct |
62 |
Correct |
130 ms |
67964 KB |
Output is correct |
63 |
Correct |
420 ms |
107932 KB |
Output is correct |
64 |
Correct |
428 ms |
107888 KB |
Output is correct |
65 |
Correct |
454 ms |
107932 KB |
Output is correct |
66 |
Correct |
256 ms |
82596 KB |
Output is correct |
67 |
Correct |
988 ms |
170612 KB |
Output is correct |
68 |
Correct |
201 ms |
77848 KB |
Output is correct |
69 |
Correct |
267 ms |
77816 KB |
Output is correct |
70 |
Correct |
291 ms |
80300 KB |
Output is correct |
71 |
Correct |
295 ms |
80204 KB |
Output is correct |
72 |
Correct |
722 ms |
112348 KB |
Output is correct |
73 |
Correct |
742 ms |
117624 KB |
Output is correct |
74 |
Correct |
1979 ms |
167260 KB |
Output is correct |
75 |
Correct |
588 ms |
91760 KB |
Output is correct |
76 |
Correct |
1224 ms |
123556 KB |
Output is correct |
77 |
Correct |
1172 ms |
123840 KB |
Output is correct |
78 |
Correct |
335 ms |
85996 KB |
Output is correct |
79 |
Correct |
435 ms |
95052 KB |
Output is correct |
80 |
Correct |
274 ms |
82432 KB |
Output is correct |
81 |
Correct |
421 ms |
89592 KB |
Output is correct |
82 |
Correct |
988 ms |
114616 KB |
Output is correct |
83 |
Correct |
1000 ms |
114764 KB |
Output is correct |
84 |
Correct |
985 ms |
138176 KB |
Output is correct |
85 |
Correct |
963 ms |
138164 KB |
Output is correct |
86 |
Correct |
656 ms |
159820 KB |
Output is correct |
87 |
Correct |
670 ms |
162944 KB |
Output is correct |
88 |
Correct |
759 ms |
115228 KB |
Output is correct |
89 |
Correct |
1123 ms |
119556 KB |
Output is correct |