#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |