General
 
 
# Author Problem Lang Verdict Time Memory Sent Judged  
69076469 Practice:
mayankk_agarwal
763A - 52 C++14 (GCC 6-32) Wrong answer on test 7 1887 ms 3624 KB 2020-01-19 06:34:37 2020-01-19 06:34:37
→ Source
#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;

#define fast ios_base::sync_with_stdio(false), cin.tie(NULL);  //will need explicit io flushing using fflush(stdout) for interactive programs


ll n,u,v,val;
vector<pair<ll,ll>> edges;
vector<ll> colors;


ll solve()
{
	n = edges.size();
	if(n==0 || n==1)
	{
		if(n==0)
			return u;
		return edges[0].first;    //this is the only cross edge. In any case, both of them can be the vertex help			
	}

	// cout << edges[0].first << ' ' << edges[0].second << ' ' << edges[1].first << ' ' << edges[1].second << '\n';
	ll vertex;

	if(edges[1].first!= edges[0].first && edges[1].first!= edges[0].second && edges[1].second!= edges[0].first && edges[1].second!= edges[0].second)
		return -1;
	if(edges[1].first == edges[0].first || edges[1].first == edges[0].second)
		vertex = edges[1].first;
	else
		vertex = edges[1].second;


	//from now on only allowed edges have to contain vertex (as all are cross coloured)

	for(ll i = 2; i < n; ++i)
	{
		if(edges[i].first != vertex && edges[i].second != vertex)
			return -1;
	}
	return vertex;



}

int main()
{
	fast;
	cin >> n;

	for(ll i = 1; i < n; ++i)
	{
		cin >> u >> v;
		edges.push_back({u,v});
	}

	colors.push_back(0);
	for(ll i = 0; i < n; ++i)
	{
		cin >> val;
		colors.push_back(val);
	}

	for(ll i = 0; i < n-1; ++i)
	{
		if(colors[edges[i].first] == colors[edges[i].second]) edges.erase(edges.begin()+i);
	}

	ll ans = solve();
	if(ans == -1)
		cout << "NO" << '\n';
	else
	{
		cout << "YES" << '\n';
		cout << ans << '\n';
	}

}
?
Time: ? ms, memory: ? KB
Verdict: ?
Input
?
Participant's output
?
Jury's answer
?
Checker comment
?
Diagnostics
?
Click to see test details