#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int N = 1e5 + 10;
set<int> in[N], out[N], com[N], e[N];
long long ans;
long long par[N], sz[N];
int n, m;
int find( int u ) { return par[u] == u ? u : par[u] = find( par[u] ); }
long long cal( int u ) { return 1LL * ( ( ( int )e[u].size() - sz[u] ) * sz[u] ) + 1LL * sz[u] * ( sz[u]-1 ); }
void merge( int a, int b ) {
vector<pii> v;
a = find( a ), b = find( b );
if( a == b ) return ;
if( in[a].size() + out[a].size() + e[a].size() < in[b].size() + out[b].size() + e[b].size() ) swap( a, b );
ans -= cal( a ), ans -= cal( b );
in[a].erase( b ), in[b].erase( a ), out[a].erase( b ), out[b].erase( a );
for( int x : in[b] ) {
if( out[a].count( x ) ) v.emplace_back( x, a );
out[x].erase( b ), out[x].emplace( a );
}
for( int x : out[b] ) {
if( in[a].count( x ) ) v.emplace_back( x, a );
in[x].erase( b ), in[x].emplace( a );
}
for( int x : in[b] ) in[a].emplace( x );
for( int x : out[b] ) out[a].emplace( x );
for( int x : e[b] ) e[a].emplace( x );
in[b].clear(), out[b].clear(), e[b].clear();
sz[a] += sz[b], par[b] = a;
ans += cal( a );
for( pii x : v ) merge( x.x, x.y );
}
int main()
{
scanf("%d %d",&n,&m);
iota( par, par + N, 0 ), fill( sz, sz + N, 1 );
for( int i = 1 ; i <= n ; i++ ) e[i].emplace( i );
while( m-- ) {
int u, v;
scanf("%d %d",&u,&v);
int a = find( u ), b = find( v );
if( out[b].count( a ) ) merge( u, v );
else {
ans -= cal( b );
in[b].emplace( a ), out[a].emplace( b );
e[b].emplace( u );
ans += cal( b );
}
printf("%lld\n",ans);
}
return 0;
}
Compilation message
joitter2.cpp: In function 'int main()':
joitter2.cpp:44:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
44 | scanf("%d %d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~~
joitter2.cpp:49:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
49 | scanf("%d %d",&u,&v);
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
20716 KB |
Output is correct |
2 |
Correct |
14 ms |
20716 KB |
Output is correct |
3 |
Correct |
14 ms |
20716 KB |
Output is correct |
4 |
Correct |
13 ms |
20716 KB |
Output is correct |
5 |
Correct |
14 ms |
20716 KB |
Output is correct |
6 |
Correct |
13 ms |
20736 KB |
Output is correct |
7 |
Correct |
15 ms |
20716 KB |
Output is correct |
8 |
Correct |
14 ms |
20716 KB |
Output is correct |
9 |
Correct |
14 ms |
20716 KB |
Output is correct |
10 |
Correct |
14 ms |
20736 KB |
Output is correct |
11 |
Correct |
14 ms |
20716 KB |
Output is correct |
12 |
Correct |
14 ms |
20716 KB |
Output is correct |
13 |
Correct |
14 ms |
20736 KB |
Output is correct |
14 |
Correct |
14 ms |
20728 KB |
Output is correct |
15 |
Correct |
14 ms |
20716 KB |
Output is correct |
16 |
Correct |
14 ms |
20716 KB |
Output is correct |
17 |
Correct |
14 ms |
20716 KB |
Output is correct |
18 |
Correct |
14 ms |
20844 KB |
Output is correct |
19 |
Correct |
14 ms |
20716 KB |
Output is correct |
20 |
Correct |
14 ms |
20736 KB |
Output is correct |
21 |
Correct |
15 ms |
20716 KB |
Output is correct |
22 |
Correct |
14 ms |
20716 KB |
Output is correct |
23 |
Correct |
15 ms |
20716 KB |
Output is correct |
24 |
Correct |
14 ms |
20716 KB |
Output is correct |
25 |
Correct |
14 ms |
20716 KB |
Output is correct |
26 |
Correct |
14 ms |
20716 KB |
Output is correct |
27 |
Correct |
14 ms |
20716 KB |
Output is correct |
28 |
Correct |
14 ms |
20716 KB |
Output is correct |
29 |
Correct |
13 ms |
20716 KB |
Output is correct |
30 |
Correct |
14 ms |
20716 KB |
Output is correct |
31 |
Correct |
15 ms |
20716 KB |
Output is correct |
32 |
Correct |
14 ms |
20716 KB |
Output is correct |
33 |
Correct |
14 ms |
20716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
20716 KB |
Output is correct |
2 |
Correct |
14 ms |
20716 KB |
Output is correct |
3 |
Correct |
14 ms |
20716 KB |
Output is correct |
4 |
Correct |
13 ms |
20716 KB |
Output is correct |
5 |
Correct |
14 ms |
20716 KB |
Output is correct |
6 |
Correct |
13 ms |
20736 KB |
Output is correct |
7 |
Correct |
15 ms |
20716 KB |
Output is correct |
8 |
Correct |
14 ms |
20716 KB |
Output is correct |
9 |
Correct |
14 ms |
20716 KB |
Output is correct |
10 |
Correct |
14 ms |
20736 KB |
Output is correct |
11 |
Correct |
14 ms |
20716 KB |
Output is correct |
12 |
Correct |
14 ms |
20716 KB |
Output is correct |
13 |
Correct |
14 ms |
20736 KB |
Output is correct |
14 |
Correct |
14 ms |
20728 KB |
Output is correct |
15 |
Correct |
14 ms |
20716 KB |
Output is correct |
16 |
Correct |
14 ms |
20716 KB |
Output is correct |
17 |
Correct |
14 ms |
20716 KB |
Output is correct |
18 |
Correct |
14 ms |
20844 KB |
Output is correct |
19 |
Correct |
14 ms |
20716 KB |
Output is correct |
20 |
Correct |
14 ms |
20736 KB |
Output is correct |
21 |
Correct |
15 ms |
20716 KB |
Output is correct |
22 |
Correct |
14 ms |
20716 KB |
Output is correct |
23 |
Correct |
15 ms |
20716 KB |
Output is correct |
24 |
Correct |
14 ms |
20716 KB |
Output is correct |
25 |
Correct |
14 ms |
20716 KB |
Output is correct |
26 |
Correct |
14 ms |
20716 KB |
Output is correct |
27 |
Correct |
14 ms |
20716 KB |
Output is correct |
28 |
Correct |
14 ms |
20716 KB |
Output is correct |
29 |
Correct |
13 ms |
20716 KB |
Output is correct |
30 |
Correct |
14 ms |
20716 KB |
Output is correct |
31 |
Correct |
15 ms |
20716 KB |
Output is correct |
32 |
Correct |
14 ms |
20716 KB |
Output is correct |
33 |
Correct |
14 ms |
20716 KB |
Output is correct |
34 |
Correct |
18 ms |
20844 KB |
Output is correct |
35 |
Correct |
130 ms |
26528 KB |
Output is correct |
36 |
Correct |
157 ms |
29420 KB |
Output is correct |
37 |
Correct |
165 ms |
29548 KB |
Output is correct |
38 |
Correct |
185 ms |
29164 KB |
Output is correct |
39 |
Correct |
17 ms |
20844 KB |
Output is correct |
40 |
Correct |
18 ms |
21100 KB |
Output is correct |
41 |
Correct |
17 ms |
20972 KB |
Output is correct |
42 |
Correct |
16 ms |
20844 KB |
Output is correct |
43 |
Correct |
17 ms |
20972 KB |
Output is correct |
44 |
Correct |
17 ms |
20972 KB |
Output is correct |
45 |
Correct |
16 ms |
20844 KB |
Output is correct |
46 |
Correct |
16 ms |
20844 KB |
Output is correct |
47 |
Correct |
18 ms |
20972 KB |
Output is correct |
48 |
Correct |
17 ms |
20972 KB |
Output is correct |
49 |
Correct |
26 ms |
21740 KB |
Output is correct |
50 |
Correct |
159 ms |
29548 KB |
Output is correct |
51 |
Correct |
22 ms |
21228 KB |
Output is correct |
52 |
Correct |
145 ms |
27884 KB |
Output is correct |
53 |
Correct |
24 ms |
21740 KB |
Output is correct |
54 |
Correct |
152 ms |
28800 KB |
Output is correct |
55 |
Correct |
20 ms |
21356 KB |
Output is correct |
56 |
Correct |
20 ms |
21356 KB |
Output is correct |
57 |
Correct |
21 ms |
21740 KB |
Output is correct |
58 |
Correct |
21 ms |
21740 KB |
Output is correct |
59 |
Correct |
17 ms |
20844 KB |
Output is correct |
60 |
Correct |
125 ms |
25784 KB |
Output is correct |
61 |
Correct |
20 ms |
20972 KB |
Output is correct |
62 |
Correct |
159 ms |
29036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
20716 KB |
Output is correct |
2 |
Correct |
14 ms |
20716 KB |
Output is correct |
3 |
Correct |
14 ms |
20716 KB |
Output is correct |
4 |
Correct |
13 ms |
20716 KB |
Output is correct |
5 |
Correct |
14 ms |
20716 KB |
Output is correct |
6 |
Correct |
13 ms |
20736 KB |
Output is correct |
7 |
Correct |
15 ms |
20716 KB |
Output is correct |
8 |
Correct |
14 ms |
20716 KB |
Output is correct |
9 |
Correct |
14 ms |
20716 KB |
Output is correct |
10 |
Correct |
14 ms |
20736 KB |
Output is correct |
11 |
Correct |
14 ms |
20716 KB |
Output is correct |
12 |
Correct |
14 ms |
20716 KB |
Output is correct |
13 |
Correct |
14 ms |
20736 KB |
Output is correct |
14 |
Correct |
14 ms |
20728 KB |
Output is correct |
15 |
Correct |
14 ms |
20716 KB |
Output is correct |
16 |
Correct |
14 ms |
20716 KB |
Output is correct |
17 |
Correct |
14 ms |
20716 KB |
Output is correct |
18 |
Correct |
14 ms |
20844 KB |
Output is correct |
19 |
Correct |
14 ms |
20716 KB |
Output is correct |
20 |
Correct |
14 ms |
20736 KB |
Output is correct |
21 |
Correct |
15 ms |
20716 KB |
Output is correct |
22 |
Correct |
14 ms |
20716 KB |
Output is correct |
23 |
Correct |
15 ms |
20716 KB |
Output is correct |
24 |
Correct |
14 ms |
20716 KB |
Output is correct |
25 |
Correct |
14 ms |
20716 KB |
Output is correct |
26 |
Correct |
14 ms |
20716 KB |
Output is correct |
27 |
Correct |
14 ms |
20716 KB |
Output is correct |
28 |
Correct |
14 ms |
20716 KB |
Output is correct |
29 |
Correct |
13 ms |
20716 KB |
Output is correct |
30 |
Correct |
14 ms |
20716 KB |
Output is correct |
31 |
Correct |
15 ms |
20716 KB |
Output is correct |
32 |
Correct |
14 ms |
20716 KB |
Output is correct |
33 |
Correct |
14 ms |
20716 KB |
Output is correct |
34 |
Correct |
18 ms |
20844 KB |
Output is correct |
35 |
Correct |
130 ms |
26528 KB |
Output is correct |
36 |
Correct |
157 ms |
29420 KB |
Output is correct |
37 |
Correct |
165 ms |
29548 KB |
Output is correct |
38 |
Correct |
185 ms |
29164 KB |
Output is correct |
39 |
Correct |
17 ms |
20844 KB |
Output is correct |
40 |
Correct |
18 ms |
21100 KB |
Output is correct |
41 |
Correct |
17 ms |
20972 KB |
Output is correct |
42 |
Correct |
16 ms |
20844 KB |
Output is correct |
43 |
Correct |
17 ms |
20972 KB |
Output is correct |
44 |
Correct |
17 ms |
20972 KB |
Output is correct |
45 |
Correct |
16 ms |
20844 KB |
Output is correct |
46 |
Correct |
16 ms |
20844 KB |
Output is correct |
47 |
Correct |
18 ms |
20972 KB |
Output is correct |
48 |
Correct |
17 ms |
20972 KB |
Output is correct |
49 |
Correct |
26 ms |
21740 KB |
Output is correct |
50 |
Correct |
159 ms |
29548 KB |
Output is correct |
51 |
Correct |
22 ms |
21228 KB |
Output is correct |
52 |
Correct |
145 ms |
27884 KB |
Output is correct |
53 |
Correct |
24 ms |
21740 KB |
Output is correct |
54 |
Correct |
152 ms |
28800 KB |
Output is correct |
55 |
Correct |
20 ms |
21356 KB |
Output is correct |
56 |
Correct |
20 ms |
21356 KB |
Output is correct |
57 |
Correct |
21 ms |
21740 KB |
Output is correct |
58 |
Correct |
21 ms |
21740 KB |
Output is correct |
59 |
Correct |
17 ms |
20844 KB |
Output is correct |
60 |
Correct |
125 ms |
25784 KB |
Output is correct |
61 |
Correct |
20 ms |
20972 KB |
Output is correct |
62 |
Correct |
159 ms |
29036 KB |
Output is correct |
63 |
Correct |
610 ms |
73244 KB |
Output is correct |
64 |
Correct |
607 ms |
73116 KB |
Output is correct |
65 |
Correct |
604 ms |
73068 KB |
Output is correct |
66 |
Correct |
221 ms |
29804 KB |
Output is correct |
67 |
Correct |
495 ms |
36204 KB |
Output is correct |
68 |
Correct |
215 ms |
29932 KB |
Output is correct |
69 |
Correct |
466 ms |
36908 KB |
Output is correct |
70 |
Correct |
247 ms |
29856 KB |
Output is correct |
71 |
Correct |
254 ms |
29932 KB |
Output is correct |
72 |
Correct |
475 ms |
36332 KB |
Output is correct |
73 |
Correct |
480 ms |
36204 KB |
Output is correct |
74 |
Correct |
1099 ms |
48536 KB |
Output is correct |
75 |
Correct |
714 ms |
42732 KB |
Output is correct |
76 |
Correct |
834 ms |
47724 KB |
Output is correct |
77 |
Correct |
833 ms |
47724 KB |
Output is correct |
78 |
Correct |
276 ms |
35692 KB |
Output is correct |
79 |
Correct |
525 ms |
39020 KB |
Output is correct |
80 |
Correct |
256 ms |
35692 KB |
Output is correct |
81 |
Correct |
448 ms |
39160 KB |
Output is correct |
82 |
Correct |
863 ms |
57216 KB |
Output is correct |
83 |
Correct |
862 ms |
57324 KB |
Output is correct |
84 |
Correct |
823 ms |
71312 KB |
Output is correct |
85 |
Correct |
849 ms |
71440 KB |
Output is correct |
86 |
Correct |
326 ms |
30776 KB |
Output is correct |
87 |
Correct |
400 ms |
33004 KB |
Output is correct |
88 |
Correct |
485 ms |
36332 KB |
Output is correct |
89 |
Correct |
832 ms |
46828 KB |
Output is correct |