/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 6ms 876.0 KiB
#2 Accepted 6ms 876.0 KiB
#3 Wrong Answer 8ms 840.0 KiB
#4 Wrong Answer 8ms 956.0 KiB
#5 Wrong Answer 25ms 2.848 MiB
#6 Wrong Answer 41ms 3.027 MiB
#7 Wrong Answer 31ms 3.004 MiB
#8 Wrong Answer 241ms 900.0 KiB
#9 Wrong Answer 75ms 1.172 MiB
#10 Wrong Answer 50ms 3.152 MiB
#11 Wrong Answer 37ms 3.32 MiB
#12 Wrong Answer 41ms 3.027 MiB
#13 Wrong Answer 20ms 2.969 MiB
#14 Wrong Answer 54ms 3.125 MiB
#15 Wrong Answer 41ms 1.277 MiB
#16 Wrong Answer 18ms 2.766 MiB
#17 Wrong Answer 17ms 2.84 MiB
#18 Wrong Answer 17ms 2.688 MiB
#19 Wrong Answer 66ms 14.445 MiB
#20 Wrong Answer 65ms 14.391 MiB
#21 Wrong Answer 63ms 14.77 MiB
#22 Accepted 72ms 15.91 MiB
#23 Wrong Answer 100ms 14.125 MiB
#24 Wrong Answer 101ms 14.27 MiB
#25 Wrong Answer 75ms 14.598 MiB
#26 Wrong Answer 9ms 1.586 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, en = -1;
		for(ll i=1 ; i<=n ; i++) {
			cin >> a[i];
			if(a[i]==1) en = i;
			if(a[i]==1 && st==-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 {
			ll strt = (st+en)/2;
		    z = 0;
			ans = 0;
			dfs(strt);
			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:18:57
Judged At
2024-10-03 13:22:37
Judged By
Score
10
Total Time
241ms
Peak Memory
15.91 MiB