1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #include <stdio.h> #define SZ 10001 struct Node{ int parent = 0; int left = 0; int right = 0; }; int t, V, E, s1, s2, max_idx; int count_node(Node tree[], int node){ int ans = 1; if (tree[node].left != 0) ans += count_node(tree, tree[node].left); if (tree[node].right != 0) ans += count_node(tree, tree[node].right); return ans; } int main(){ //freopen("input.txt", "r", stdin); scanf("%d", &t); for (int tc = 1; tc <= t; tc++){ Node tree[SZ]; bool check[SZ] = { false, }; scanf("%d %d %d %d", &V, &E, &s1, &s2); int x, y; for (int i = 0; i < E; i++){ scanf("%d %d", &x, &y); if (tree[x].left == 0){ tree[x].left = y; } else{ tree[x].right = y; } tree[y].parent = x; } int cur = tree[s1].parent; while (true){ check[cur] = true; if (cur == 1) break; cur = tree[cur].parent; } cur = tree[s2].parent; while (true){ if (check[cur]){ printf("#%d %d %d\n", tc, cur, count_node(tree, cur)); break; } cur = tree[cur].parent; } } } | cs |
'SWEA::문제풀이' 카테고리의 다른 글
5648 원자 소멸 시뮬레이션 (0) | 2018.10.08 |
---|---|
5644 무선 충전 (0) | 2018.10.08 |
균형점 (0) | 2018.09.06 |
최대 상금 (0) | 2018.09.06 |
4676 늘어지는 소리 만들기 (0) | 2018.07.14 |