본문 바로가기
라이브러리/자료구조

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;
}

 

'라이브러리 > 자료구조' 카테고리의 다른 글

Segment tree  (0) 2021.08.12
BIT  (0) 2021.03.22
DSU  (0) 2021.03.22
Tree  (0) 2020.08.06

태그

,

댓글0