/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 5ms 5.02 MiB
#2 Accepted 5ms 5.02 MiB
#3 Wrong Answer 6ms 5.246 MiB
#4 Wrong Answer 12ms 5.027 MiB
#5 Wrong Answer 41ms 6.02 MiB
#6 Wrong Answer 40ms 6.27 MiB
#7 Wrong Answer 30ms 6.141 MiB
#8 Wrong Answer 19ms 5.02 MiB
#9 Wrong Answer 39ms 5.215 MiB
#10 Wrong Answer 68ms 6.062 MiB
#11 Wrong Answer 34ms 6.27 MiB
#12 Wrong Answer 43ms 6.199 MiB
#13 Wrong Answer 19ms 6.27 MiB
#14 Wrong Answer 75ms 6.281 MiB
#15 Wrong Answer 29ms 5.336 MiB
#16 Wrong Answer 21ms 6.141 MiB
#17 Wrong Answer 31ms 6.238 MiB
#18 Wrong Answer 18ms 6.27 MiB
#19 Accepted 62ms 13.133 MiB
#20 Wrong Answer 60ms 13.203 MiB
#21 Wrong Answer 61ms 13.242 MiB
#22 Accepted 84ms 21.637 MiB
#23 Wrong Answer 131ms 12.77 MiB
#24 Wrong Answer 105ms 12.77 MiB
#25 Wrong Answer 98ms 12.52 MiB
#26 Wrong Answer 9ms 5.52 MiB

Code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define nl '\n'
const int N = 200000+9;
vector<int> graph[N] ;
bool vis[N] , marked[N] , vis1[N];
int aa[N] , ans , counter;
bool dsf(int i){
    vis[i] =1 ;
    bool flag = 0 ;
    for(auto it : graph[i]){
        if(!vis[it]){
            if(dsf(it) == 1){
                flag = 1 ;
            }
        }
    }
    if(aa[i] == 1 or flag == 1){
        marked[i] = 1 ;
        return 1 ;
    }
    else{
        return 0 ;
    }
    
}
void dfs2(int i){
    vis1[i] = 1 ;
    counter++;
    bool flag = 0 ;
    for(auto it : graph[i]){
        if((vis1[it] == 0) and (marked[it] == 1)){
            flag = 1 ;
            //cout<< it << " " << vis1[it] << " " << marked[it] << nl;
            dfs2(it);
        }
    }
    if(flag == 0){
        ans = counter ;
    }
    counter++;
}

void solve()
{
    
    int n ;cin>> n ;
    for(int i=1 ; i<=n ; i++){
        graph[i].clear();
        vis[i] = 0 ;
        vis1[i] = 0 ;
        marked[i] = 0 ;
    }
    ans = 0 , counter = 0 ;
    int first = -69 ;
    for(int i=1 ; i<=n ; i++){
        cin>> aa[i];
    }
    for(int i=1 ; i<=n ; i++){
        if(aa[i] == 1){
            first = i ;
            break;
        }
    }
    for(int i=1 ; i<=n-1 ; i++){
        int u , v ; cin>> u >> v ;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    if(first == -69){
        cout<< 0 << nl;
        return;
    }
    //cout<< first << nl;
    if(dsf(first)){
        marked[first]= 1 ;
    }
    
    for(int i=1 ; i<=n ; i++){
        //cout<< marked[i] << " ";
        if(aa[i] == 1){
            first = i ;
            break;
        }
    }
    /*for(int i=1 ; i<=n ; i++){
        cout<< vis1[i] << " ";
    }
    cout<< nl;
    for(int i=1 ; i<=n ; i++){
        cout<< marked[i] << " ";
    }
    cout<< nl;*/
    
    dfs2(first);
    cout<< ans-1 << nl;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    int T ;
    cin >> T ;
    for (int tt = 1 ; tt <= T ; tt++)
    {
        solve() ;
    }
}

Information

Submit By
Type
Submission
Problem
P1078 Apple on Tree
Language
C++20 (G++ 13.2.0)
Submit At
2024-08-17 09:21:15
Judged At
2024-08-17 09:21:15
Judged By
Score
16
Total Time
131ms
Peak Memory
21.637 MiB