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 <iostream>
#include <vector>
#include <cstring>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
vector<int> Ai(N - 1), Bi(N - 1);
for (int i = 0; i < N - 1; ++i) {
cin >> Ai[i] >> Bi[i];
--Ai[i]; // Convert to 0-based indexing
--Bi[i];
}
vector<int> S(N);
for (int i = 0; i < N; ++i) {
cin >> S[i];
--S[i]; // Convert to 0-based indexing
}
// Adjacency matrix for the state graph
int adj[7][7]; // Since K <= 7
memset(adj, 0, sizeof(adj));
// Build the state graph
for (int i = 0; i < N - 1; ++i) {
int s1 = S[Ai[i]];
int s2 = S[Bi[i]];
if (s1 != s2) {
adj[s1][s2] = 1;
adj[s2][s1] = 1;
}
}
// Count unique edges between states
int E = 0;
for (int i = 0; i < K; ++i) {
for (int j = i + 1; j < K; ++j) {
if (adj[i][j]) {
++E;
}
}
}
int N_states = K;
int cycles = E - N_states + 1;
// Calculate the minimum number of mergers needed
int answer = max(0, 1 - cycles);
cout << answer << endl;
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |