Submission #206552

# Submission time Handle Problem Language Result Execution time Memory
206552 2020-03-04T00:18:33 Z MohamedAhmed04 Mag (COCI16_mag) C++14
120 / 120
589 ms 162680 KB
#include <bits/stdc++.h>

using namespace std ;

const int MAX = 1e6 + 10 ;

int arr[MAX] , dpdown[MAX] , dpup[MAX] ;
int n ;

vector< vector<int> >adj(MAX) ;

int Max = 0 ;

void dfs(int node , int par)
{
	if(arr[node] == 1)
		dpdown[node] = 1 ;
	else
		dpdown[node] = 0 ;
	int a = 0 , b = 0 ;
	for(auto &child : adj[node])
	{
		if(child == par)
			continue ;
		dfs(child , node) ;
		if(dpdown[node])
		{
			dpdown[node] = max(dpdown[node] , dpdown[child]+1) ;
			if(dpdown[child] > a)
				b = a , a = dpdown[child] ;
			else if(dpdown[child] > b)
				b = dpdown[child] ;
		}
	}
	if(arr[node] == 1)
		Max = max(Max , a+b+1) ;
}

bool flag = false ;

void dfs2(int node , int par)
{
	int cnt = 0 ;
	for(auto &child : adj[node])
	{
		if(child == par)
			continue ;
		dfs2(child , node) ;
		if(arr[node] == 2)
			cnt += (dpdown[child] == Max) ;
	}
	if(cnt > 1)
		flag = true ;
	return ;
}

void dfs3(int node , int par , int best)
{
	dpup[node] = best + 1 ;
	if(arr[node] != 1)
	{
		dpup[node] = 0 ;
		for(auto &child : adj[node])
		{
			if(child == par)
				continue ;
			dfs3(child , node , 0) ;
		}
		if(arr[node] == 2)
		{
			for(auto &child : adj[node])
			{
				if(child == par)
					continue ;
				if(dpdown[child] == Max && best == Max)
					flag = true ;
			}
		}
		return ;
	}
	int mx = dpup[node] , c = node ;
	for(auto &child : adj[node])
	{
		if(child == par)
			continue ;
		if(dpdown[child]+1 > mx)
			mx = dpdown[child]+1 , c = child ;
	}
	for(auto &child : adj[node])
	{
		if(child == par)
			continue ;
		if(c != child)
			dfs3(child , node , mx) ;
		else
		{
			int mx2 = dpup[node] ;
			for(auto &child2 : adj[node])
			{
				if(child2 == par || child == child2)
					continue ;
				if(dpdown[child2]+1 > mx2)
					mx2 = dpdown[child2]+1 ;
			}
			dfs3(child , node , mx2) ;
		}
	}
	return ;
}

int main()
{
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	cin>>n ;
	for(int i = 0 ; i < n-1 ;  ++i)
	{
		int x , y ;
		cin>>x>>y ;
		adj[x].push_back(y) ;
		adj[y].push_back(x) ;
	}
	int cnt = 0 ;
	for(int i = 1 ; i <= n ; ++i)
	{
		cin>>arr[i] ;
		cnt += (arr[i] == 1) ;
	}
	if(!cnt)
		return cout<<*min_element(arr+1 , arr+n+1)<<"/"<<1<<"\n" , 0 ;
	dfs(1 , -1) ;
	int ans = Max ;
	dfs2(1 , -1) ;
	dfs3(1 , -1 , 0) ;
	if(flag == true)
		return cout<<2<<"/"<<ans*2+1<<"\n" , 0 ;
	else
		return cout<<1<<"/"<<ans<<"\n" , 0 ;
}		
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23928 KB Output is correct
2 Correct 19 ms 23928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 23928 KB Output is correct
2 Correct 19 ms 23928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 437 ms 89592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 23928 KB Output is correct
2 Correct 535 ms 144120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 533 ms 142168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 456 ms 66680 KB Output is correct
2 Correct 330 ms 55544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 421 ms 67816 KB Output is correct
2 Correct 86 ms 28280 KB Output is correct
3 Correct 589 ms 162680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 28280 KB Output is correct
2 Correct 468 ms 82552 KB Output is correct
3 Correct 395 ms 53580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 423 ms 64368 KB Output is correct
2 Correct 463 ms 66124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 460 ms 67320 KB Output is correct
2 Correct 377 ms 45944 KB Output is correct