Submission #286567

# Submission time Handle Problem Language Result Execution time Memory
286567 2020-08-30T14:38:39 Z FlashGamezzz Mag (COCI16_mag) C++11
120 / 120
606 ms 252664 KB
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <string>
#include <utility>
#include <vector>

using namespace std;

long n, minM = 1000000000, maxP = 0, mag[1000000], pd[1000000] = {}, path[1000000] = {}, pn[1000000] = {};
vector <long> adj[1000000], mi[1000000];

void dfs(int i, int p) {
	minM = min(minM, mag[i]);
	for (int a : adj[i]) {
		if (a != p) {
			dfs(a, i);
		}
	}
	if (mag[i] == 1) {
		pd[i] = 1;
		for (int a : adj[i]) {
			if (a != p && mag[a] == 1) {
				pd[i] = max(pd[i], pd[a]+1);
			}
		}
		for (int a : adj[i]) {
			if (a != p && mag[a] == 1) {
				if (pd[a]+1 == pd[i]) {
					mi[i].push_back(a);
				}
			}
		}
		path[i] = pd[i];
		if (mi[i].size() == 1){
			long mind = mi[i][0];
			for (int a : adj[i]){
				if (a != p && mag[a] == 1 && a != mind) {
					path[i] = max(path[i], pd[i]+pd[a]);
				}
			}
			for (int a : adj[i]){
				if (a != p && mag[a] == 1 && a != mind) {
					if (pd[i]+pd[a] == path[i]){
						mi[i].push_back(a);
					}
				}
			}
		} else if (mi[i].size() > 1){
			path[i] = 2*pd[i]-1;
		}
		maxP = max(path[i], maxP);
	}
}
void dfs2(int i, int v){
	pn[i] = v;
	for (int a : mi[i]) {
		dfs2(a, v);
	}
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n;
	for (int i = 0; i < n-1; i++) {
		int a, b;
		cin >> a >> b;
		adj[a-1].push_back(b-1);
		adj[b-1].push_back(a-1);
	}
	for (int i = 0; i < n; i++) {
		cin >> mag[i];
	}
	dfs(0, -1);
	if (minM == 1){
		for (int i = 0; i < n; i++){
			if (path[i] == maxP){
				dfs2(i, i+1);
			}
		}
		bool twok = false;
		for (int i = 0; i < n; i++){
			if (mag[i] == 2){
				int count = 0;
				for (int a : adj[i]){
					if (pn[a] != 0){
						count++;
						if (count >= 2){
							twok = true;
							break;
						}
					}
				}
			}
		}
		if (twok){
			cout << "2/" << (2*maxP+1);
		} else {
			cout << "1/" << maxP;
		}
	} else {
		cout << minM << "/1";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 34 ms 47360 KB Output is correct
2 Correct 37 ms 47360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 47352 KB Output is correct
2 Correct 32 ms 47488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 488 ms 165084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 47360 KB Output is correct
2 Correct 606 ms 209812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 575 ms 231176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 512 ms 145528 KB Output is correct
2 Correct 390 ms 121848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 418 ms 112348 KB Output is correct
2 Correct 128 ms 57428 KB Output is correct
3 Correct 576 ms 252664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 56952 KB Output is correct
2 Correct 522 ms 156488 KB Output is correct
3 Correct 438 ms 95992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 434 ms 125116 KB Output is correct
2 Correct 503 ms 152696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 495 ms 142456 KB Output is correct
2 Correct 462 ms 94372 KB Output is correct