/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 6ms 832.0 KiB
#2 Wrong Answer 6ms 836.0 KiB
#3 Wrong Answer 8ms 876.0 KiB

Code

#include<bits/stdc++.h>          // --> Bismillahir_ Rahmanir_ Rahim <--
#define ll long long 
using namespace std;

const ll N = 3e5;
vector<ll>g[N];
ll n,e;
ll a[N];

bool vis[N];
ll z = 0,ans = 0;
void dfs(ll u) {
	vis[u] = true;
	if(a[u]==1) {
		ans += z;
		z = 0;
	}
	for(auto &v : g[u]) {
		if(!vis[v]) {
			++z;
		
			dfs(v);
			
		}
	}
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
	
	ll tc; cin >> tc;
	while(tc--) {
		cin >> n;
		e = n-1;
		ll st = -1;
		for(ll i=1 ; i<=n ; i++) {
			cin >> a[i];
			if(a[i]==1) st = i;
		}
		
		while(e--) {
			ll x,y; cin >> x >> y;
			g[x].push_back(y);
			g[y].push_back(x);
		}
		if(st==-1) {
			cout << 0 << "\n";
		}
		else {
			//cout << st<< endl;
		    z = 0;
			ans = 0;
			dfs(st);
			cout << ans << "\n";
		}
		for(ll i=0 ; i<N ; i++) {
			g[i].clear();
			vis[i] = 0;
		}
	}
   
    return 0;
} 

Information

Submit By
Type
Submission
Problem
P1078 Apple on Tree
Contest
Bangladesh 2.0
Language
C++20 (G++ 13.2.0)
Submit At
2024-08-16 17:11:32
Judged At
2024-11-11 03:12:22
Judged By
Score
1
Total Time
8ms
Peak Memory
876.0 KiB