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 <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, K;
cin >> N >> K;
vector<pair<int, int>> state_edges;
vector<int> A(N - 1), B(N - 1);
for (int i = 0; i < N - 1; ++i) {
cin >> A[i] >> B[i];
--A[i]; // Convert to 0-based index
--B[i];
}
vector<int> S(N);
for (int i = 0; i < N; ++i) {
cin >> S[i];
--S[i]; // Convert to 0-based index
}
// Collect edges between different states
for (int i = 0; i < N - 1; ++i) {
int u = A[i];
int v = B[i];
int su = S[u];
int sv = S[v];
if (su != sv) {
int s = min(su, sv);
int t = max(su, sv);
state_edges.emplace_back(s, t);
}
}
// Sort and remove duplicates
sort(state_edges.begin(), state_edges.end());
state_edges.erase(unique(state_edges.begin(), state_edges.end()), state_edges.end());
int E = state_edges.size(); // Number of edges in the state graph
int N_states = K; // Number of nodes in the state graph
int cycles_initial = E - N_states + 1;
int answer = max(0, 1 - cycles_initial);
cout << answer << '\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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |