QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#709671 | #7684. Sweet Sugar | infCraft | WA | 132ms | 29720kb | C++17 | 2.2kb | 2024-11-04 16:08:32 | 2024-11-04 16:08:32 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define Yes cout << "Yes\n"
#define No cout << "No\n"
#define YES cout << "YES\n"
#define NO cout << "NO\n"
#define ff first
#define ss second
#define fori(x,y) for(int i=x;i<=(int)(y);++i)
#define forj(x,y) for(int j=x;j<=(int)(y);++j)
#define fork(x,y) for(int k=x;k<=(int)(y);++k)
#define debug(x) cerr << #x << " = " << x << endl
#define int long long
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll MOD = 998244353;
ll qpow(ll a,ll p) {ll res=1; while(p) {if (p&1) {res=res*a%MOD;} a=a*a%MOD; p>>=1;} return res;}
const int N = 1e6+7;
const int INF = 1e9;
int n,k;
vector<int> g[N];
int arr[N];
int ans = 0;
int dp[N][2];
void dfs(int u, int fa) {
// dp[u][0] = arr[u] == 2 ? 2 : 0;
// dp[u][1] = arr[u] == 1 ? 1 : -INF;
int tmp = 0; // 记录最大和
multiset<int> sets;
for (auto v: g[u]) {
if (v == fa) continue;
dfs(v, u);
tmp += max(dp[v][0], dp[v][1]);
sets.insert(max(dp[v][0], dp[v][1])-min(dp[v][0], dp[v][1]));
}
int tmp1 = -INF, tmp0 = 0;
// debug(tmp);
if (tmp) {
if (tmp%2) {
tmp1 = tmp;
tmp0 = tmp-*sets.begin();
}
else {
tmp1 = tmp-*sets.begin();
tmp0 = tmp;
}
}
if (arr[u]%2) {
dp[u][0] = tmp1+arr[u];
dp[u][1] = tmp0+arr[u];
}
else {
dp[u][0] = tmp0+arr[u];
dp[u][1] = tmp1+arr[u];
}
if (k%2&&dp[u][1]>=k || k%2 == 0&&dp[u][0] >= k) {
dp[u][1] = -INF;
dp[u][0] = 0;
ans++;
}
// debug(u);
// debug(dp[u][1]);
// debug(dp[u][0]);
}
void solve() {
cin >> n >> k;
ans = 0;
fori(1, n) {
cin >> arr[i];
g[i].clear();
}
fori(1, n-1) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 0);
cout << ans << endl;
}
signed main() {
IOS;
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 29720kb
input:
4 7 5 1 2 1 2 2 1 2 1 2 2 3 3 4 3 5 5 6 5 7 2 2 1 0 1 2 1 1 1 1 2 1
output:
2 0 1 0
result:
ok 4 number(s): "2 0 1 0"
Test #2:
score: 0
Accepted
time: 3ms
memory: 27844kb
input:
12 1 1 0 1 1 1 1 1 2 1 2 0 1 2 1 1 2 2 1 3 0 1 3 1 1 3 2 1 2000000 0 1 2000000 1 1 2000000 2
output:
0 1 0 0 0 1 0 0 0 0 0 0
result:
ok 12 numbers
Test #3:
score: -100
Wrong Answer
time: 132ms
memory: 27268kb
input:
200000 5 2 1 1 0 0 1 2 4 5 2 4 1 3 2 5 1 0 0 0 0 0 5 1 1 2 3 2 5 4 5 3 1 0 0 0 1 1 4 4 2 3 4 5 2 5 9 1 0 0 0 2 4 3 2 1 3 1 5 1 5 3 0 1 1 0 1 5 4 2 1 4 3 5 1 5 1 0 2 1 1 1 5 3 2 4 3 4 1 4 5 1 1 0 1 1 0 1 5 4 2 1 3 5 2 5 7 0 2 1 1 2 5 1 2 3 2 5 5 4 5 5 0 1 0 1 0 2 4 4 3 5 2 1 5 5 1 0 0 1 0 1 4 1 4 5 2...
output:
1 0 0 0 1 3 3 0 0 2 0 0 2 1 0 0 1 1 0 1 0 1 0 2 1 0 0 0 0 0 1 2 0 0 2 2 0 1 0 0 0 0 1 3 0 0 1 1 2 1 2 0 4 0 0 1 0 1 0 0 1 5 0 1 1 1 0 1 0 1 1 1 1 0 1 1 1 0 3 1 0 1 0 0 2 0 0 0 1 1 0 0 1 0 2 0 5 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 1 0 0 1 1 0 1 0 1 1 1 2 0 1 2 0 0 2 0 0 0 0 0 0 0 0 ...
result:
wrong answer 20th numbers differ - expected: '2', found: '1'