#include <bits/stdc++.h>
#define rep(i, l, r) for(int i = l; i <= r; i++)
#define rep2(i, l, r) for(int i = l; i >= r; i--)
#define fi first
#define se second
#define bit(i, x) (x >> i & 1)
using namespace std;
const int N = 3e5 + 3;
const int mod = 1e9 + 7;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long rnd(long long l, long long r) {
return uniform_int_distribution<long long>(l, r)(rng);
}
int n, k, c[N];
vector<int> G[N];
pair<int, int> e[N];
namespace sub1{
int s[N], vst[N];
void dfs(int u, int mask) {
vst[u] = 1;
for(auto v : G[u]) if (!vst[v] && (mask & (1 << (c[v] - 1))) > 0) {
dfs(v, mask);
}
}
long long sol() {
int res = k;
rep(i, 1, n) s[c[i]] = i;
rep(mask, 1, (1 << k) - 1) {
int cnt = 0;
rep(i, 1, n) if (!vst[i] && (mask & (1 << (c[i] - 1))) > 0) {
cnt++;
dfs(i, mask);
}
if (cnt == 1) res = min(res, __builtin_popcount(mask) - 1);
rep(i, 1, n) vst[i] = 0;
}
return res;
}
}
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("testing.txt", "r", stdin);
// freopen("outputing.txt", "w", stdout);
cin >> n >> k;
rep(i, 1, n - 1) {
int u, v; cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
rep(i, 1, n) cin >> c[i];
cout << sub1::sol();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7516 KB |
Output is correct |
2 |
Correct |
4 ms |
7512 KB |
Output is correct |
3 |
Correct |
4 ms |
7516 KB |
Output is correct |
4 |
Correct |
4 ms |
7524 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
3 ms |
7516 KB |
Output is correct |
7 |
Correct |
5 ms |
7516 KB |
Output is correct |
8 |
Correct |
5 ms |
7516 KB |
Output is correct |
9 |
Correct |
5 ms |
7516 KB |
Output is correct |
10 |
Correct |
3 ms |
7516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7516 KB |
Output is correct |
2 |
Correct |
4 ms |
7512 KB |
Output is correct |
3 |
Correct |
4 ms |
7516 KB |
Output is correct |
4 |
Correct |
4 ms |
7524 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
3 ms |
7516 KB |
Output is correct |
7 |
Correct |
5 ms |
7516 KB |
Output is correct |
8 |
Correct |
5 ms |
7516 KB |
Output is correct |
9 |
Correct |
5 ms |
7516 KB |
Output is correct |
10 |
Correct |
3 ms |
7516 KB |
Output is correct |
11 |
Execution timed out |
3009 ms |
7516 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
67 ms |
18576 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7516 KB |
Output is correct |
2 |
Correct |
4 ms |
7512 KB |
Output is correct |
3 |
Correct |
4 ms |
7516 KB |
Output is correct |
4 |
Correct |
4 ms |
7524 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
3 ms |
7516 KB |
Output is correct |
7 |
Correct |
5 ms |
7516 KB |
Output is correct |
8 |
Correct |
5 ms |
7516 KB |
Output is correct |
9 |
Correct |
5 ms |
7516 KB |
Output is correct |
10 |
Correct |
3 ms |
7516 KB |
Output is correct |
11 |
Execution timed out |
3009 ms |
7516 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |