답안 #311686

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
311686 2020-10-11T02:48:43 Z shrek12357 Mag (COCI16_mag) C++14
24 / 120
1879 ms 191824 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <climits>
#include <cmath>
#include <fstream>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define ll long long
//cin.tie(0);
//ios_base::sync_with_stdio(0);s

ll best = INT_MAX, nodes = 1;
const int MAXN = 1e6 + 5;
ll mag[MAXN];
vector<ll> adjList[MAXN];
ll dp[MAXN];

void dfs(ll src, ll par) {
	vector<ll> nums;
	ll b1 = 0, b2 = 0;
	for (auto i : adjList[src]) {
		if (i == par) {
			continue;
		}
		dfs(i, src);
		nums.push_back(dp[i]);
		dp[src] = max(dp[src], dp[i]);
		if (dp[i] >= b1) {
			b2 = b1;
			b1 = dp[i];
		}
		else if (dp[i] > b2) {
			b2 = dp[i];
		}
	}
	if (mag[src] == 1) {
		dp[src]++;
	}
	else {
		dp[src] = 0;
	}
	nums.push_back(0);
	sort(nums.begin(), nums.end());
	if (mag[src] == 1) {
		ll tot = b1 + b2 + 1;
		if (best * tot > nodes) {
			nodes = tot;
			best = 1;
		}
	}
	if (mag[src] == 2) {
		if (nums.size() == 1) {
			return;
		}
		for (int i = 1; i < nums.size(); i++) {
			if (nums[i] - nums[i - 1] == 1) {
				ll tot = nums[i] * 2;
				if (best * tot > 2 * nodes) {
					best = 2;
					nodes = tot;
				}
			}
		}
	}
}

ll gcd(ll a, ll b) {
	if (a < b) {
		swap(a, b);
	}
	if (b == 0) {
		return a;
	}
	return (a%b, b);
}

int main() {
	ll n;
	cin >> n;
	for (int i = 0; i < n - 1; i++) {
		ll a, b;
		cin >> a >> b;
		a--;
		b--;
		adjList[a].push_back(b);
		adjList[b].push_back(a);
	}
	for (int i = 0; i < n; i++) {
		cin >> mag[i];
		best = min(best, mag[i]);
	}
	dfs(0, 0);
	ll g = gcd(best, nodes);
	ll finA = best / g, finB = nodes / g;
	cout << finA << "/" << finB << endl;
}

Compilation message

mag.cpp: In function 'void dfs(long long int, long long int)':
mag.cpp:61:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |   for (int i = 1; i < nums.size(); i++) {
      |                   ~~^~~~~~~~~~~~~
mag.cpp: In function 'long long int gcd(long long int, long long int)':
mag.cpp:80:11: warning: left operand of comma operator has no effect [-Wunused-value]
   80 |  return (a%b, b);
      |          ~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 23936 KB Output is correct
2 Correct 17 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 18 ms 23808 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1521 ms 111512 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 18 ms 23808 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1879 ms 191824 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1758 ms 70544 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1803 ms 77580 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 214 ms 29048 KB Output is correct
2 Incorrect 1750 ms 71780 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1663 ms 70100 KB Output is correct
2 Incorrect 1814 ms 70392 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1750 ms 71928 KB Output is correct
2 Correct 1081 ms 50424 KB Output is correct