# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
216728 | 2020-03-27T23:47:11 Z | ainta | Making Friends on Joitter is Fun (JOI20_joitter2) | C++17 | 2257 ms | 121324 KB |
#include<cstdio> #include<algorithm> #include<map> #include<set> #include<vector> #define N_ 101000 using namespace std; int n, m, UF[N_]; long long res, InD[N_]; map<int, int>Out[N_], In[N_]; vector<int>G[N_]; set<int>E[N_], F[N_]; int Find(int a) { if (a == UF[a])return a; return UF[a] = Find(UF[a]); } void Merge(int a, int b) { if (Find(a) == Find(b))return; int pa = Find(a), pb = Find(b); int nd = InD[pa] + InD[pb] - Out[pb][pa] - Out[pa][pb]; res += 1ll * (G[pa].size() + G[pb].size()) * nd + 1ll * G[pa].size()*G[pb].size() * 2; res -= 1ll * InD[pb] * G[pb].size(); res -= 1ll * InD[pa] * G[pa].size(); if (G[pa].size() + In[pa].size() + Out[pa].size() + F[pa].size() < G[pb].size() + In[pb].size() + Out[pb].size() + F[pb].size())swap(pa, pb); InD[pa] = nd; In[pb].erase(pa); Out[pb].erase(pa); set<int>TS; for (auto &t : In[pb]) { if (t.first != pa) { if (Out[pa].count(t.first))TS.insert(t.first); In[pa][t.first] += t.second; Out[t.first][pa] += t.second; } } for (auto &t : Out[pb]) { if (t.first != pa) { if (In[pa].count(t.first))TS.insert(t.first); Out[pa][t.first] += t.second; In[t.first][pa] += t.second; } } for (auto &t : G[pb]) { G[pa].push_back(t); } for (auto &t : F[pb]) { if (Find(t) == pa || Find(t) == pb)continue; if (E[t].find(pa) != E[t].end()) { int zz = Find(t); Out[zz][pa]--; In[pa][zz]--; res -= G[pa].size(); InD[pa]--; } E[t].erase(pb); E[t].insert(pa); F[pa].insert(t); } In[pb].clear(); Out[pb].clear(); G[pb].clear(); F[pb].clear(); UF[pb] = pa; for (auto &t : TS) { Merge(pa, t); } } int main() { int i; scanf("%d%d", &n, &m); for (i = 1; i <= n; i++) { UF[i] = i; G[i].push_back(i); } for (i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); int pa, pb; pa = Find(a), pb = Find(b); if (pa != pb) { if (Out[pb].count(pa)) { Merge(pa, pb); } else { if(E[a].find(pb) == E[a].end()){ res += G[pb].size(); Out[pa][pb]++; In[pb][pa]++; InD[pb]++; E[a].insert(pb); F[pb].insert(a); } } } printf("%lld\n", res); } }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 21632 KB | Output is correct |
2 | Correct | 17 ms | 21504 KB | Output is correct |
3 | Correct | 17 ms | 21632 KB | Output is correct |
4 | Correct | 17 ms | 21504 KB | Output is correct |
5 | Correct | 17 ms | 21632 KB | Output is correct |
6 | Correct | 17 ms | 21632 KB | Output is correct |
7 | Correct | 18 ms | 21760 KB | Output is correct |
8 | Correct | 18 ms | 21760 KB | Output is correct |
9 | Correct | 20 ms | 21760 KB | Output is correct |
10 | Correct | 17 ms | 21760 KB | Output is correct |
11 | Correct | 18 ms | 21632 KB | Output is correct |
12 | Correct | 17 ms | 21632 KB | Output is correct |
13 | Correct | 17 ms | 21632 KB | Output is correct |
14 | Correct | 18 ms | 21632 KB | Output is correct |
15 | Correct | 17 ms | 21632 KB | Output is correct |
16 | Correct | 17 ms | 21632 KB | Output is correct |
17 | Correct | 17 ms | 21632 KB | Output is correct |
18 | Correct | 17 ms | 21760 KB | Output is correct |
19 | Correct | 17 ms | 21632 KB | Output is correct |
20 | Correct | 18 ms | 21760 KB | Output is correct |
21 | Correct | 19 ms | 21760 KB | Output is correct |
22 | Correct | 17 ms | 21632 KB | Output is correct |
23 | Correct | 18 ms | 21760 KB | Output is correct |
24 | Correct | 17 ms | 21760 KB | Output is correct |
25 | Correct | 18 ms | 21760 KB | Output is correct |
26 | Correct | 17 ms | 21632 KB | Output is correct |
27 | Correct | 17 ms | 21632 KB | Output is correct |
28 | Correct | 18 ms | 21632 KB | Output is correct |
29 | Correct | 17 ms | 21760 KB | Output is correct |
30 | Correct | 17 ms | 21632 KB | Output is correct |
31 | Correct | 17 ms | 21760 KB | Output is correct |
32 | Correct | 17 ms | 21632 KB | Output is correct |
33 | Correct | 18 ms | 21752 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 21632 KB | Output is correct |
2 | Correct | 17 ms | 21504 KB | Output is correct |
3 | Correct | 17 ms | 21632 KB | Output is correct |
4 | Correct | 17 ms | 21504 KB | Output is correct |
5 | Correct | 17 ms | 21632 KB | Output is correct |
6 | Correct | 17 ms | 21632 KB | Output is correct |
7 | Correct | 18 ms | 21760 KB | Output is correct |
8 | Correct | 18 ms | 21760 KB | Output is correct |
9 | Correct | 20 ms | 21760 KB | Output is correct |
10 | Correct | 17 ms | 21760 KB | Output is correct |
11 | Correct | 18 ms | 21632 KB | Output is correct |
12 | Correct | 17 ms | 21632 KB | Output is correct |
13 | Correct | 17 ms | 21632 KB | Output is correct |
14 | Correct | 18 ms | 21632 KB | Output is correct |
15 | Correct | 17 ms | 21632 KB | Output is correct |
16 | Correct | 17 ms | 21632 KB | Output is correct |
17 | Correct | 17 ms | 21632 KB | Output is correct |
18 | Correct | 17 ms | 21760 KB | Output is correct |
19 | Correct | 17 ms | 21632 KB | Output is correct |
20 | Correct | 18 ms | 21760 KB | Output is correct |
21 | Correct | 19 ms | 21760 KB | Output is correct |
22 | Correct | 17 ms | 21632 KB | Output is correct |
23 | Correct | 18 ms | 21760 KB | Output is correct |
24 | Correct | 17 ms | 21760 KB | Output is correct |
25 | Correct | 18 ms | 21760 KB | Output is correct |
26 | Correct | 17 ms | 21632 KB | Output is correct |
27 | Correct | 17 ms | 21632 KB | Output is correct |
28 | Correct | 18 ms | 21632 KB | Output is correct |
29 | Correct | 17 ms | 21760 KB | Output is correct |
30 | Correct | 17 ms | 21632 KB | Output is correct |
31 | Correct | 17 ms | 21760 KB | Output is correct |
32 | Correct | 17 ms | 21632 KB | Output is correct |
33 | Correct | 18 ms | 21752 KB | Output is correct |
34 | Correct | 21 ms | 21888 KB | Output is correct |
35 | Correct | 145 ms | 28024 KB | Output is correct |
36 | Correct | 185 ms | 31608 KB | Output is correct |
37 | Correct | 190 ms | 31740 KB | Output is correct |
38 | Correct | 189 ms | 31352 KB | Output is correct |
39 | Correct | 20 ms | 22016 KB | Output is correct |
40 | Correct | 27 ms | 22912 KB | Output is correct |
41 | Correct | 26 ms | 22776 KB | Output is correct |
42 | Correct | 20 ms | 22012 KB | Output is correct |
43 | Correct | 22 ms | 22144 KB | Output is correct |
44 | Correct | 24 ms | 22272 KB | Output is correct |
45 | Correct | 20 ms | 22016 KB | Output is correct |
46 | Correct | 20 ms | 22016 KB | Output is correct |
47 | Correct | 24 ms | 22400 KB | Output is correct |
48 | Correct | 25 ms | 22400 KB | Output is correct |
49 | Correct | 36 ms | 23296 KB | Output is correct |
50 | Correct | 191 ms | 31864 KB | Output is correct |
51 | Correct | 27 ms | 22520 KB | Output is correct |
52 | Correct | 156 ms | 29588 KB | Output is correct |
53 | Correct | 35 ms | 23168 KB | Output is correct |
54 | Correct | 172 ms | 30712 KB | Output is correct |
55 | Correct | 25 ms | 22656 KB | Output is correct |
56 | Correct | 25 ms | 22656 KB | Output is correct |
57 | Correct | 25 ms | 23040 KB | Output is correct |
58 | Correct | 26 ms | 23040 KB | Output is correct |
59 | Correct | 28 ms | 23168 KB | Output is correct |
60 | Correct | 138 ms | 28024 KB | Output is correct |
61 | Correct | 25 ms | 22400 KB | Output is correct |
62 | Correct | 174 ms | 30968 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 21632 KB | Output is correct |
2 | Correct | 17 ms | 21504 KB | Output is correct |
3 | Correct | 17 ms | 21632 KB | Output is correct |
4 | Correct | 17 ms | 21504 KB | Output is correct |
5 | Correct | 17 ms | 21632 KB | Output is correct |
6 | Correct | 17 ms | 21632 KB | Output is correct |
7 | Correct | 18 ms | 21760 KB | Output is correct |
8 | Correct | 18 ms | 21760 KB | Output is correct |
9 | Correct | 20 ms | 21760 KB | Output is correct |
10 | Correct | 17 ms | 21760 KB | Output is correct |
11 | Correct | 18 ms | 21632 KB | Output is correct |
12 | Correct | 17 ms | 21632 KB | Output is correct |
13 | Correct | 17 ms | 21632 KB | Output is correct |
14 | Correct | 18 ms | 21632 KB | Output is correct |
15 | Correct | 17 ms | 21632 KB | Output is correct |
16 | Correct | 17 ms | 21632 KB | Output is correct |
17 | Correct | 17 ms | 21632 KB | Output is correct |
18 | Correct | 17 ms | 21760 KB | Output is correct |
19 | Correct | 17 ms | 21632 KB | Output is correct |
20 | Correct | 18 ms | 21760 KB | Output is correct |
21 | Correct | 19 ms | 21760 KB | Output is correct |
22 | Correct | 17 ms | 21632 KB | Output is correct |
23 | Correct | 18 ms | 21760 KB | Output is correct |
24 | Correct | 17 ms | 21760 KB | Output is correct |
25 | Correct | 18 ms | 21760 KB | Output is correct |
26 | Correct | 17 ms | 21632 KB | Output is correct |
27 | Correct | 17 ms | 21632 KB | Output is correct |
28 | Correct | 18 ms | 21632 KB | Output is correct |
29 | Correct | 17 ms | 21760 KB | Output is correct |
30 | Correct | 17 ms | 21632 KB | Output is correct |
31 | Correct | 17 ms | 21760 KB | Output is correct |
32 | Correct | 17 ms | 21632 KB | Output is correct |
33 | Correct | 18 ms | 21752 KB | Output is correct |
34 | Correct | 21 ms | 21888 KB | Output is correct |
35 | Correct | 145 ms | 28024 KB | Output is correct |
36 | Correct | 185 ms | 31608 KB | Output is correct |
37 | Correct | 190 ms | 31740 KB | Output is correct |
38 | Correct | 189 ms | 31352 KB | Output is correct |
39 | Correct | 20 ms | 22016 KB | Output is correct |
40 | Correct | 27 ms | 22912 KB | Output is correct |
41 | Correct | 26 ms | 22776 KB | Output is correct |
42 | Correct | 20 ms | 22012 KB | Output is correct |
43 | Correct | 22 ms | 22144 KB | Output is correct |
44 | Correct | 24 ms | 22272 KB | Output is correct |
45 | Correct | 20 ms | 22016 KB | Output is correct |
46 | Correct | 20 ms | 22016 KB | Output is correct |
47 | Correct | 24 ms | 22400 KB | Output is correct |
48 | Correct | 25 ms | 22400 KB | Output is correct |
49 | Correct | 36 ms | 23296 KB | Output is correct |
50 | Correct | 191 ms | 31864 KB | Output is correct |
51 | Correct | 27 ms | 22520 KB | Output is correct |
52 | Correct | 156 ms | 29588 KB | Output is correct |
53 | Correct | 35 ms | 23168 KB | Output is correct |
54 | Correct | 172 ms | 30712 KB | Output is correct |
55 | Correct | 25 ms | 22656 KB | Output is correct |
56 | Correct | 25 ms | 22656 KB | Output is correct |
57 | Correct | 25 ms | 23040 KB | Output is correct |
58 | Correct | 26 ms | 23040 KB | Output is correct |
59 | Correct | 28 ms | 23168 KB | Output is correct |
60 | Correct | 138 ms | 28024 KB | Output is correct |
61 | Correct | 25 ms | 22400 KB | Output is correct |
62 | Correct | 174 ms | 30968 KB | Output is correct |
63 | Correct | 670 ms | 87972 KB | Output is correct |
64 | Correct | 664 ms | 87876 KB | Output is correct |
65 | Correct | 669 ms | 87928 KB | Output is correct |
66 | Correct | 282 ms | 40176 KB | Output is correct |
67 | Correct | 1447 ms | 95336 KB | Output is correct |
68 | Correct | 302 ms | 40176 KB | Output is correct |
69 | Correct | 443 ms | 45932 KB | Output is correct |
70 | Correct | 295 ms | 40172 KB | Output is correct |
71 | Correct | 298 ms | 40176 KB | Output is correct |
72 | Correct | 894 ms | 58736 KB | Output is correct |
73 | Correct | 960 ms | 61940 KB | Output is correct |
74 | Correct | 2257 ms | 91656 KB | Output is correct |
75 | Correct | 744 ms | 53616 KB | Output is correct |
76 | Correct | 1367 ms | 68484 KB | Output is correct |
77 | Correct | 1403 ms | 68584 KB | Output is correct |
78 | Correct | 383 ms | 50676 KB | Output is correct |
79 | Correct | 608 ms | 55664 KB | Output is correct |
80 | Correct | 364 ms | 50932 KB | Output is correct |
81 | Correct | 535 ms | 55532 KB | Output is correct |
82 | Correct | 1188 ms | 68408 KB | Output is correct |
83 | Correct | 1174 ms | 68628 KB | Output is correct |
84 | Correct | 1136 ms | 92592 KB | Output is correct |
85 | Correct | 1143 ms | 92624 KB | Output is correct |
86 | Correct | 1484 ms | 119152 KB | Output is correct |
87 | Correct | 1519 ms | 121324 KB | Output is correct |
88 | Correct | 944 ms | 59644 KB | Output is correct |
89 | Correct | 1288 ms | 63600 KB | Output is correct |