답안 #1081662

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1081662 2024-08-30T08:56:12 Z Elias Pipes (CEOI15_pipes) C++17
0 / 100
5000 ms 9724 KB
#include <bits/stdc++.h>

using namespace std;

int parent[100000];
int depth[100000];
int root[100000];
int union_parent[100000];
vector<vector<int>> children;

int find(int a)
{
	if (union_parent[a] == a)
		return a;
	return union_parent[a] = find(union_parent[a]);
}

bool unite(int a, int b)
{
	a = find(a);
	b = find(b);

	if (a == b)
		return false;

	if (depth[b] > depth[a])
		swap(a, b);

	union_parent[a] = b;
	return true;
}

vector<vector<int>> adj;

int iter = 0;

void dfs_reconstruct(int i, int d, int p, int r)
{
	root[i] = r;
	parent[i] = p;
	depth[i] = d;

	iter++;

	assert(iter < 10000);

	for (int c : adj[i])
	{
		if (c == p)
			continue;
		dfs_reconstruct(c, d + 1, i, r);
	}
}

signed main()
{
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	int n, m;
	cin >> n >> m;

	children.resize(n);
	adj.resize(n);

	for (int i = 0; i < n; i++)
	{
		parent[i] = root[i] = union_parent[i] = i;
		children[i] = vector<int>{i};
		depth[i] = 1;
	}

	while (m--)
	{
		for (int i = 0; i < n; i++)
			if (parent[i] != i)
			assert(depth[i] > depth[parent[i]]);

		int a, b;
		cin >> a >> b;

		a--, b--;



		if (root[a] == root[b])
		{
			a = find(a);
			b = find(b);
			
			while (a != b)
			{
				assert(root[a] == root[b]);

				if (depth[a] < depth[b])
					swap(a, b);

				// assert(parent[a] != a);

				assert(unite(a, parent[a]));

				a = find(a);
				b = find(b);
			}
		}
		else
		{
			if (children[root[a]].size() < children[root[b]].size())
				swap(a, b);

			vector<pair<int, int>> inserts;

			vector<int> new_nodes;

			for (int i : children[root[b]])
			{
				int p = parent[i];

				new_nodes.push_back(i);
				new_nodes.push_back(p);

				if (i == p)
					continue;

				inserts.push_back({i, p});
				inserts.push_back({p, i});
			}
			children[root[b]].clear();

			sort(inserts.begin(), inserts.end());
			inserts.erase(unique(inserts.begin(), inserts.end()), inserts.end());

			sort(new_nodes.begin(), new_nodes.end());
			new_nodes.erase(unique(new_nodes.begin(), new_nodes.end()), new_nodes.end());

			for (int node : new_nodes)
			{
				children[root[a]].push_back(node);
			}

			for (auto [a, b] : inserts)
			{
				adj[a].push_back(b);
			}

			iter = 0;
			dfs_reconstruct(b, depth[a] + 1, a, root[a]);

			for (auto [a, b] : inserts)
			{
				adj[a].clear();
			}
		}
	}

	for (int i = 0; i < n; i++)
	{
		if (find(i) != find(parent[i]))
			cout << i + 1 << " " << parent[i] + 1 << "\n";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 16 ms 1628 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 10 ms 1372 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5049 ms 5340 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 673 ms 6036 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5064 ms 7796 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5052 ms 8228 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5040 ms 9724 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5054 ms 9720 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5031 ms 9516 KB Time limit exceeded
2 Halted 0 ms 0 KB -