답안 #877051

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
877051 2023-11-22T19:43:57 Z parsadox2 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
100 / 100
1408 ms 79980 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);
			if(Check(b , u))
				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);
			if(Check(b , u))
				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))
			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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14940 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 4 ms 15004 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 4 ms 14940 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 3 ms 14940 KB Output is correct
11 Correct 3 ms 15000 KB Output is correct
12 Correct 3 ms 14936 KB Output is correct
13 Correct 4 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 14952 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 3 ms 14940 KB Output is correct
19 Correct 3 ms 14940 KB Output is correct
20 Correct 3 ms 14968 KB Output is correct
21 Correct 3 ms 14940 KB Output is correct
22 Correct 4 ms 15196 KB Output is correct
23 Correct 4 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 14940 KB Output is correct
28 Correct 4 ms 14940 KB Output is correct
29 Correct 4 ms 14960 KB Output is correct
30 Correct 3 ms 14940 KB Output is correct
31 Correct 4 ms 14940 KB Output is correct
32 Correct 3 ms 14940 KB Output is correct
33 Correct 3 ms 14940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14940 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 4 ms 15004 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 4 ms 14940 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 3 ms 14940 KB Output is correct
11 Correct 3 ms 15000 KB Output is correct
12 Correct 3 ms 14936 KB Output is correct
13 Correct 4 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 14952 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 3 ms 14940 KB Output is correct
19 Correct 3 ms 14940 KB Output is correct
20 Correct 3 ms 14968 KB Output is correct
21 Correct 3 ms 14940 KB Output is correct
22 Correct 4 ms 15196 KB Output is correct
23 Correct 4 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 14940 KB Output is correct
28 Correct 4 ms 14940 KB Output is correct
29 Correct 4 ms 14960 KB Output is correct
30 Correct 3 ms 14940 KB Output is correct
31 Correct 4 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 6 ms 14936 KB Output is correct
35 Correct 78 ms 20988 KB Output is correct
36 Correct 108 ms 23928 KB Output is correct
37 Correct 111 ms 23896 KB Output is correct
38 Correct 99 ms 23584 KB Output is correct
39 Correct 7 ms 15332 KB Output is correct
40 Correct 9 ms 15452 KB Output is correct
41 Correct 9 ms 15452 KB Output is correct
42 Correct 5 ms 15196 KB Output is correct
43 Correct 5 ms 15448 KB Output is correct
44 Correct 5 ms 15452 KB Output is correct
45 Correct 5 ms 15196 KB Output is correct
46 Correct 5 ms 15192 KB Output is correct
47 Correct 7 ms 15548 KB Output is correct
48 Correct 10 ms 15508 KB Output is correct
49 Correct 14 ms 16116 KB Output is correct
50 Correct 101 ms 23892 KB Output is correct
51 Correct 8 ms 15708 KB Output is correct
52 Correct 91 ms 22468 KB Output is correct
53 Correct 11 ms 16216 KB Output is correct
54 Correct 95 ms 23064 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 16216 KB Output is correct
59 Correct 9 ms 15452 KB Output is correct
60 Correct 84 ms 20384 KB Output is correct
61 Correct 7 ms 15452 KB Output is correct
62 Correct 104 ms 23564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14940 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 4 ms 15004 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 4 ms 14940 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 3 ms 14940 KB Output is correct
11 Correct 3 ms 15000 KB Output is correct
12 Correct 3 ms 14936 KB Output is correct
13 Correct 4 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 14952 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 3 ms 14940 KB Output is correct
19 Correct 3 ms 14940 KB Output is correct
20 Correct 3 ms 14968 KB Output is correct
21 Correct 3 ms 14940 KB Output is correct
22 Correct 4 ms 15196 KB Output is correct
23 Correct 4 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 14940 KB Output is correct
28 Correct 4 ms 14940 KB Output is correct
29 Correct 4 ms 14960 KB Output is correct
30 Correct 3 ms 14940 KB Output is correct
31 Correct 4 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 6 ms 14936 KB Output is correct
35 Correct 78 ms 20988 KB Output is correct
36 Correct 108 ms 23928 KB Output is correct
37 Correct 111 ms 23896 KB Output is correct
38 Correct 99 ms 23584 KB Output is correct
39 Correct 7 ms 15332 KB Output is correct
40 Correct 9 ms 15452 KB Output is correct
41 Correct 9 ms 15452 KB Output is correct
42 Correct 5 ms 15196 KB Output is correct
43 Correct 5 ms 15448 KB Output is correct
44 Correct 5 ms 15452 KB Output is correct
45 Correct 5 ms 15196 KB Output is correct
46 Correct 5 ms 15192 KB Output is correct
47 Correct 7 ms 15548 KB Output is correct
48 Correct 10 ms 15508 KB Output is correct
49 Correct 14 ms 16116 KB Output is correct
50 Correct 101 ms 23892 KB Output is correct
51 Correct 8 ms 15708 KB Output is correct
52 Correct 91 ms 22468 KB Output is correct
53 Correct 11 ms 16216 KB Output is correct
54 Correct 95 ms 23064 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 16216 KB Output is correct
59 Correct 9 ms 15452 KB Output is correct
60 Correct 84 ms 20384 KB Output is correct
61 Correct 7 ms 15452 KB Output is correct
62 Correct 104 ms 23564 KB Output is correct
63 Correct 484 ms 76692 KB Output is correct
64 Correct 466 ms 76624 KB Output is correct
65 Correct 501 ms 76812 KB Output is correct
66 Correct 264 ms 33796 KB Output is correct
67 Correct 1183 ms 47304 KB Output is correct
68 Correct 259 ms 33728 KB Output is correct
69 Correct 353 ms 40724 KB Output is correct
70 Correct 260 ms 33616 KB Output is correct
71 Correct 277 ms 33640 KB Output is correct
72 Correct 743 ms 42320 KB Output is correct
73 Correct 799 ms 43864 KB Output is correct
74 Correct 1408 ms 55688 KB Output is correct
75 Correct 538 ms 46612 KB Output is correct
76 Correct 903 ms 52624 KB Output is correct
77 Correct 887 ms 52716 KB Output is correct
78 Correct 249 ms 39468 KB Output is correct
79 Correct 438 ms 42928 KB Output is correct
80 Correct 229 ms 39916 KB Output is correct
81 Correct 382 ms 42732 KB Output is correct
82 Correct 666 ms 61008 KB Output is correct
83 Correct 705 ms 61196 KB Output is correct
84 Correct 682 ms 79980 KB Output is correct
85 Correct 646 ms 79784 KB Output is correct
86 Correct 984 ms 47360 KB Output is correct
87 Correct 956 ms 49556 KB Output is correct
88 Correct 840 ms 42664 KB Output is correct
89 Correct 875 ms 51244 KB Output is correct