#include<bits/stdc++.h>
using namespace std;
#define sz(v) (int)v.size()
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define dbg(v) "[" #v " = " << (v) << "]"
#define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define rep(i, l, r) for(int i = (l); i < (r); ++i)
using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using ld = long double;
template<typename T>
bool minimize(T& a, const T& b){
if(a > b){
return a = b, true;
} return false;
}
template<typename T>
bool maximize(T& a, const T& b){
if(a < b){
return a = b, true;
} return false;
}
template<int dimension, typename T>
struct tensor : public vector<tensor<dimension - 1, T>> {
static_assert(dimension > 0, "Dimension must be positive !\n");
template<typename... Args>
tensor(int n = 0, Args... args) : vector<tensor<dimension - 1, T>> (n, tensor<dimension - 1, T>(args...)) {}
};
template<typename T>
struct tensor<1, T> : public vector<T> {
tensor(int n = 0, T val = T()) : vector<T>(n, val) {}
};
const int MAX = 1e5 + 5;
int n, q;
vector<int> adj[MAX];
namespace subtask1{
bool check(){
return sz(adj[1]) == n - 1 && q == 1;
}
int cnt[MAX];
void solve(){
int D;
cin >> D;
for(int i = 1; i <= D; ++i){
int u;
cin >> u;
++cnt[u];
}
int cost = 0;
int leaves = 0;
for(int i = 2; i <= n; ++i){
if(cnt[i] > 0){
leaves += cnt[i];
cost += 2 * (cnt[i] / 2);
cnt[i] &= 1;
if(cnt[i] > 0) cost += 2;
} else{
cost += 1;
leaves += 1;
}
}
if(leaves & 1) cout << -1 << '\n';
else cout << cost << '\n';
}
}
namespace subtask2{
bool check(){
if(q != 1) return false;
for(int i = 1; i <= n; ++i){
for(auto j : adj[i]){
if(abs(i - j) > 1){
return false;
}
}
}
return true;
}
int cnt[MAX];
void solve(){
int D;
cin >> D;
for(int i = 0; i < D; ++i){
int u;
cin >> u;
++cnt[u];
}
//greedy
if(D & 1){
cout << -1 << '\n';
return;
}
int cost = n - 1; //(1, n)
int last = -1;
for(int i = 2; i < n; ++i){
cost += 2 * (cnt[i] / 2);
cnt[i] &= 1;
if(cnt[i] == 1){
if(last != -1) cost += abs(last - i) + 2, last = -1;
else last = i;
}
}
assert(last == -1);
cout << cost << '\n';
}
}
namespace subtask3{
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
if(fopen("task.inp", "r")){
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
}
cin >> n >> q;
for(int i = 1; i < n; ++i){
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
if(subtask1::check()){
return subtask1::solve(), 0;
}
if(subtask2::check()){
return subtask2::solve(), 0;
}
return 0;
}
Compilation message
cleaning.cpp: In function 'int main()':
cleaning.cpp:142:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
142 | freopen("task.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cleaning.cpp:143:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
143 | freopen("task.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
3160 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3160 KB |
Output is correct |
2 |
Correct |
6 ms |
3352 KB |
Output is correct |
3 |
Correct |
22 ms |
7260 KB |
Output is correct |
4 |
Correct |
25 ms |
7516 KB |
Output is correct |
5 |
Correct |
18 ms |
7000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
3672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
5724 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
7260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |