Submission #877052

# Submission time Handle Problem Language Result Execution time Memory
877052 2023-11-22T19:46:47 Z parsadox2 Making Friends on Joitter is Fun (JOI20_joitter2) C++14
100 / 100
1185 ms 77908 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10 , M = 3e5 + 10;
int n , m;
long long fin_ans = 0;

struct DSU{
	set <int> st[N] , in[N] , out[N];
	int sz[N] , par[N];
	void Build()
	{
		for(int i = 1 ; i <= n ; i++)
		{
			sz[i] = 1;
			st[i].insert(i);
			in[i].insert(i);
			out[i].insert(i);
		}
	}
	int root(int v)
	{
		return (par[v] == 0 ? v : par[v] = root(par[v]));
	}
	bool Check(int a , int b)
	{
		if(out[a].find(b) != out[a].end() && in[a].find(b) != in[a].end())
			return true;
		return false;
	}
	void Merge(int a , int b)
	{
		fin_ans -= 1LL * sz[a] * ((long long)st[a].size() - 1);
		fin_ans -= 1LL * sz[b] * ((long long)st[b].size() - 1);
		if(sz[a] > sz[b])
			swap(a , b);
		for(auto u : st[a])
			st[b].insert(u);
		vector <int> tmp;
		for(auto u : out[a])   if(u != a && u != b)
		{
			in[u].erase(a);
			out[b].insert(u);
			in[u].insert(b);
			tmp.push_back(u);		
		}
		for(auto u : in[a])  if(u != a && u != b)
		{
			out[u].erase(a);
			in[b].insert(u);
			out[u].insert(b);
			tmp.push_back(u);
		}
		st[a].clear();
		in[a].clear();
		out[a].clear();
		par[a] = b;
		sz[b] += sz[a];
		fin_ans += 1LL * sz[b] * ((long long)st[b].size() - 1);
		for(auto u : tmp)  if(root(b) != root(u) && Check(root(b) , root(u)))
			Merge(root(b) , root(u));
	}
	void Add(int a , int b)
	{
		int ra = root(a) , rb = root(b);
		if(st[ra].find(b) != st[ra].end())
			return;
		st[ra].insert(b);
		out[ra].insert(rb);
		in[rb].insert(ra);
		fin_ans += sz[ra];
		if(Check(ra , rb))
		{
			Merge(ra , rb);
			return;
		}
	}
} dsu;

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);   cout.tie(0);
	cin >> n >> m;
	dsu.Build();
	while(m--)
	{
		int u , v;  cin >> u >> v;
		dsu.Add(v , u);
		cout << fin_ans << '\n';
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14940 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 3 ms 14940 KB Output is correct
4 Correct 3 ms 14936 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 14996 KB Output is correct
7 Correct 3 ms 14936 KB Output is correct
8 Correct 3 ms 14936 KB Output is correct
9 Correct 4 ms 14940 KB Output is correct
10 Correct 3 ms 14940 KB Output is correct
11 Correct 3 ms 14936 KB Output is correct
12 Correct 4 ms 14940 KB Output is correct
13 Correct 3 ms 14940 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 3 ms 14936 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 14936 KB Output is correct
18 Correct 4 ms 14940 KB Output is correct
19 Correct 3 ms 14940 KB Output is correct
20 Correct 3 ms 14940 KB Output is correct
21 Correct 5 ms 14940 KB Output is correct
22 Correct 3 ms 14940 KB Output is correct
23 Correct 3 ms 14940 KB Output is correct
24 Correct 3 ms 14940 KB Output is correct
25 Correct 3 ms 14940 KB Output is correct
26 Correct 4 ms 14940 KB Output is correct
27 Correct 3 ms 14956 KB Output is correct
28 Correct 3 ms 14940 KB Output is correct
29 Correct 3 ms 14936 KB Output is correct
30 Correct 4 ms 14940 KB Output is correct
31 Correct 3 ms 14940 KB Output is correct
32 Correct 3 ms 14940 KB Output is correct
33 Correct 3 ms 14940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14940 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 3 ms 14940 KB Output is correct
4 Correct 3 ms 14936 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 14996 KB Output is correct
7 Correct 3 ms 14936 KB Output is correct
8 Correct 3 ms 14936 KB Output is correct
9 Correct 4 ms 14940 KB Output is correct
10 Correct 3 ms 14940 KB Output is correct
11 Correct 3 ms 14936 KB Output is correct
12 Correct 4 ms 14940 KB Output is correct
13 Correct 3 ms 14940 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 3 ms 14936 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 14936 KB Output is correct
18 Correct 4 ms 14940 KB Output is correct
19 Correct 3 ms 14940 KB Output is correct
20 Correct 3 ms 14940 KB Output is correct
21 Correct 5 ms 14940 KB Output is correct
22 Correct 3 ms 14940 KB Output is correct
23 Correct 3 ms 14940 KB Output is correct
24 Correct 3 ms 14940 KB Output is correct
25 Correct 3 ms 14940 KB Output is correct
26 Correct 4 ms 14940 KB Output is correct
27 Correct 3 ms 14956 KB Output is correct
28 Correct 3 ms 14940 KB Output is correct
29 Correct 3 ms 14936 KB Output is correct
30 Correct 4 ms 14940 KB Output is correct
31 Correct 3 ms 14940 KB Output is correct
32 Correct 3 ms 14940 KB Output is correct
33 Correct 3 ms 14940 KB Output is correct
34 Correct 5 ms 14940 KB Output is correct
35 Correct 77 ms 18676 KB Output is correct
36 Correct 105 ms 21332 KB Output is correct
37 Correct 114 ms 21232 KB Output is correct
38 Correct 95 ms 21072 KB Output is correct
39 Correct 6 ms 15196 KB Output is correct
40 Correct 8 ms 15452 KB Output is correct
41 Correct 8 ms 15452 KB Output is correct
42 Correct 7 ms 15348 KB Output is correct
43 Correct 6 ms 15452 KB Output is correct
44 Correct 6 ms 15436 KB Output is correct
45 Correct 5 ms 15192 KB Output is correct
46 Correct 5 ms 15196 KB Output is correct
47 Correct 8 ms 15452 KB Output is correct
48 Correct 7 ms 15452 KB Output is correct
49 Correct 12 ms 15964 KB Output is correct
50 Correct 97 ms 21368 KB Output is correct
51 Correct 12 ms 15448 KB Output is correct
52 Correct 90 ms 19784 KB Output is correct
53 Correct 11 ms 15960 KB Output is correct
54 Correct 93 ms 20544 KB Output is correct
55 Correct 7 ms 15708 KB Output is correct
56 Correct 7 ms 15708 KB Output is correct
57 Correct 7 ms 16220 KB Output is correct
58 Correct 8 ms 16220 KB Output is correct
59 Correct 8 ms 15452 KB Output is correct
60 Correct 83 ms 18004 KB Output is correct
61 Correct 7 ms 15452 KB Output is correct
62 Correct 96 ms 20720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14940 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 3 ms 14940 KB Output is correct
4 Correct 3 ms 14936 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 14996 KB Output is correct
7 Correct 3 ms 14936 KB Output is correct
8 Correct 3 ms 14936 KB Output is correct
9 Correct 4 ms 14940 KB Output is correct
10 Correct 3 ms 14940 KB Output is correct
11 Correct 3 ms 14936 KB Output is correct
12 Correct 4 ms 14940 KB Output is correct
13 Correct 3 ms 14940 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 3 ms 14936 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 14936 KB Output is correct
18 Correct 4 ms 14940 KB Output is correct
19 Correct 3 ms 14940 KB Output is correct
20 Correct 3 ms 14940 KB Output is correct
21 Correct 5 ms 14940 KB Output is correct
22 Correct 3 ms 14940 KB Output is correct
23 Correct 3 ms 14940 KB Output is correct
24 Correct 3 ms 14940 KB Output is correct
25 Correct 3 ms 14940 KB Output is correct
26 Correct 4 ms 14940 KB Output is correct
27 Correct 3 ms 14956 KB Output is correct
28 Correct 3 ms 14940 KB Output is correct
29 Correct 3 ms 14936 KB Output is correct
30 Correct 4 ms 14940 KB Output is correct
31 Correct 3 ms 14940 KB Output is correct
32 Correct 3 ms 14940 KB Output is correct
33 Correct 3 ms 14940 KB Output is correct
34 Correct 5 ms 14940 KB Output is correct
35 Correct 77 ms 18676 KB Output is correct
36 Correct 105 ms 21332 KB Output is correct
37 Correct 114 ms 21232 KB Output is correct
38 Correct 95 ms 21072 KB Output is correct
39 Correct 6 ms 15196 KB Output is correct
40 Correct 8 ms 15452 KB Output is correct
41 Correct 8 ms 15452 KB Output is correct
42 Correct 7 ms 15348 KB Output is correct
43 Correct 6 ms 15452 KB Output is correct
44 Correct 6 ms 15436 KB Output is correct
45 Correct 5 ms 15192 KB Output is correct
46 Correct 5 ms 15196 KB Output is correct
47 Correct 8 ms 15452 KB Output is correct
48 Correct 7 ms 15452 KB Output is correct
49 Correct 12 ms 15964 KB Output is correct
50 Correct 97 ms 21368 KB Output is correct
51 Correct 12 ms 15448 KB Output is correct
52 Correct 90 ms 19784 KB Output is correct
53 Correct 11 ms 15960 KB Output is correct
54 Correct 93 ms 20544 KB Output is correct
55 Correct 7 ms 15708 KB Output is correct
56 Correct 7 ms 15708 KB Output is correct
57 Correct 7 ms 16220 KB Output is correct
58 Correct 8 ms 16220 KB Output is correct
59 Correct 8 ms 15452 KB Output is correct
60 Correct 83 ms 18004 KB Output is correct
61 Correct 7 ms 15452 KB Output is correct
62 Correct 96 ms 20720 KB Output is correct
63 Correct 487 ms 73344 KB Output is correct
64 Correct 481 ms 73164 KB Output is correct
65 Correct 486 ms 73384 KB Output is correct
66 Correct 233 ms 31316 KB Output is correct
67 Correct 1028 ms 45108 KB Output is correct
68 Correct 235 ms 31316 KB Output is correct
69 Correct 340 ms 38412 KB Output is correct
70 Correct 285 ms 31348 KB Output is correct
71 Correct 268 ms 31316 KB Output is correct
72 Correct 656 ms 40096 KB Output is correct
73 Correct 725 ms 41728 KB Output is correct
74 Correct 1185 ms 52864 KB Output is correct
75 Correct 529 ms 43052 KB Output is correct
76 Correct 936 ms 49248 KB Output is correct
77 Correct 857 ms 49432 KB Output is correct
78 Correct 235 ms 37968 KB Output is correct
79 Correct 406 ms 39300 KB Output is correct
80 Correct 225 ms 38480 KB Output is correct
81 Correct 370 ms 39252 KB Output is correct
82 Correct 672 ms 58892 KB Output is correct
83 Correct 682 ms 58696 KB Output is correct
84 Correct 653 ms 77908 KB Output is correct
85 Correct 651 ms 77640 KB Output is correct
86 Correct 801 ms 45248 KB Output is correct
87 Correct 890 ms 46132 KB Output is correct
88 Correct 671 ms 40560 KB Output is correct
89 Correct 805 ms 47880 KB Output is correct