#include<bits/stdc++.h>
#define ll long long
using namespace std;
unordered_map<ll,ll> f[300009];
unordered_map<ll,ll> g[300009];
ll p[300009],s[300009];
int br[309][309];
ll ans;
ll P(ll x){
if(x == p[x]) return x;
p[x] = P(p[x]);
return p[x];
}
void un(ll x, ll y){
x = P(x); y = P(y);
if(x == y ) return;
if(f[x].size() + g[x].size() >
f[y].size() + g[y].size()) swap(x, y);
ans -= s[x] * (ll)f[x].size() - s[x];
ans -= s[y] * (ll)f[y].size() - s[y];
s[y] += s[x];
p[x] = y;
vector<int> v;
for(auto it = f[x].begin(); it != f[x].end(); it++){
f[y][it->first]=1;
int u = P(it->first);
if(g[y].find(u) != g[y].end())
v.push_back(it->first);
g[u][y] = 1;
if(g[u].find(x) != g[u].end())
g[u].erase(g[u].find(x));
}
for(auto it = g[x].begin(); it != g[x].end(); it++){
g[y][it->first] = 1;
if(g[it->first].find(y) != g[it->first].end())
v.push_back(it->first);
}
ans += s[y] * (ll)f[y].size() - s[y];
for(int u : v)
un(y, u);
}
main(){
ll n, q;
cin >> n >> q;
for(ll i = 1; i <= n; i++)
s[i] = 1, p[i] = i, f[i][i] = 1;
while(q--){
ll a=0, b=0;
cin >> a >> b;
ll c = P(b), d = P(a);
if(f[c].find(a) == f[c].end()){
if(g[c].find(d) != g[c].end()) un(a, b);
else{
f[c][a] = 1,
ans += s[c];
}
g[P(a)][c] = 1;
}
cout<<ans<<endl;
}
}
Compilation message
joitter2.cpp:42:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
33152 KB |
Output is correct |
2 |
Correct |
25 ms |
33152 KB |
Output is correct |
3 |
Correct |
25 ms |
33280 KB |
Output is correct |
4 |
Correct |
25 ms |
33152 KB |
Output is correct |
5 |
Correct |
27 ms |
33408 KB |
Output is correct |
6 |
Correct |
26 ms |
33152 KB |
Output is correct |
7 |
Correct |
33 ms |
33228 KB |
Output is correct |
8 |
Correct |
31 ms |
33280 KB |
Output is correct |
9 |
Correct |
32 ms |
33280 KB |
Output is correct |
10 |
Correct |
26 ms |
33152 KB |
Output is correct |
11 |
Correct |
26 ms |
33280 KB |
Output is correct |
12 |
Correct |
26 ms |
33152 KB |
Output is correct |
13 |
Correct |
25 ms |
33152 KB |
Output is correct |
14 |
Correct |
25 ms |
33152 KB |
Output is correct |
15 |
Correct |
25 ms |
33152 KB |
Output is correct |
16 |
Correct |
26 ms |
33152 KB |
Output is correct |
17 |
Correct |
27 ms |
33272 KB |
Output is correct |
18 |
Correct |
26 ms |
33280 KB |
Output is correct |
19 |
Correct |
26 ms |
33280 KB |
Output is correct |
20 |
Correct |
28 ms |
33400 KB |
Output is correct |
21 |
Correct |
31 ms |
33280 KB |
Output is correct |
22 |
Correct |
26 ms |
33280 KB |
Output is correct |
23 |
Correct |
28 ms |
33280 KB |
Output is correct |
24 |
Correct |
27 ms |
33280 KB |
Output is correct |
25 |
Correct |
31 ms |
33280 KB |
Output is correct |
26 |
Correct |
27 ms |
33272 KB |
Output is correct |
27 |
Correct |
30 ms |
33280 KB |
Output is correct |
28 |
Correct |
25 ms |
33280 KB |
Output is correct |
29 |
Correct |
26 ms |
33280 KB |
Output is correct |
30 |
Correct |
25 ms |
33280 KB |
Output is correct |
31 |
Correct |
31 ms |
33280 KB |
Output is correct |
32 |
Correct |
25 ms |
33152 KB |
Output is correct |
33 |
Correct |
31 ms |
33280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
33152 KB |
Output is correct |
2 |
Correct |
25 ms |
33152 KB |
Output is correct |
3 |
Correct |
25 ms |
33280 KB |
Output is correct |
4 |
Correct |
25 ms |
33152 KB |
Output is correct |
5 |
Correct |
27 ms |
33408 KB |
Output is correct |
6 |
Correct |
26 ms |
33152 KB |
Output is correct |
7 |
Correct |
33 ms |
33228 KB |
Output is correct |
8 |
Correct |
31 ms |
33280 KB |
Output is correct |
9 |
Correct |
32 ms |
33280 KB |
Output is correct |
10 |
Correct |
26 ms |
33152 KB |
Output is correct |
11 |
Correct |
26 ms |
33280 KB |
Output is correct |
12 |
Correct |
26 ms |
33152 KB |
Output is correct |
13 |
Correct |
25 ms |
33152 KB |
Output is correct |
14 |
Correct |
25 ms |
33152 KB |
Output is correct |
15 |
Correct |
25 ms |
33152 KB |
Output is correct |
16 |
Correct |
26 ms |
33152 KB |
Output is correct |
17 |
Correct |
27 ms |
33272 KB |
Output is correct |
18 |
Correct |
26 ms |
33280 KB |
Output is correct |
19 |
Correct |
26 ms |
33280 KB |
Output is correct |
20 |
Correct |
28 ms |
33400 KB |
Output is correct |
21 |
Correct |
31 ms |
33280 KB |
Output is correct |
22 |
Correct |
26 ms |
33280 KB |
Output is correct |
23 |
Correct |
28 ms |
33280 KB |
Output is correct |
24 |
Correct |
27 ms |
33280 KB |
Output is correct |
25 |
Correct |
31 ms |
33280 KB |
Output is correct |
26 |
Correct |
27 ms |
33272 KB |
Output is correct |
27 |
Correct |
30 ms |
33280 KB |
Output is correct |
28 |
Correct |
25 ms |
33280 KB |
Output is correct |
29 |
Correct |
26 ms |
33280 KB |
Output is correct |
30 |
Correct |
25 ms |
33280 KB |
Output is correct |
31 |
Correct |
31 ms |
33280 KB |
Output is correct |
32 |
Correct |
25 ms |
33152 KB |
Output is correct |
33 |
Correct |
31 ms |
33280 KB |
Output is correct |
34 |
Correct |
53 ms |
33280 KB |
Output is correct |
35 |
Correct |
881 ms |
36576 KB |
Output is correct |
36 |
Correct |
888 ms |
38392 KB |
Output is correct |
37 |
Correct |
858 ms |
38520 KB |
Output is correct |
38 |
Correct |
843 ms |
38396 KB |
Output is correct |
39 |
Correct |
37 ms |
33536 KB |
Output is correct |
40 |
Correct |
41 ms |
34176 KB |
Output is correct |
41 |
Correct |
43 ms |
34168 KB |
Output is correct |
42 |
Correct |
37 ms |
33536 KB |
Output is correct |
43 |
Correct |
39 ms |
33664 KB |
Output is correct |
44 |
Correct |
38 ms |
33656 KB |
Output is correct |
45 |
Correct |
38 ms |
33528 KB |
Output is correct |
46 |
Correct |
38 ms |
33528 KB |
Output is correct |
47 |
Correct |
40 ms |
33912 KB |
Output is correct |
48 |
Correct |
41 ms |
33920 KB |
Output is correct |
49 |
Correct |
59 ms |
34424 KB |
Output is correct |
50 |
Correct |
844 ms |
38672 KB |
Output is correct |
51 |
Correct |
58 ms |
33784 KB |
Output is correct |
52 |
Correct |
849 ms |
37496 KB |
Output is correct |
53 |
Correct |
58 ms |
34296 KB |
Output is correct |
54 |
Correct |
831 ms |
38024 KB |
Output is correct |
55 |
Correct |
43 ms |
33912 KB |
Output is correct |
56 |
Correct |
41 ms |
33912 KB |
Output is correct |
57 |
Correct |
40 ms |
34168 KB |
Output is correct |
58 |
Correct |
42 ms |
34176 KB |
Output is correct |
59 |
Correct |
43 ms |
34172 KB |
Output is correct |
60 |
Correct |
827 ms |
36844 KB |
Output is correct |
61 |
Correct |
42 ms |
33912 KB |
Output is correct |
62 |
Correct |
838 ms |
38240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
33152 KB |
Output is correct |
2 |
Correct |
25 ms |
33152 KB |
Output is correct |
3 |
Correct |
25 ms |
33280 KB |
Output is correct |
4 |
Correct |
25 ms |
33152 KB |
Output is correct |
5 |
Correct |
27 ms |
33408 KB |
Output is correct |
6 |
Correct |
26 ms |
33152 KB |
Output is correct |
7 |
Correct |
33 ms |
33228 KB |
Output is correct |
8 |
Correct |
31 ms |
33280 KB |
Output is correct |
9 |
Correct |
32 ms |
33280 KB |
Output is correct |
10 |
Correct |
26 ms |
33152 KB |
Output is correct |
11 |
Correct |
26 ms |
33280 KB |
Output is correct |
12 |
Correct |
26 ms |
33152 KB |
Output is correct |
13 |
Correct |
25 ms |
33152 KB |
Output is correct |
14 |
Correct |
25 ms |
33152 KB |
Output is correct |
15 |
Correct |
25 ms |
33152 KB |
Output is correct |
16 |
Correct |
26 ms |
33152 KB |
Output is correct |
17 |
Correct |
27 ms |
33272 KB |
Output is correct |
18 |
Correct |
26 ms |
33280 KB |
Output is correct |
19 |
Correct |
26 ms |
33280 KB |
Output is correct |
20 |
Correct |
28 ms |
33400 KB |
Output is correct |
21 |
Correct |
31 ms |
33280 KB |
Output is correct |
22 |
Correct |
26 ms |
33280 KB |
Output is correct |
23 |
Correct |
28 ms |
33280 KB |
Output is correct |
24 |
Correct |
27 ms |
33280 KB |
Output is correct |
25 |
Correct |
31 ms |
33280 KB |
Output is correct |
26 |
Correct |
27 ms |
33272 KB |
Output is correct |
27 |
Correct |
30 ms |
33280 KB |
Output is correct |
28 |
Correct |
25 ms |
33280 KB |
Output is correct |
29 |
Correct |
26 ms |
33280 KB |
Output is correct |
30 |
Correct |
25 ms |
33280 KB |
Output is correct |
31 |
Correct |
31 ms |
33280 KB |
Output is correct |
32 |
Correct |
25 ms |
33152 KB |
Output is correct |
33 |
Correct |
31 ms |
33280 KB |
Output is correct |
34 |
Correct |
53 ms |
33280 KB |
Output is correct |
35 |
Correct |
881 ms |
36576 KB |
Output is correct |
36 |
Correct |
888 ms |
38392 KB |
Output is correct |
37 |
Correct |
858 ms |
38520 KB |
Output is correct |
38 |
Correct |
843 ms |
38396 KB |
Output is correct |
39 |
Correct |
37 ms |
33536 KB |
Output is correct |
40 |
Correct |
41 ms |
34176 KB |
Output is correct |
41 |
Correct |
43 ms |
34168 KB |
Output is correct |
42 |
Correct |
37 ms |
33536 KB |
Output is correct |
43 |
Correct |
39 ms |
33664 KB |
Output is correct |
44 |
Correct |
38 ms |
33656 KB |
Output is correct |
45 |
Correct |
38 ms |
33528 KB |
Output is correct |
46 |
Correct |
38 ms |
33528 KB |
Output is correct |
47 |
Correct |
40 ms |
33912 KB |
Output is correct |
48 |
Correct |
41 ms |
33920 KB |
Output is correct |
49 |
Correct |
59 ms |
34424 KB |
Output is correct |
50 |
Correct |
844 ms |
38672 KB |
Output is correct |
51 |
Correct |
58 ms |
33784 KB |
Output is correct |
52 |
Correct |
849 ms |
37496 KB |
Output is correct |
53 |
Correct |
58 ms |
34296 KB |
Output is correct |
54 |
Correct |
831 ms |
38024 KB |
Output is correct |
55 |
Correct |
43 ms |
33912 KB |
Output is correct |
56 |
Correct |
41 ms |
33912 KB |
Output is correct |
57 |
Correct |
40 ms |
34168 KB |
Output is correct |
58 |
Correct |
42 ms |
34176 KB |
Output is correct |
59 |
Correct |
43 ms |
34172 KB |
Output is correct |
60 |
Correct |
827 ms |
36844 KB |
Output is correct |
61 |
Correct |
42 ms |
33912 KB |
Output is correct |
62 |
Correct |
838 ms |
38240 KB |
Output is correct |
63 |
Correct |
1292 ms |
70264 KB |
Output is correct |
64 |
Correct |
1277 ms |
70392 KB |
Output is correct |
65 |
Correct |
1342 ms |
70324 KB |
Output is correct |
66 |
Correct |
744 ms |
50312 KB |
Output is correct |
67 |
Correct |
1264 ms |
92088 KB |
Output is correct |
68 |
Correct |
717 ms |
50592 KB |
Output is correct |
69 |
Correct |
800 ms |
53956 KB |
Output is correct |
70 |
Correct |
750 ms |
50224 KB |
Output is correct |
71 |
Correct |
773 ms |
50312 KB |
Output is correct |
72 |
Correct |
1110 ms |
68920 KB |
Output is correct |
73 |
Correct |
1083 ms |
71192 KB |
Output is correct |
74 |
Correct |
1707 ms |
85908 KB |
Output is correct |
75 |
Correct |
1184 ms |
58808 KB |
Output is correct |
76 |
Correct |
1453 ms |
71116 KB |
Output is correct |
77 |
Correct |
1419 ms |
71208 KB |
Output is correct |
78 |
Correct |
614 ms |
54152 KB |
Output is correct |
79 |
Correct |
1149 ms |
59400 KB |
Output is correct |
80 |
Correct |
610 ms |
55812 KB |
Output is correct |
81 |
Correct |
1108 ms |
60040 KB |
Output is correct |
82 |
Correct |
1081 ms |
65968 KB |
Output is correct |
83 |
Correct |
1065 ms |
65908 KB |
Output is correct |
84 |
Correct |
1032 ms |
82188 KB |
Output is correct |
85 |
Correct |
1079 ms |
82368 KB |
Output is correct |
86 |
Correct |
1179 ms |
100584 KB |
Output is correct |
87 |
Correct |
1424 ms |
101608 KB |
Output is correct |
88 |
Correct |
1090 ms |
70204 KB |
Output is correct |
89 |
Correct |
1429 ms |
69572 KB |
Output is correct |