# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
212014 | 2020-03-22T03:37:27 Z | Mercenary | Making Friends on Joitter is Fun (JOI20_joitter2) | C++14 | 677 ms | 50928 KB |
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/trie_policy.hpp> #define pb push_back #define mp make_pair #define taskname "A" using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef double ld; typedef pair<int,int> ii; typedef tree <int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; const int maxn = 1e5 + 5; const int mod = 1e9 + 7; int n , m; ll res = 0; int lab[maxn]; set<int> in[maxn],out[maxn]; vector<int> node[maxn]; int FindLab(int u){return lab[u] < 0 ? u : lab[u] = FindLab(lab[u]);} void Merge(int u , int v){ u = FindLab(u);v = FindLab(v); if(u == v)return; res -= (ll)in[u].size() * node[u].size(); res -= (ll)in[v].size() * node[v].size(); res -= (ll)node[u].size() * ((int)node[u].size() - 1); res -= (ll)node[v].size() * ((int)node[v].size() - 1); if(in[u].size() + out[u].size() + node[u].size() < in[v].size() + out[v].size() + node[v].size()) swap(u , v); lab[u] += lab[v]; lab[v] = u; for(auto & c : node[v]){ node[u].pb(c); in[u].erase(c); } vector<int> add; for(auto & c : in[v]){ if(out[u].find(FindLab(c)) != out[u].end()){ add.pb(c); } if(FindLab(c) != u){ out[FindLab(c)].erase(v); out[FindLab(c)].insert(u); in[u].insert(c); } } for(auto & c : out[v]){ if(FindLab(c) != u)out[u].insert(c); if(out[c].find(u) != out[c].end()){ add.pb(c);; } } out[u].erase(v); res += (ll)in[u].size() * node[u].size(); res += (ll)node[u].size() * ((int)node[u].size() - 1); for(auto c : add){ Merge(u,c); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); if(fopen(taskname".INP","r")){ freopen(taskname".INP", "r",stdin); freopen(taskname".OUT", "w",stdout); } cin >> n >> m; for(int i = 1 ; i <= n ; ++i){ lab[i] = -1;node[i].pb(i); } for(int i = 1 ; i <= m ; ++i){ int u , v;cin >> u >> v; if(FindLab(u) != FindLab(v)){ if(out[FindLab(v)].find(FindLab(u)) != out[FindLab(v)].end()){ Merge(u,v); }else if(in[FindLab(v)].find(u) == in[FindLab(v)].end()){ in[FindLab(v)].insert(u); res += node[FindLab(v)].size(); out[FindLab(u)].insert(FindLab(v)); } } cout << res << '\n'; } }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 11 ms | 12160 KB | Output is correct |
2 | Correct | 11 ms | 12032 KB | Output is correct |
3 | Correct | 12 ms | 12032 KB | Output is correct |
4 | Correct | 12 ms | 12160 KB | Output is correct |
5 | Correct | 12 ms | 12032 KB | Output is correct |
6 | Correct | 11 ms | 12160 KB | Output is correct |
7 | Correct | 12 ms | 12160 KB | Output is correct |
8 | Correct | 13 ms | 12160 KB | Output is correct |
9 | Correct | 13 ms | 12160 KB | Output is correct |
10 | Correct | 12 ms | 12032 KB | Output is correct |
11 | Correct | 13 ms | 12416 KB | Output is correct |
12 | Correct | 12 ms | 12160 KB | Output is correct |
13 | Correct | 12 ms | 12160 KB | Output is correct |
14 | Correct | 11 ms | 12032 KB | Output is correct |
15 | Correct | 12 ms | 12032 KB | Output is correct |
16 | Correct | 12 ms | 12032 KB | Output is correct |
17 | Correct | 11 ms | 12160 KB | Output is correct |
18 | Correct | 13 ms | 12048 KB | Output is correct |
19 | Correct | 12 ms | 12032 KB | Output is correct |
20 | Correct | 11 ms | 12160 KB | Output is correct |
21 | Correct | 12 ms | 12160 KB | Output is correct |
22 | Correct | 12 ms | 12160 KB | Output is correct |
23 | Correct | 12 ms | 12160 KB | Output is correct |
24 | Correct | 11 ms | 12032 KB | Output is correct |
25 | Correct | 12 ms | 12160 KB | Output is correct |
26 | Correct | 11 ms | 12032 KB | Output is correct |
27 | Correct | 11 ms | 12032 KB | Output is correct |
28 | Correct | 12 ms | 12032 KB | Output is correct |
29 | Correct | 12 ms | 12160 KB | Output is correct |
30 | Correct | 12 ms | 12160 KB | Output is correct |
31 | Correct | 12 ms | 12064 KB | Output is correct |
32 | Correct | 11 ms | 12032 KB | Output is correct |
33 | Correct | 12 ms | 12160 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 11 ms | 12160 KB | Output is correct |
2 | Correct | 11 ms | 12032 KB | Output is correct |
3 | Correct | 12 ms | 12032 KB | Output is correct |
4 | Correct | 12 ms | 12160 KB | Output is correct |
5 | Correct | 12 ms | 12032 KB | Output is correct |
6 | Correct | 11 ms | 12160 KB | Output is correct |
7 | Correct | 12 ms | 12160 KB | Output is correct |
8 | Correct | 13 ms | 12160 KB | Output is correct |
9 | Correct | 13 ms | 12160 KB | Output is correct |
10 | Correct | 12 ms | 12032 KB | Output is correct |
11 | Correct | 13 ms | 12416 KB | Output is correct |
12 | Correct | 12 ms | 12160 KB | Output is correct |
13 | Correct | 12 ms | 12160 KB | Output is correct |
14 | Correct | 11 ms | 12032 KB | Output is correct |
15 | Correct | 12 ms | 12032 KB | Output is correct |
16 | Correct | 12 ms | 12032 KB | Output is correct |
17 | Correct | 11 ms | 12160 KB | Output is correct |
18 | Correct | 13 ms | 12048 KB | Output is correct |
19 | Correct | 12 ms | 12032 KB | Output is correct |
20 | Correct | 11 ms | 12160 KB | Output is correct |
21 | Correct | 12 ms | 12160 KB | Output is correct |
22 | Correct | 12 ms | 12160 KB | Output is correct |
23 | Correct | 12 ms | 12160 KB | Output is correct |
24 | Correct | 11 ms | 12032 KB | Output is correct |
25 | Correct | 12 ms | 12160 KB | Output is correct |
26 | Correct | 11 ms | 12032 KB | Output is correct |
27 | Correct | 11 ms | 12032 KB | Output is correct |
28 | Correct | 12 ms | 12032 KB | Output is correct |
29 | Correct | 12 ms | 12160 KB | Output is correct |
30 | Correct | 12 ms | 12160 KB | Output is correct |
31 | Correct | 12 ms | 12064 KB | Output is correct |
32 | Correct | 11 ms | 12032 KB | Output is correct |
33 | Correct | 12 ms | 12160 KB | Output is correct |
34 | Correct | 14 ms | 12160 KB | Output is correct |
35 | Correct | 94 ms | 15256 KB | Output is correct |
36 | Correct | 114 ms | 17144 KB | Output is correct |
37 | Correct | 119 ms | 17196 KB | Output is correct |
38 | Correct | 111 ms | 16888 KB | Output is correct |
39 | Correct | 13 ms | 12288 KB | Output is correct |
40 | Correct | 14 ms | 12416 KB | Output is correct |
41 | Correct | 15 ms | 12416 KB | Output is correct |
42 | Correct | 13 ms | 12288 KB | Output is correct |
43 | Correct | 14 ms | 12288 KB | Output is correct |
44 | Correct | 14 ms | 12288 KB | Output is correct |
45 | Correct | 14 ms | 12416 KB | Output is correct |
46 | Correct | 13 ms | 12288 KB | Output is correct |
47 | Correct | 14 ms | 12288 KB | Output is correct |
48 | Correct | 14 ms | 12416 KB | Output is correct |
49 | Correct | 20 ms | 12928 KB | Output is correct |
50 | Correct | 117 ms | 17272 KB | Output is correct |
51 | Correct | 18 ms | 12544 KB | Output is correct |
52 | Correct | 109 ms | 16120 KB | Output is correct |
53 | Correct | 20 ms | 12928 KB | Output is correct |
54 | Correct | 106 ms | 16632 KB | Output is correct |
55 | Correct | 15 ms | 12544 KB | Output is correct |
56 | Correct | 16 ms | 12544 KB | Output is correct |
57 | Correct | 16 ms | 12928 KB | Output is correct |
58 | Correct | 15 ms | 12800 KB | Output is correct |
59 | Correct | 17 ms | 12416 KB | Output is correct |
60 | Correct | 93 ms | 14712 KB | Output is correct |
61 | Correct | 15 ms | 12416 KB | Output is correct |
62 | Correct | 111 ms | 16888 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 11 ms | 12160 KB | Output is correct |
2 | Correct | 11 ms | 12032 KB | Output is correct |
3 | Correct | 12 ms | 12032 KB | Output is correct |
4 | Correct | 12 ms | 12160 KB | Output is correct |
5 | Correct | 12 ms | 12032 KB | Output is correct |
6 | Correct | 11 ms | 12160 KB | Output is correct |
7 | Correct | 12 ms | 12160 KB | Output is correct |
8 | Correct | 13 ms | 12160 KB | Output is correct |
9 | Correct | 13 ms | 12160 KB | Output is correct |
10 | Correct | 12 ms | 12032 KB | Output is correct |
11 | Correct | 13 ms | 12416 KB | Output is correct |
12 | Correct | 12 ms | 12160 KB | Output is correct |
13 | Correct | 12 ms | 12160 KB | Output is correct |
14 | Correct | 11 ms | 12032 KB | Output is correct |
15 | Correct | 12 ms | 12032 KB | Output is correct |
16 | Correct | 12 ms | 12032 KB | Output is correct |
17 | Correct | 11 ms | 12160 KB | Output is correct |
18 | Correct | 13 ms | 12048 KB | Output is correct |
19 | Correct | 12 ms | 12032 KB | Output is correct |
20 | Correct | 11 ms | 12160 KB | Output is correct |
21 | Correct | 12 ms | 12160 KB | Output is correct |
22 | Correct | 12 ms | 12160 KB | Output is correct |
23 | Correct | 12 ms | 12160 KB | Output is correct |
24 | Correct | 11 ms | 12032 KB | Output is correct |
25 | Correct | 12 ms | 12160 KB | Output is correct |
26 | Correct | 11 ms | 12032 KB | Output is correct |
27 | Correct | 11 ms | 12032 KB | Output is correct |
28 | Correct | 12 ms | 12032 KB | Output is correct |
29 | Correct | 12 ms | 12160 KB | Output is correct |
30 | Correct | 12 ms | 12160 KB | Output is correct |
31 | Correct | 12 ms | 12064 KB | Output is correct |
32 | Correct | 11 ms | 12032 KB | Output is correct |
33 | Correct | 12 ms | 12160 KB | Output is correct |
34 | Correct | 14 ms | 12160 KB | Output is correct |
35 | Correct | 94 ms | 15256 KB | Output is correct |
36 | Correct | 114 ms | 17144 KB | Output is correct |
37 | Correct | 119 ms | 17196 KB | Output is correct |
38 | Correct | 111 ms | 16888 KB | Output is correct |
39 | Correct | 13 ms | 12288 KB | Output is correct |
40 | Correct | 14 ms | 12416 KB | Output is correct |
41 | Correct | 15 ms | 12416 KB | Output is correct |
42 | Correct | 13 ms | 12288 KB | Output is correct |
43 | Correct | 14 ms | 12288 KB | Output is correct |
44 | Correct | 14 ms | 12288 KB | Output is correct |
45 | Correct | 14 ms | 12416 KB | Output is correct |
46 | Correct | 13 ms | 12288 KB | Output is correct |
47 | Correct | 14 ms | 12288 KB | Output is correct |
48 | Correct | 14 ms | 12416 KB | Output is correct |
49 | Correct | 20 ms | 12928 KB | Output is correct |
50 | Correct | 117 ms | 17272 KB | Output is correct |
51 | Correct | 18 ms | 12544 KB | Output is correct |
52 | Correct | 109 ms | 16120 KB | Output is correct |
53 | Correct | 20 ms | 12928 KB | Output is correct |
54 | Correct | 106 ms | 16632 KB | Output is correct |
55 | Correct | 15 ms | 12544 KB | Output is correct |
56 | Correct | 16 ms | 12544 KB | Output is correct |
57 | Correct | 16 ms | 12928 KB | Output is correct |
58 | Correct | 15 ms | 12800 KB | Output is correct |
59 | Correct | 17 ms | 12416 KB | Output is correct |
60 | Correct | 93 ms | 14712 KB | Output is correct |
61 | Correct | 15 ms | 12416 KB | Output is correct |
62 | Correct | 111 ms | 16888 KB | Output is correct |
63 | Correct | 413 ms | 45748 KB | Output is correct |
64 | Correct | 408 ms | 45688 KB | Output is correct |
65 | Correct | 401 ms | 45816 KB | Output is correct |
66 | Correct | 131 ms | 22768 KB | Output is correct |
67 | Correct | 236 ms | 28400 KB | Output is correct |
68 | Correct | 141 ms | 22768 KB | Output is correct |
69 | Correct | 302 ms | 24304 KB | Output is correct |
70 | Correct | 133 ms | 22768 KB | Output is correct |
71 | Correct | 141 ms | 22764 KB | Output is correct |
72 | Correct | 281 ms | 26356 KB | Output is correct |
73 | Correct | 269 ms | 26612 KB | Output is correct |
74 | Correct | 677 ms | 45172 KB | Output is correct |
75 | Correct | 460 ms | 29168 KB | Output is correct |
76 | Correct | 497 ms | 36208 KB | Output is correct |
77 | Correct | 498 ms | 36336 KB | Output is correct |
78 | Correct | 187 ms | 26612 KB | Output is correct |
79 | Correct | 298 ms | 29424 KB | Output is correct |
80 | Correct | 190 ms | 26644 KB | Output is correct |
81 | Correct | 286 ms | 28652 KB | Output is correct |
82 | Correct | 479 ms | 38260 KB | Output is correct |
83 | Correct | 455 ms | 38260 KB | Output is correct |
84 | Correct | 442 ms | 50928 KB | Output is correct |
85 | Correct | 435 ms | 50804 KB | Output is correct |
86 | Correct | 170 ms | 26868 KB | Output is correct |
87 | Correct | 201 ms | 27884 KB | Output is correct |
88 | Correct | 277 ms | 26612 KB | Output is correct |
89 | Correct | 479 ms | 34804 KB | Output is correct |