/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 6ms 788.0 KiB
#2 Wrong Answer 6ms 832.0 KiB
#3 Wrong Answer 8ms 832.0 KiB
#4 Wrong Answer 8ms 948.0 KiB
#5 Wrong Answer 25ms 2.816 MiB
#6 Wrong Answer 41ms 3.109 MiB
#7 Wrong Answer 32ms 2.969 MiB
#8 Wrong Answer 251ms 900.0 KiB
#9 Wrong Answer 75ms 1.168 MiB
#10 Wrong Answer 50ms 2.918 MiB
#11 Wrong Answer 37ms 3.344 MiB
#12 Wrong Answer 45ms 2.887 MiB
#13 Wrong Answer 25ms 2.82 MiB
#14 Wrong Answer 54ms 3.316 MiB
#15 Wrong Answer 41ms 1.328 MiB
#16 Wrong Answer 18ms 2.766 MiB
#17 Wrong Answer 17ms 2.812 MiB
#18 Wrong Answer 17ms 2.691 MiB
#19 Wrong Answer 66ms 14.512 MiB
#20 Wrong Answer 68ms 14.582 MiB
#21 Wrong Answer 67ms 14.598 MiB
#22 Accepted 79ms 18.969 MiB
#23 Wrong Answer 125ms 14.168 MiB
#24 Wrong Answer 112ms 14.172 MiB
#25 Wrong Answer 76ms 14.41 MiB
#26 Wrong Answer 9ms 1.66 MiB

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-10-03 13:23:38
Judged By
Score
7
Total Time
251ms
Peak Memory
18.969 MiB