/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 5ms 588.0 KiB
#2 Wrong Answer 5ms 540.0 KiB
#3 Wrong Answer 7ms 720.0 KiB

Code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 5;
vector<int>adj[N];
int cnt = 0;
vector<bool>vis(N, false);
void dfs(int node, vector<int>&apple, int total){
	vis[node] = true;
	for(auto child : adj[node]){
		if(!vis[child]){
			if(apple[child] == 1){
				total--;
				cnt++;
				//cout << child <<" " << total <<  '\n';
				if(total == 0)break;
				dfs(child, apple, total);
			}
			else {
				cnt++;
				//cout << child << '\n';
				dfs(child, apple, total);
			}
			if(total == 0){
				return;
			}
		}
	}
}
void solve(){
	for(int i = 0; i < N; i++){
		vis[i] = false;
		adj[i].clear();
	}
	cnt = 0;
	int n;
	cin >> n;
	vector<int>apple(n);
	int start = 0, total = 0;
	for(int i = 0; i < n; i++){
		cin >> apple[i];
		if(apple[i] == 1){
			total++;
		}
	}
	--total;
	if(total == 0){
		cout << 0 << '\n';
		return;
	}
	for(int i = 0; i < n; i++){
		if(apple[i] == 1){
			start = i + 1;
			break;
		}
	}
	//cout << start << '\n';
	for(int i = 0; i < n - 1; i++){
		int u, v;
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs(start, apple, total);
	cout << cnt << '\n';

}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	cin >> t;
	while(t--){
		solve();
	}
	return 0;
}

Information

Submit By
Type
Submission
Problem
P1078 Apple on Tree
Language
C++20 (G++ 13.2.0)
Submit At
2024-10-03 13:27:12
Judged At
2024-11-11 02:51:30
Judged By
Score
1
Total Time
7ms
Peak Memory
720.0 KiB