#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 = i;
}
}
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:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
140 | freopen("task.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cleaning.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
141 | freopen("task.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
2932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
2908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
3416 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
4700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
23 ms |
5972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |