포럼
문제 USACO0588

소 접촉자 추적

설명

농부 존에게는 \(1\dots N\)으로 라벨이 붙은 \(N\) (\(2\le N\le 10^5\))마리의 소가 있으며, 소들 사이의 연결 관계는 트리로 표현된다. 안타깝게도 소들 사이에 병이 퍼지고 있다.

처음에 일부 소들이 감염된 상태로 시작한다. 매일 밤, 감염된 소는 자신의 이웃들에게 병을 퍼뜨린다. 한 번 감염된 소는 계속 감염된 상태로 남는다. 며칠 밤이 지난 후, 농부 존은 문제가 있음을 깨닫고 소들을 검사하여 누가 병에 걸렸는지 알아냈다.

밤의 수에 대한 \(Q\) (\(1\le Q\le 20\))개의 서로 다른 값이 주어지며, 각 값은 \([0,N]\) 범위의 정수이다. 각 밤의 수에 대해, 처음에 병을 가지고 시작했을 수 있는 소의 최소 마릿수를 구하거나, 그 밤의 수가 주어진 정보와 모순됨을 판별하라.

문제 제공: Suhas Nagar, Brandon Wang

제약

채점 방식

  • 입력 4-5: \(N \le 10\)
  • 입력 6-8: 모든 소가 감염되어 있다.
  • 입력 9-11: \(N \le 400\)
  • 입력 12-23: 추가 제약 조건 없음.

문제 제공: Suhas Nagar, Brandon Wang

입력 형식

첫째 줄에 \(N\)이 주어진다.

다음 줄에 길이 \(N\)의 비트 문자열이 주어진다. \(i\)번째 비트는 \(i\)번째 소가 감염되었으면 1, 아니면 0이다. 적어도 한 마리의 소는 감염되어 있다.

다음 \(N-1\)개의 줄에 트리의 간선이 주어진다.

그다음 \(Q\)가 주어지고, 이어서 밤의 수에 대한 \(Q\)개의 값이 주어진다.

출력 형식

\(Q\)개의 줄에 걸쳐 각 밤의 수에 대한 답을 출력하고, 불가능하면 \(-1\)을 출력한다.

예제 1
입력
5
11111
1 2
2 3
3 4
4 5
6
5
4
3
2
1
0
출력
1
1
1
1
2
5
설명

For the first four queries, one possibility is that just cow 3 started with the
illness. For the fifth query (1 night), one possibility is that cows 2 and 4
started with the illness. For the sixth query (0 nights), one possibility is
that all five cows started with the illness.

예제 2
입력
10
1111111111
1 2
2 3
2 4
2 5
2 6
6 7
7 8
8 9
9 10
11
0
1
2
3
4
5
6
7
8
9
10
출력
10
3
2
1
1
1
1
1
1
1
1
설명

For the first query (0 nights), one possibility is that all ten cows started
with the illness. For the second query (1 night), one possibility is that cows
2, 7, and 9 started with the illness. For the third query (2 nights), one
possibility is that cows 2 and 9 started with the illness. For the fourth to
eleventh queries, one possibility is that just cow 7 started with the illness.

예제 3
입력
5
11100
1 2
2 3
3 4
4 5
6
0
1
2
3
4
5
출력
3
1
1
-1
-1
-1
설명

For the first query (0 nights), one possibility is that cows 1, 2, and 3 started
with the illness. For the second query (1 night), one possibility is that just
cow 2 started with the illness. For the third query (2 nights), one possibility
is that just cow 1 started with the illness. For the fourth through sixth
queries, there is no consistent possibility.

문제 정보

riseoj 작성

출처 올림피아드 > USACO > 2023-2024 > December > Platinum

태그

평가 및 의견

Cowntact Tracing

개요
출제자 난이도 Unrated 레이팅 미적용 의견 0 / 1 공개 집계 (커뮤니티 난이도, 주요 주제, 품질)는 의견이 충분히 모이면 공개됩니다.

Log in to rate problems.

개별 의견

아직 의견이 없습니다. 자격이 된다면 위 양식에서 가장 먼저 평가해 보세요.

풀이 제출

Cowntact Tracing

게스트로 둘러보고 있습니다. 로그인하면 풀이를 제출하고 진행 상황을 확인할 수 있습니다. 로그인하고 제출하기
공개
C++20 Tab 들여쓰기 · Ctrl+/ 주석 토글 · Enter 자동 들여쓰기
1 1 1 0 공백: 4 · UTF-8