목록Algorithm/C++ (148)
cgy12306
// https://www.acmicpc.net/problem/1967 // 트리의 지름 #include #include #include using namespace std; vector tree[10001]; bool visited[10001]; int sum = 0, sp; void dfs(int start, int length) { visited[start] = true; if (sum < length) { sum = length; sp = start; } for (auto next : tree[start]) { if (!visited[next.first]) { dfs(next.first, length + next.second); } } } int main() { ios::sync_with_stdi..
// https://www.acmicpc.net/problem/14675 // 단절점과 단절선 #include #include using namespace std; int N, question; vector tree[100001]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> N; for (int i = 0; i > a >> b; tree[a].push_back(b); tree[b].push_back(a); } cin >> question; for (int q = 0; q > t >> k; i..
// https://www.acmicpc.net/problem/1068 // 트리 #include #include #include using namespace std; int del, N; vector tree[51]; bool deleted[51]; void bfs(int start) { queue que; que.push(start); while (!que.empty()) { int leaf = que.front(); que.pop(); deleted[leaf] = true; for (int i = 0; i < tree[leaf].size(); i++) { if (deleted[tree[leaf][i]]) continue; que.push(tree[leaf][i]); } } } int main() {..
// https://www.acmicpc.net/problem/5639 // 이진 검색 트리 #include #include using namespace std; typedef struct TreeNode { int key; struct TreeNode *left, *right; }TreeNode; TreeNode *insert(TreeNode *node, int key) { if (node == NULL) { node = (TreeNode *)malloc(sizeof(TreeNode)); node->key = key; node->left = NULL; node->right = NULL; } else if (node->key > key) { node->left = insert(node->left, key..
// https://www.acmicpc.net/problem/1991 // 트리 순회 #include #include using namespace std; typedef struct TreeNode{ char name; struct TreeNode *left; struct TreeNode *right; }TreeNode; TreeNode *init(char name) { TreeNode *node = (TreeNode *)malloc(sizeof(TreeNode)); if (node == NULL) return NULL; node->name = name; node->left = NULL; node->right = NULL; return node; } void insert(TreeNode *node, c..
// https://www.acmicpc.net/problem/11286 // 절댓값 힙 #include #include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int N; priority_queue pq; cin >> N; for (int i = 0; i > num; if (num == 0) { if (pq.empty()) { cout
// https://www.acmicpc.net/problem/11279 // 최대 힙 #include #include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int N; priority_queue pq; cin >> N; for (int i = 0; i > num; if (num == 0) { if (pq.empty()) { cout
// https://www.acmicpc.net/problem/1927 // 최소 힙 #include #include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int N; priority_queue pq; cin >> N; for (int i = 0; i > num; if (num == 0) { if (pq.empty()) { cout