목록Algorithm (154)
cgy12306
// https://www.acmicpc.net/problem/10844 // 쉬운 계단 수 #include using namespace std; int dp[101][101]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, sum = 0; cin >> n; dp[1][0] = 0; for (int i = 1; i
// https://www.acmicpc.net/problem/9466 // 텀 프로젝트 #include #include using namespace std; int T, n, cnt = 0; int student[100002]; bool visited[100002], done[100002]; void dfs(int start) { visited[start] = true; int next = student[start]; if (!visited[next]) { dfs(next); } else if (!done[next]) { for (int i = next; i != start; i = student[i]) { cnt++; } cnt++; } done[start] = true; } int main() { ..
// https://www.acmicpc.net/problem/2146 // 다리 만들기 #include #include #include #include using namespace std; bool visited[101][101]; int N, map[101][101]; int dy[] = { -1, 1 , 0, 0 }; int dx[] = { 0, 0, -1, 1 }; int bridge = 987654321; int mapcnt = 2; // dfs void check(int x, int y) { visited[y][x] = true; map[y][x] = mapcnt; for (int i = 0; i < 4; i++) { int ax = x + dx[i]; int ay = y + dy[i]; if..
// https://www.acmicpc.net/problem/1167 // 트리의 지름 #include #include #include using namespace std; vector tree[100001]; bool visited[100001]; 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_st..
// 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..