본문 바로가기
알고리즘/라이브러리

Tree

by 유시은 2020. 8. 6.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MAX = 100001;
int parent[MAX];

struct Tree {
	int root, size;
	vector<int> child[MAX], edge[MAX];
	Tree() { root = -1; size = 0; }
	Tree(int n) { root = -1; size = n; }

	void addEdge(int u, int v) {
		edge[u].push_back(v);
		edge[v].push_back(u);
	}
	void build(int id) {
		if (root == -1) root = id;
		for (int next : edge[id]) {
			if (!parent[next]) {
				parent[next] = id;
				build(next);
			}
		}
	}
};

int main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	int n; cin >> n;
	Tree* T = new Tree(n);

	for (int i = 0; i < n - 1; ++i) {
		int u, v; cin >> u >> v;
		T->addEdge(u, v);
	}
	T->build(1);
	// delete T;

	return 0;
}

 

'알고리즘 > 라이브러리' 카테고리의 다른 글

Topological Sort  (0) 2020.09.16
Trie  (0) 2020.09.04
Floyd-Warshall  (0) 2020.08.15
KMP  (0) 2020.08.11
Dijkstra  (0) 2020.08.06

댓글