SOLUTION INFO
C++ · main.cpp
- 작성자
- tony9402
- 공동 작성자
- 없음
#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> graph;
vector<int> siz;
void dfs(int cur, int prv) {
siz[cur] = 1;
for(auto &nxt: graph[cur]) {
if(nxt == prv) continue;
dfs(nxt, cur);
siz[cur] += siz[nxt];
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int N, R, Q; cin >> N >> R >> Q;
graph.resize(N + 1);
siz.resize(N + 1);
for(int i=1;i<N;i++) {
int a, b; cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
dfs(R, R);
while(Q--) {
int x;cin >> x;
cout << siz[x] << '\n';
}
return 0;
}
SOLUTION DESCRIPTION
풀이 설명
등록된 풀이 설명이 없습니다.
SOLUTION INFO
Java · Main.java
- 작성자
- suin8
- 공동 작성자
- 없음
import java.util.*;
import java.io.*;
// 문제에서 주어진 힌트를 그대로 구현하였습니다.
public class Main {
static int[] subtreesize, parent;
static ArrayList<Integer>[] tree, list;
public static void main(String[] args) throws IOException {
FastReader rd = new FastReader();
int N = rd.nextInt();
int R = rd.nextInt();
int Q = rd.nextInt();
tree = new ArrayList[N + 1];
list = new ArrayList[N + 1];
subtreesize = new int[N + 1];
parent = new int[N + 1];
for(int i = 0;i <= N;i++) {
tree[i] = new ArrayList<Integer>();
list[i] = new ArrayList<Integer>();
}
for(int i = 0;i < N - 1;i++) {
int U = rd.nextInt();
int V = rd.nextInt();
list[U].add(V);
list[V].add(U);
}
// 문제에 주어진 힌트
makeTree(R, -1);
countSubtreeNodes(R);
for(int i = 0;i < Q;i++) {
int U = rd.nextInt();
System.out.println(subtreesize[U]);
}
}
// 입력받은 list를 토대로 트리를 만듭니다.
static void makeTree(int curNode, int p) {
for(int node : list[curNode]) {
if(node != p) {
tree[curNode].add(node);
parent[node] = curNode;
makeTree(node, curNode);
}
}
}
// dp와 재귀호출로 서브트리크기를 센다.
static void countSubtreeNodes(int curNode) {
// 자신도 size에 포함하기 때문에 1부터 시작
subtreesize[curNode] = 1;
for(int node : tree[curNode]) {
countSubtreeNodes(node);
subtreesize[curNode] += subtreesize[node];
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
SOLUTION DESCRIPTION
풀이 설명
등록된 풀이 설명이 없습니다.
SOLUTION INFO
Python · main.py
- 작성자
- cieske
- 공동 작성자
- 없음
import sys
def input():
return sys.stdin.readline().rstrip()
sys.setrecursionlimit(1000000) # 꼭 걸어주도록 하자.... (Python 인 경우)
n, root, query = map(int, input().split())
tree = [[] for _ in range(n+1)]
for _ in range(n-1): # Tree 생성
x, y = map(int, input().split())
tree[x].append(y)
tree[y].append(x)
num_child = [0]*(n+1)
def dfs(cur, parent):
if len(tree[cur]) == 1 and parent != -1: # Leaf node라면
num_child[cur] = 1
return 1
n_sub = 0 # cur를 root로 하는 subtree의 node 개수
for child in tree[cur]:
if child != parent: # 각 child를 root로 하는 subtree의 node 개수 추가
n_sub += dfs(child, cur)
num_child[cur] = n_sub + 1 # 본인 추가
return n_sub + 1
dfs(root, -1)
for _ in range(query):
print(num_child[int(input())])
SOLUTION DESCRIPTION
풀이 설명
등록된 풀이 설명이 없습니다.