๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Problem Solving

[C++] ๋ฐฑ์ค€ 17073 - ๋‚˜๋ฌด ์œ„์˜ ๋น—๋ฌผ

๋ฌธ์ œ

 

https://www.acmicpc.net/problem/17073


ํ’€์ด

 

๋‹จ์ˆœํžˆ (W / leaf์˜ ๊ฐœ์ˆ˜)์˜ ๊ฐ’๋งŒ ๊ตฌํ•˜๋ฉด ๋ผ์„œ ํŠธ๋ฆฌ๋ฅผ ๊ตฌํ˜„ํ•  ํ•„์š” ์—†์ด ์—ฐ๊ฒฐ๋œ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜๊ฐ€ 1์ธ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ. ๋ฌธ์ œ์—์„œ N์˜ ๋ฒ”์œ„๊ฐ€ 2 ์ด์ƒ์ด๋ผ ํ•˜์˜€์œผ๋ฏ€๋กœ ๋…ธ๋“œ๊ฐ€ ํ•œ ๊ฐœ์ผ๋•Œ์˜ ์˜ˆ์™ธ ์ผ€์ด์Šค๋Š” ๊ด€๋ฆฌํ•˜์ง€ ์•Š์•„๋„ ๋จ.

 

#include <cstdio>
using namespace std;

int N, U, V; 
double W, leafCnt = 0;
int edgeCnt[500001] = { 0 };

int main() {
	scanf("%d %lf", &N, &W);

	for (int i = 0; i < N - 1; i++) {
		scanf("%d %d", &U, &V);
		edgeCnt[U]++;
		edgeCnt[V]++;
	}

	for (int i = 2; i <= N; i++) {
		if (edgeCnt[i] == 1) {
			leafCnt++;
		}
	}

	printf("%lf", W / leafCnt);
	return 0;
}