Submission #133432

# Submission time Handle Problem Language Result Execution time Memory
133432 2019-07-20T16:15:08 Z tdwn Mag (COCI16_mag) C++17
108 / 120
1782 ms 191968 KB
#include <bits/stdc++.h>
#define ll long long 
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn = 1000100;
vector<int>g[maxn];
ll arr[maxn], n;
ll dp[maxn], result1;
ll result2;

ll dpup[maxn];

void dfs(int node, int p) {
	vector<ll>v;

	if(p != -1) {
		if(arr[node] == 1) dpup[node] = 1 + dpup[p];
		else dpup[node] = 0;
	}

	for(int i:g[node]) {
		if(i != p) {
			dfs(i, node);

			if(arr[i] == 1) {
				v.pb(dp[i]);
			}
		}
	}

	sort(v.rbegin(), v.rend());

	if(arr[node] == 2) {
		v.pb(dpup[p]);
		v.pb(0);
		sort(v.rbegin(), v.rend());

		//cout<<node<<": "<<v[0]<<" "<<v[1]<<"\n";

		result2 = max(result2, v[0] + v[1] + 1LL);
		return;
	}

	if(arr[node] != 1) return;

	if(v.size() == 0) {
		dp[node] = 1LL;
		result1 = max(result1, dp[node]);
	}

	if(v.size() == 1) {
		dp[node] = 1 + v[0];
		result1 = max(result1, dp[node]);
	}
	else if(v.size() > 1){
		dp[node] = 1 + v[0];
		result1 = max(result1, v[0] + v[1] + 1);
	}
}

int main() {
	cin>>n;
	ll a, b;
	for(int i=0;i<n-1;i++) {
		cin>>a>>b;
		g[a].pb(b);
		g[b].pb(a);
	}

	ll minval = LLONG_MAX;
	for(int i=1;i<=n;i++) {
		cin>>arr[i];
		minval = min(minval, arr[i]);
	}

	if(minval != 1) {
		cout<<minval<<"/1\n";
	}
	else {
		dfs(1, -1);
		if(result2 > 2*result1) {
			cout<<"2/"<<result2<<"\n";
		}
		else {
			cout<<"1/"<<result1<<"\n";
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23800 KB Output is correct
2 Correct 24 ms 23928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23928 KB Output is correct
2 Correct 25 ms 23896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1400 ms 111708 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23772 KB Output is correct
2 Correct 1722 ms 191968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1741 ms 184400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1589 ms 78696 KB Output is correct
2 Correct 1178 ms 69284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1663 ms 82416 KB Output is correct
2 Correct 228 ms 30968 KB Output is correct
3 Correct 1782 ms 188560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 217 ms 29704 KB Output is correct
2 Correct 1588 ms 79716 KB Output is correct
3 Correct 1034 ms 57044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1537 ms 76668 KB Output is correct
2 Correct 1653 ms 78056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1624 ms 79708 KB Output is correct
2 Correct 1071 ms 52164 KB Output is correct