Submission #568240

# Submission time Handle Problem Language Result Execution time Memory
568240 2022-05-25T03:03:52 Z LittleCube Making Friends on Joitter is Fun (JOI20_joitter2) C++14
100 / 100
1515 ms 159568 KB
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;

int N, M, dsu[100005], rk[100005];
ll ans;
set<int> fd[100005], f[100005], fdr[100005], fr[100005];

vector<pii> chain;

int find(int k)
{
	return k == dsu[k] ? k : dsu[k] = find(dsu[k]);
}

void Merge(set<int> &small, set<int> &big)
{
	for (int i : small)
		big.insert(i);
}

void query(set<int> &small, set<int> &big, int another)
{
	for (int i : small)
		if (big.find(i) != big.end())
			chain.emplace_back(pii(i, another));
}

void follow(int a, int b)
{
	// cout << "merge " << a << " " << b << '\n';
	// out A <-> in B
	if (fr[a].size() <= fdr[b].size())
		query(fr[a], fdr[b], a);
	else
		query(fdr[b], fr[a], a);

	if (fr[b].size() <= fdr[a].size())
		query(fr[b], fdr[a], a);
	else
		query(fdr[a], fr[b], a);

	if (rk[a] <= rk[b])
		for (int i : fdr[a])
		{
			fr[i].erase(a);
			fr[i].insert(b);
		}
	else
		for (int i : fdr[b])
		{
			fr[i].erase(b);
			fr[i].insert(a);
		}

	if (rk[a] <= rk[b])
		for (int i : fr[a])
		{
			fdr[i].erase(a);
			fdr[i].insert(b);
		}
	else
		for (int i : fr[b])
		{
			fdr[i].erase(b);
			fdr[i].insert(a);
		}
	// cout << fd[a].size() << " " << fd[b].size() << " ";
	ans -= fd[a].size() * rk[a] + fd[b].size() * rk[b];

	if (fd[a].size() <= fd[b].size())
		Merge(fd[a], fd[b]);
	else
		Merge(fd[b], fd[a]);

	if (fdr[a].size() <= fdr[b].size())
		Merge(fdr[a], fdr[b]);
	else
		Merge(fdr[b], fdr[a]);

	if (f[a].size() <= f[b].size())
		Merge(f[a], f[b]);
	else
		Merge(f[b], f[a]);

	if (fr[a].size() <= fr[b].size())
		Merge(fr[a], fr[b]);
	else
		Merge(fr[b], fr[a]);
	// cout << max(fd[a].size(), fd[b].size()) << '\n';
	ans += max(fd[a].size(), fd[b].size()) * (rk[a] + rk[b]);
	// cout << "update ans " << ans << '\n';
	if (rk[a] <= rk[b])
	{
		dsu[a] = b;
		rk[b] += rk[a];

		if (f[a].size() >= f[b].size())
			f[a].swap(f[b]);
		if (fd[a].size() >= fd[b].size())
			fd[b].swap(fd[a]);
		if (fr[a].size() >= fr[b].size())
			fr[a].swap(fr[b]);
		if (fdr[a].size() >= fdr[b].size())
			fdr[b].swap(fdr[a]);
	}
	else
	{
		dsu[b] = a;
		rk[a] += rk[b];
		fd[a].swap(fd[b]);
		if (f[a].size() <= f[b].size())
			f[a].swap(f[b]);
		if (fd[a].size() <= fd[b].size())
			fd[b].swap(fd[a]);
		if (fr[a].size() <= fr[b].size())
			fr[a].swap(fr[b]);
		if (fdr[a].size() <= fdr[b].size())
			fdr[b].swap(fdr[a]);
	}
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), // cout.tie(0);
	cin >> N >> M;
	for (int i = 1; i <= N; i++)
	{
		dsu[i] = i, rk[i] = 1;
		fd[i].insert(i);
		f[i].insert(i);
		fdr[i].insert(i);
		fr[i].insert(i);
	}
	for (int i = 1; i <= M; i++)
	{
		int A, B;
		cin >> A >> B;
		if (find(A) == find(B))
			;
		else if (fd[find(B)].find(A) != fd[find(B)].end())
			;
		else if (fd[find(A)].find(B) != fd[find(A)].end())
		{
			// // cout << "mergeeee\n";
			chain.emplace_back(pii(A, B));
		}
		else
		{
			// // cout << "simple follow\n";
			if (fr[find(B)].find(find(A)) != fr[find(B)].end())
				chain.emplace_back(pii(A, B));

			fd[find(B)].insert(A);
			f[find(A)].insert(B);
			fdr[find(B)].insert(find(A));
			fr[find(A)].insert(find(B));
			ans += rk[find(B)];
		}
		while (!chain.empty())
		{
			auto [a, b] = chain.back();
			chain.pop_back();
			if (find(a) != find(b))
				follow(find(a), find(b));
		}

		cout << ans << '\n';
	}
}

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:167:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  167 |    auto [a, b] = chain.back();
      |         ^
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19028 KB Output is correct
2 Correct 12 ms 19048 KB Output is correct
3 Correct 10 ms 19092 KB Output is correct
4 Correct 13 ms 19028 KB Output is correct
5 Correct 12 ms 19116 KB Output is correct
6 Correct 10 ms 19112 KB Output is correct
7 Correct 11 ms 19156 KB Output is correct
8 Correct 12 ms 19156 KB Output is correct
9 Correct 11 ms 19156 KB Output is correct
10 Correct 10 ms 19016 KB Output is correct
11 Correct 10 ms 19028 KB Output is correct
12 Correct 10 ms 19068 KB Output is correct
13 Correct 10 ms 19028 KB Output is correct
14 Correct 11 ms 19048 KB Output is correct
15 Correct 11 ms 19116 KB Output is correct
16 Correct 11 ms 19112 KB Output is correct
17 Correct 10 ms 19028 KB Output is correct
18 Correct 11 ms 19028 KB Output is correct
19 Correct 9 ms 19116 KB Output is correct
20 Correct 9 ms 19156 KB Output is correct
21 Correct 10 ms 19156 KB Output is correct
22 Correct 10 ms 19028 KB Output is correct
23 Correct 11 ms 19176 KB Output is correct
24 Correct 12 ms 19156 KB Output is correct
25 Correct 11 ms 19124 KB Output is correct
26 Correct 10 ms 19028 KB Output is correct
27 Correct 11 ms 19076 KB Output is correct
28 Correct 13 ms 19120 KB Output is correct
29 Correct 11 ms 19120 KB Output is correct
30 Correct 11 ms 19132 KB Output is correct
31 Correct 11 ms 19124 KB Output is correct
32 Correct 11 ms 19116 KB Output is correct
33 Correct 11 ms 19156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19028 KB Output is correct
2 Correct 12 ms 19048 KB Output is correct
3 Correct 10 ms 19092 KB Output is correct
4 Correct 13 ms 19028 KB Output is correct
5 Correct 12 ms 19116 KB Output is correct
6 Correct 10 ms 19112 KB Output is correct
7 Correct 11 ms 19156 KB Output is correct
8 Correct 12 ms 19156 KB Output is correct
9 Correct 11 ms 19156 KB Output is correct
10 Correct 10 ms 19016 KB Output is correct
11 Correct 10 ms 19028 KB Output is correct
12 Correct 10 ms 19068 KB Output is correct
13 Correct 10 ms 19028 KB Output is correct
14 Correct 11 ms 19048 KB Output is correct
15 Correct 11 ms 19116 KB Output is correct
16 Correct 11 ms 19112 KB Output is correct
17 Correct 10 ms 19028 KB Output is correct
18 Correct 11 ms 19028 KB Output is correct
19 Correct 9 ms 19116 KB Output is correct
20 Correct 9 ms 19156 KB Output is correct
21 Correct 10 ms 19156 KB Output is correct
22 Correct 10 ms 19028 KB Output is correct
23 Correct 11 ms 19176 KB Output is correct
24 Correct 12 ms 19156 KB Output is correct
25 Correct 11 ms 19124 KB Output is correct
26 Correct 10 ms 19028 KB Output is correct
27 Correct 11 ms 19076 KB Output is correct
28 Correct 13 ms 19120 KB Output is correct
29 Correct 11 ms 19120 KB Output is correct
30 Correct 11 ms 19132 KB Output is correct
31 Correct 11 ms 19124 KB Output is correct
32 Correct 11 ms 19116 KB Output is correct
33 Correct 11 ms 19156 KB Output is correct
34 Correct 12 ms 19284 KB Output is correct
35 Correct 86 ms 25680 KB Output is correct
36 Correct 134 ms 29468 KB Output is correct
37 Correct 127 ms 29504 KB Output is correct
38 Correct 116 ms 29040 KB Output is correct
39 Correct 16 ms 20052 KB Output is correct
40 Correct 23 ms 20788 KB Output is correct
41 Correct 18 ms 20780 KB Output is correct
42 Correct 15 ms 20028 KB Output is correct
43 Correct 15 ms 19956 KB Output is correct
44 Correct 15 ms 19900 KB Output is correct
45 Correct 15 ms 19924 KB Output is correct
46 Correct 17 ms 20152 KB Output is correct
47 Correct 21 ms 20388 KB Output is correct
48 Correct 19 ms 20396 KB Output is correct
49 Correct 25 ms 21104 KB Output is correct
50 Correct 117 ms 29652 KB Output is correct
51 Correct 22 ms 20384 KB Output is correct
52 Correct 109 ms 27356 KB Output is correct
53 Correct 28 ms 21004 KB Output is correct
54 Correct 107 ms 28428 KB Output is correct
55 Correct 18 ms 20436 KB Output is correct
56 Correct 21 ms 20436 KB Output is correct
57 Correct 18 ms 20532 KB Output is correct
58 Correct 19 ms 20556 KB Output is correct
59 Correct 18 ms 21044 KB Output is correct
60 Correct 85 ms 25940 KB Output is correct
61 Correct 20 ms 20396 KB Output is correct
62 Correct 125 ms 28872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19028 KB Output is correct
2 Correct 12 ms 19048 KB Output is correct
3 Correct 10 ms 19092 KB Output is correct
4 Correct 13 ms 19028 KB Output is correct
5 Correct 12 ms 19116 KB Output is correct
6 Correct 10 ms 19112 KB Output is correct
7 Correct 11 ms 19156 KB Output is correct
8 Correct 12 ms 19156 KB Output is correct
9 Correct 11 ms 19156 KB Output is correct
10 Correct 10 ms 19016 KB Output is correct
11 Correct 10 ms 19028 KB Output is correct
12 Correct 10 ms 19068 KB Output is correct
13 Correct 10 ms 19028 KB Output is correct
14 Correct 11 ms 19048 KB Output is correct
15 Correct 11 ms 19116 KB Output is correct
16 Correct 11 ms 19112 KB Output is correct
17 Correct 10 ms 19028 KB Output is correct
18 Correct 11 ms 19028 KB Output is correct
19 Correct 9 ms 19116 KB Output is correct
20 Correct 9 ms 19156 KB Output is correct
21 Correct 10 ms 19156 KB Output is correct
22 Correct 10 ms 19028 KB Output is correct
23 Correct 11 ms 19176 KB Output is correct
24 Correct 12 ms 19156 KB Output is correct
25 Correct 11 ms 19124 KB Output is correct
26 Correct 10 ms 19028 KB Output is correct
27 Correct 11 ms 19076 KB Output is correct
28 Correct 13 ms 19120 KB Output is correct
29 Correct 11 ms 19120 KB Output is correct
30 Correct 11 ms 19132 KB Output is correct
31 Correct 11 ms 19124 KB Output is correct
32 Correct 11 ms 19116 KB Output is correct
33 Correct 11 ms 19156 KB Output is correct
34 Correct 12 ms 19284 KB Output is correct
35 Correct 86 ms 25680 KB Output is correct
36 Correct 134 ms 29468 KB Output is correct
37 Correct 127 ms 29504 KB Output is correct
38 Correct 116 ms 29040 KB Output is correct
39 Correct 16 ms 20052 KB Output is correct
40 Correct 23 ms 20788 KB Output is correct
41 Correct 18 ms 20780 KB Output is correct
42 Correct 15 ms 20028 KB Output is correct
43 Correct 15 ms 19956 KB Output is correct
44 Correct 15 ms 19900 KB Output is correct
45 Correct 15 ms 19924 KB Output is correct
46 Correct 17 ms 20152 KB Output is correct
47 Correct 21 ms 20388 KB Output is correct
48 Correct 19 ms 20396 KB Output is correct
49 Correct 25 ms 21104 KB Output is correct
50 Correct 117 ms 29652 KB Output is correct
51 Correct 22 ms 20384 KB Output is correct
52 Correct 109 ms 27356 KB Output is correct
53 Correct 28 ms 21004 KB Output is correct
54 Correct 107 ms 28428 KB Output is correct
55 Correct 18 ms 20436 KB Output is correct
56 Correct 21 ms 20436 KB Output is correct
57 Correct 18 ms 20532 KB Output is correct
58 Correct 19 ms 20556 KB Output is correct
59 Correct 18 ms 21044 KB Output is correct
60 Correct 85 ms 25940 KB Output is correct
61 Correct 20 ms 20396 KB Output is correct
62 Correct 125 ms 28872 KB Output is correct
63 Correct 699 ms 100576 KB Output is correct
64 Correct 730 ms 100500 KB Output is correct
65 Correct 672 ms 100588 KB Output is correct
66 Correct 463 ms 66548 KB Output is correct
67 Correct 925 ms 135560 KB Output is correct
68 Correct 457 ms 66568 KB Output is correct
69 Correct 778 ms 64128 KB Output is correct
70 Correct 512 ms 66480 KB Output is correct
71 Correct 458 ms 66548 KB Output is correct
72 Correct 977 ms 90140 KB Output is correct
73 Correct 973 ms 94868 KB Output is correct
74 Correct 1515 ms 118196 KB Output is correct
75 Correct 1110 ms 73696 KB Output is correct
76 Correct 1263 ms 93804 KB Output is correct
77 Correct 1266 ms 94196 KB Output is correct
78 Correct 518 ms 65712 KB Output is correct
79 Correct 905 ms 75444 KB Output is correct
80 Correct 382 ms 64316 KB Output is correct
81 Correct 621 ms 69672 KB Output is correct
82 Correct 1106 ms 89176 KB Output is correct
83 Correct 1131 ms 89288 KB Output is correct
84 Correct 1058 ms 90976 KB Output is correct
85 Correct 1074 ms 91000 KB Output is correct
86 Correct 814 ms 157060 KB Output is correct
87 Correct 826 ms 159568 KB Output is correct
88 Correct 898 ms 92224 KB Output is correct
89 Correct 1202 ms 91576 KB Output is correct