This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1e5 + 69;
int a[maxn];
vector <int> adj[maxn];
int n;
int cnt = 0;
vector <int> v;
vector <int> col;
bool vis[maxn];
int colpre[5];
void dfs(int u, int parent)
{
//vis[u] = true;
//cout<<"Vertex "<<u<<" Colours : "<<"\n";
//for (int i=0; i<(int)(col.size()); i++)
//cout<<col[i]<<" ";
//cout<<"\n";
if (colpre[a[u]] ==1)
return;
colpre[a[u]] = 1;
v.push_back(u);
for (int v:adj[u])
{
//if (!vis[v])
if (v!=parent)
dfs(v, u);
}
colpre[a[u]] = 0;
}
void Solve()
{
cin>>n;
for (int i=1; i<=n; i++){
cin>>a[i];a[i]--;
}
for (int i=1; i<n; i++)
{
int a, b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i=1; i<=n; i++)
{
v.clear();
col.clear();
//for (int j=1; j<=n; j++)
//{
// cout<<vis[j]<<" ";
//}
//cout<<"\n";
//cout<<"First "<<i<<"\n";
for (int i=0; i<5; i++)
colpre[i] = 0;
dfs(i, -1);
cnt+= v.size()-1;
// for (int j=0; j<(int)(v.size()); j++){
// vis[v[j]] = false;
//cout<<v[j]<<" ";
// }
//cout<<"\n";
}
cout<<cnt;
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |