# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1097379 |
2024-10-07T05:12:53 Z |
Alihan_8 |
Mergers (JOI19_mergers) |
C++17 |
|
3000 ms |
30928 KB |
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ar array
struct Dsu{
vector <int> fa;
Dsu(int n){
fa.resize(n);
iota(fa.begin(), fa.end(), 0);
}
int get(int x){ return x ^ fa[x] ? fa[x] = get(fa[x]) : x; }
bool merge(int u, int v){
u = get(u), v = get(v);
if ( u == v ) return false;
fa[v] = u;
return true;
}
};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, k; cin >> n >> k;
vector <vector<int>> adj(n);
for ( int i = 0; i + 1 < n; i++ ){
int u, v; cin >> u >> v;
--u, --v;
adj[u].pb(v);
adj[v].pb(u);
}
vector <int> s(n);
for ( auto &x: s ){
cin >> x; --x;
}
auto check = [&](vector <int> t){
vector <vector<int>> cnt(n, vector <int> (k));
auto dfs = [&](auto dfs, int u, int p) -> void{
cnt[u][t[u]] = 1;
for ( auto &v: adj[u] ){
if ( v != p ){
dfs(dfs, v, u);
for ( int i = 0; i < k; i++ ){
cnt[u][i] += cnt[v][i];
}
}
}
};
dfs(dfs, 0, -1);
int bad = 0;
for ( int u = 0; u < n; u++ ){
bool flag = true;
for ( int t = 0; t < k; t++ ){
if ( cnt[u][t] > 0 && cnt[u][t] < cnt[0][t] ){
flag = false;
}
}
bad += flag;
}
return bad == 1;
};
vector <ar<int,2>> pa;
for ( int i = 0; i < k; i++ ){
for ( int j = i + 1; j < k; j++ ) pa.pb({i, j});
}
int opt = n, m = pa.size();
auto dfs = [&](auto dfs, int p, int cnt, Dsu ds) -> void{
if ( p == m ){
vector <int> t(n);
for ( int i = 0; i < n; i++ ) t[i] = ds.get(s[i]);
if ( check(t) ){
opt = min(opt, cnt);
}
return;
}
dfs(dfs, p + 1, cnt, ds);
if ( ds.merge(pa[p][0], pa[p][1]) ){
dfs(dfs, p + 1, cnt + 1, ds);
}
};
Dsu ds(k);
dfs(dfs, 0, 0, ds);
cout << opt;
cout << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
348 KB |
Output is correct |
2 |
Correct |
15 ms |
348 KB |
Output is correct |
3 |
Correct |
15 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
191 ms |
448 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
15 ms |
348 KB |
Output is correct |
12 |
Correct |
170 ms |
444 KB |
Output is correct |
13 |
Correct |
21 ms |
344 KB |
Output is correct |
14 |
Correct |
174 ms |
348 KB |
Output is correct |
15 |
Correct |
15 ms |
348 KB |
Output is correct |
16 |
Correct |
17 ms |
464 KB |
Output is correct |
17 |
Correct |
18 ms |
348 KB |
Output is correct |
18 |
Correct |
191 ms |
348 KB |
Output is correct |
19 |
Correct |
17 ms |
348 KB |
Output is correct |
20 |
Correct |
17 ms |
348 KB |
Output is correct |
21 |
Correct |
200 ms |
452 KB |
Output is correct |
22 |
Correct |
192 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
29 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
348 KB |
Output is correct |
2 |
Correct |
15 ms |
348 KB |
Output is correct |
3 |
Correct |
15 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
191 ms |
448 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
15 ms |
348 KB |
Output is correct |
12 |
Correct |
170 ms |
444 KB |
Output is correct |
13 |
Correct |
21 ms |
344 KB |
Output is correct |
14 |
Correct |
174 ms |
348 KB |
Output is correct |
15 |
Correct |
15 ms |
348 KB |
Output is correct |
16 |
Correct |
17 ms |
464 KB |
Output is correct |
17 |
Correct |
18 ms |
348 KB |
Output is correct |
18 |
Correct |
191 ms |
348 KB |
Output is correct |
19 |
Correct |
17 ms |
348 KB |
Output is correct |
20 |
Correct |
17 ms |
348 KB |
Output is correct |
21 |
Correct |
200 ms |
452 KB |
Output is correct |
22 |
Correct |
192 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
29 ms |
348 KB |
Output is correct |
25 |
Execution timed out |
3054 ms |
1112 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
348 KB |
Output is correct |
2 |
Correct |
15 ms |
348 KB |
Output is correct |
3 |
Correct |
15 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
191 ms |
448 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
15 ms |
348 KB |
Output is correct |
12 |
Correct |
170 ms |
444 KB |
Output is correct |
13 |
Correct |
21 ms |
344 KB |
Output is correct |
14 |
Correct |
174 ms |
348 KB |
Output is correct |
15 |
Correct |
15 ms |
348 KB |
Output is correct |
16 |
Correct |
17 ms |
464 KB |
Output is correct |
17 |
Correct |
18 ms |
348 KB |
Output is correct |
18 |
Correct |
191 ms |
348 KB |
Output is correct |
19 |
Correct |
17 ms |
348 KB |
Output is correct |
20 |
Correct |
17 ms |
348 KB |
Output is correct |
21 |
Correct |
200 ms |
452 KB |
Output is correct |
22 |
Correct |
192 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
29 ms |
348 KB |
Output is correct |
25 |
Execution timed out |
3053 ms |
1116 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3027 ms |
30928 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
348 KB |
Output is correct |
2 |
Correct |
15 ms |
348 KB |
Output is correct |
3 |
Correct |
15 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
191 ms |
448 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
15 ms |
348 KB |
Output is correct |
12 |
Correct |
170 ms |
444 KB |
Output is correct |
13 |
Correct |
21 ms |
344 KB |
Output is correct |
14 |
Correct |
174 ms |
348 KB |
Output is correct |
15 |
Correct |
15 ms |
348 KB |
Output is correct |
16 |
Correct |
17 ms |
464 KB |
Output is correct |
17 |
Correct |
18 ms |
348 KB |
Output is correct |
18 |
Correct |
191 ms |
348 KB |
Output is correct |
19 |
Correct |
17 ms |
348 KB |
Output is correct |
20 |
Correct |
17 ms |
348 KB |
Output is correct |
21 |
Correct |
200 ms |
452 KB |
Output is correct |
22 |
Correct |
192 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
29 ms |
348 KB |
Output is correct |
25 |
Execution timed out |
3054 ms |
1112 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |