답안 #219550

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
219550 2020-04-05T14:21:40 Z atoiz 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
17 / 100
5000 ms 81400 KB
/*input
5 5
1 2
3 4
4 1
5 3
1 4
*/

#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <stack>
#include <unordered_set>
#include <unordered_map>
#include <ctime>

using namespace std;

const int MAXN = 100007;
int N, M;
int root[MAXN];
int64_t E = 0, cnt[MAXN], deg_in[MAXN], deg_out[MAXN];
unordered_map<int, unordered_set<int>> edges[MAXN];
unordered_set<int> back_edges[MAXN];

stack<pair<int, int>> join_stack;

void init()
{
	for (int u = 1; u <= N; ++u) cnt[u] = 1, root[u] = u;
}

int get_root(int u) { return (root[u] == u ? u : root[u] = get_root(root[u])); }

void delete_edge(int u, int v)
{
	auto it = edges[u].find(v);
	assert(it != edges[u].end());

	int64_t cur = it->second.size() * cnt[v];
	deg_out[u] -= it->second.size(), deg_in[v] -= it->second.size(), E -= cur;
	edges[u].erase(it);
	back_edges[v].erase(u);
}

void add_edge(int u, int v, unordered_set<int> sources)
{
	back_edges[v].insert(u);
	for (int x : sources) {
		if (edges[u][v].find(x) == edges[u][v].end()) {
			++deg_out[u], ++deg_in[v], E += cnt[v];
			edges[u][v].insert(x);
		}
	}	
}

void join(int u, int v)
{
	u = get_root(u), v = get_root(v);
	if (u == v) return;

	// between u and v
	if (edges[u].find(v) != edges[u].end()) delete_edge(u, v);
	if (edges[v].find(u) != edges[v].end()) delete_edge(v, u);
	E += cnt[u] * cnt[v] * 2;
	// cout << "X " << E << endl;

	auto edges_v = edges[v];
	for (auto batch : edges_v) {
		int w = batch.first;
		delete_edge(v, w);
		if (back_edges[u].find(w) != back_edges[u].end()) join_stack.emplace(u, w);
		else {
			add_edge(u, w, batch.second);
		}
	}

	auto back_edges_v = back_edges[v];
	for (int w : back_edges_v) {
		auto sources = edges[w][v];
		delete_edge(w, v);
		if (back_edges[w].find(u) != back_edges[w].end()) join_stack.emplace(u, w);
		else {
			add_edge(w, u, sources);
		}
	}

	E += cnt[v] * deg_in[u];
	cnt[u] += cnt[v];
	root[v] = u;
}

void query(int u, int v)
{
	unordered_set<int> sources = {u};
	int root_u = get_root(u), root_v = get_root(v);
	if (root_u == root_v) return;
	add_edge(root_u, root_v, sources);
	// cout << "Y " << E << endl;

	if (back_edges[root_u].find(root_v) != back_edges[root_u].end()) {
		join_stack.emplace(root_u, root_v);
	}

	while (!join_stack.empty()) {
		auto p = join_stack.top();
		join_stack.pop();
		join(p.first, p.second);
		// cout << "join " << p.first << ' ' << p.second << ": " << E << endl;
	}
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N >> M;
	init();
	while (M--) {
		int u, v;
		cin >> u >> v;
		query(u, v);
		cout << E << '\n';
	}
	cerr << 1.0 * clock() / CLOCKS_PER_SEC << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 11264 KB Output is correct
2 Correct 11 ms 11392 KB Output is correct
3 Correct 11 ms 11392 KB Output is correct
4 Correct 11 ms 11136 KB Output is correct
5 Correct 11 ms 11264 KB Output is correct
6 Correct 11 ms 11264 KB Output is correct
7 Correct 12 ms 11392 KB Output is correct
8 Correct 13 ms 11392 KB Output is correct
9 Correct 12 ms 11392 KB Output is correct
10 Correct 12 ms 11264 KB Output is correct
11 Correct 11 ms 11264 KB Output is correct
12 Correct 14 ms 11264 KB Output is correct
13 Correct 11 ms 11264 KB Output is correct
14 Correct 13 ms 11392 KB Output is correct
15 Correct 12 ms 11392 KB Output is correct
16 Correct 12 ms 11264 KB Output is correct
17 Correct 12 ms 11392 KB Output is correct
18 Correct 12 ms 11392 KB Output is correct
19 Correct 12 ms 11392 KB Output is correct
20 Correct 14 ms 11312 KB Output is correct
21 Correct 12 ms 11392 KB Output is correct
22 Correct 15 ms 11392 KB Output is correct
23 Correct 12 ms 11264 KB Output is correct
24 Correct 12 ms 11392 KB Output is correct
25 Correct 12 ms 11392 KB Output is correct
26 Correct 12 ms 11392 KB Output is correct
27 Correct 11 ms 11392 KB Output is correct
28 Correct 11 ms 11392 KB Output is correct
29 Correct 12 ms 11392 KB Output is correct
30 Correct 12 ms 11264 KB Output is correct
31 Correct 16 ms 11392 KB Output is correct
32 Correct 12 ms 11392 KB Output is correct
33 Correct 12 ms 11392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 11264 KB Output is correct
2 Correct 11 ms 11392 KB Output is correct
3 Correct 11 ms 11392 KB Output is correct
4 Correct 11 ms 11136 KB Output is correct
5 Correct 11 ms 11264 KB Output is correct
6 Correct 11 ms 11264 KB Output is correct
7 Correct 12 ms 11392 KB Output is correct
8 Correct 13 ms 11392 KB Output is correct
9 Correct 12 ms 11392 KB Output is correct
10 Correct 12 ms 11264 KB Output is correct
11 Correct 11 ms 11264 KB Output is correct
12 Correct 14 ms 11264 KB Output is correct
13 Correct 11 ms 11264 KB Output is correct
14 Correct 13 ms 11392 KB Output is correct
15 Correct 12 ms 11392 KB Output is correct
16 Correct 12 ms 11264 KB Output is correct
17 Correct 12 ms 11392 KB Output is correct
18 Correct 12 ms 11392 KB Output is correct
19 Correct 12 ms 11392 KB Output is correct
20 Correct 14 ms 11312 KB Output is correct
21 Correct 12 ms 11392 KB Output is correct
22 Correct 15 ms 11392 KB Output is correct
23 Correct 12 ms 11264 KB Output is correct
24 Correct 12 ms 11392 KB Output is correct
25 Correct 12 ms 11392 KB Output is correct
26 Correct 12 ms 11392 KB Output is correct
27 Correct 11 ms 11392 KB Output is correct
28 Correct 11 ms 11392 KB Output is correct
29 Correct 12 ms 11392 KB Output is correct
30 Correct 12 ms 11264 KB Output is correct
31 Correct 16 ms 11392 KB Output is correct
32 Correct 12 ms 11392 KB Output is correct
33 Correct 12 ms 11392 KB Output is correct
34 Correct 15 ms 11520 KB Output is correct
35 Correct 128 ms 17656 KB Output is correct
36 Correct 158 ms 21112 KB Output is correct
37 Correct 160 ms 21240 KB Output is correct
38 Correct 161 ms 20728 KB Output is correct
39 Correct 16 ms 11520 KB Output is correct
40 Correct 19 ms 11648 KB Output is correct
41 Correct 17 ms 11648 KB Output is correct
42 Correct 16 ms 11520 KB Output is correct
43 Correct 497 ms 19960 KB Output is correct
44 Correct 457 ms 20088 KB Output is correct
45 Correct 15 ms 11520 KB Output is correct
46 Correct 18 ms 11520 KB Output is correct
47 Correct 28 ms 11904 KB Output is correct
48 Correct 35 ms 12032 KB Output is correct
49 Correct 29 ms 12672 KB Output is correct
50 Correct 163 ms 21212 KB Output is correct
51 Correct 331 ms 16760 KB Output is correct
52 Correct 155 ms 19064 KB Output is correct
53 Correct 33 ms 12544 KB Output is correct
54 Correct 150 ms 20088 KB Output is correct
55 Correct 22 ms 12288 KB Output is correct
56 Correct 20 ms 12288 KB Output is correct
57 Correct 19 ms 12288 KB Output is correct
58 Correct 19 ms 12288 KB Output is correct
59 Correct 16 ms 11520 KB Output is correct
60 Correct 131 ms 16504 KB Output is correct
61 Correct 81 ms 12664 KB Output is correct
62 Correct 163 ms 20472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 11264 KB Output is correct
2 Correct 11 ms 11392 KB Output is correct
3 Correct 11 ms 11392 KB Output is correct
4 Correct 11 ms 11136 KB Output is correct
5 Correct 11 ms 11264 KB Output is correct
6 Correct 11 ms 11264 KB Output is correct
7 Correct 12 ms 11392 KB Output is correct
8 Correct 13 ms 11392 KB Output is correct
9 Correct 12 ms 11392 KB Output is correct
10 Correct 12 ms 11264 KB Output is correct
11 Correct 11 ms 11264 KB Output is correct
12 Correct 14 ms 11264 KB Output is correct
13 Correct 11 ms 11264 KB Output is correct
14 Correct 13 ms 11392 KB Output is correct
15 Correct 12 ms 11392 KB Output is correct
16 Correct 12 ms 11264 KB Output is correct
17 Correct 12 ms 11392 KB Output is correct
18 Correct 12 ms 11392 KB Output is correct
19 Correct 12 ms 11392 KB Output is correct
20 Correct 14 ms 11312 KB Output is correct
21 Correct 12 ms 11392 KB Output is correct
22 Correct 15 ms 11392 KB Output is correct
23 Correct 12 ms 11264 KB Output is correct
24 Correct 12 ms 11392 KB Output is correct
25 Correct 12 ms 11392 KB Output is correct
26 Correct 12 ms 11392 KB Output is correct
27 Correct 11 ms 11392 KB Output is correct
28 Correct 11 ms 11392 KB Output is correct
29 Correct 12 ms 11392 KB Output is correct
30 Correct 12 ms 11264 KB Output is correct
31 Correct 16 ms 11392 KB Output is correct
32 Correct 12 ms 11392 KB Output is correct
33 Correct 12 ms 11392 KB Output is correct
34 Correct 15 ms 11520 KB Output is correct
35 Correct 128 ms 17656 KB Output is correct
36 Correct 158 ms 21112 KB Output is correct
37 Correct 160 ms 21240 KB Output is correct
38 Correct 161 ms 20728 KB Output is correct
39 Correct 16 ms 11520 KB Output is correct
40 Correct 19 ms 11648 KB Output is correct
41 Correct 17 ms 11648 KB Output is correct
42 Correct 16 ms 11520 KB Output is correct
43 Correct 497 ms 19960 KB Output is correct
44 Correct 457 ms 20088 KB Output is correct
45 Correct 15 ms 11520 KB Output is correct
46 Correct 18 ms 11520 KB Output is correct
47 Correct 28 ms 11904 KB Output is correct
48 Correct 35 ms 12032 KB Output is correct
49 Correct 29 ms 12672 KB Output is correct
50 Correct 163 ms 21212 KB Output is correct
51 Correct 331 ms 16760 KB Output is correct
52 Correct 155 ms 19064 KB Output is correct
53 Correct 33 ms 12544 KB Output is correct
54 Correct 150 ms 20088 KB Output is correct
55 Correct 22 ms 12288 KB Output is correct
56 Correct 20 ms 12288 KB Output is correct
57 Correct 19 ms 12288 KB Output is correct
58 Correct 19 ms 12288 KB Output is correct
59 Correct 16 ms 11520 KB Output is correct
60 Correct 131 ms 16504 KB Output is correct
61 Correct 81 ms 12664 KB Output is correct
62 Correct 163 ms 20472 KB Output is correct
63 Correct 720 ms 81400 KB Output is correct
64 Correct 694 ms 81272 KB Output is correct
65 Correct 684 ms 81272 KB Output is correct
66 Correct 245 ms 24696 KB Output is correct
67 Correct 524 ms 31480 KB Output is correct
68 Correct 249 ms 24696 KB Output is correct
69 Execution timed out 5072 ms 76848 KB Time limit exceeded
70 Halted 0 ms 0 KB -