Submission #477532

# Submission time Handle Problem Language Result Execution time Memory
477532 2021-10-02T12:38:15 Z nicolaalexandra Cats or Dogs (JOI18_catdog) C++14
38 / 100
3000 ms 7972 KB
#include <bits/stdc++.h>
#include "catdog.h"
#define DIM 100010
using namespace std;

vector <int> L[DIM],a,b;
int v[DIM],dp[DIM][3];
int n,i,q,tip,x;


void initialize(int _n, vector<int> A, vector<int> B) {
    n = _n;
    for (int i=0;i<n-1;i++){
        L[A[i]].push_back(B[i]);
        L[B[i]].push_back(A[i]);
    }
}

void dfs (int nod, int tata){

    int ok = 0;
    for (auto vecin : L[nod])
        if (vecin != tata){
            ok = 1;
            dfs (vecin,nod);
        }

    if (!ok){

        if (v[nod] == 1)
            dp[nod][0] = dp[nod][2] = 1;
        if (v[nod] == 2)
            dp[nod][0] = dp[nod][1] = 1;

    } else {


        if (!v[nod]){ /// nu am nimic in nod

            for (auto vecin : L[nod]){
                if (vecin == tata)
                    continue;

                dp[nod][0] += min(dp[vecin][0],min(dp[vecin][1]+1,dp[vecin][2]+1));

                dp[nod][1] += min (dp[vecin][1],min(dp[vecin][0],dp[vecin][2]+1));

                dp[nod][2] += min (dp[vecin][2],min(dp[vecin][0],dp[vecin][1]+1));

            }

        } else {
            if (v[nod] == 1){ /// cat

                dp[nod][0] = dp[nod][2] = 1;
                for (auto vecin : L[nod]){
                    if (vecin == tata)
                        continue;

                    int val = min (dp[vecin][0],min(dp[vecin][1],dp[vecin][2]+1));

                    dp[nod][0] += val;
                    dp[nod][1] += val;
                    dp[nod][2] += val;

                }


            } else { /// dog

                dp[nod][0] = dp[nod][1] = 1;
                for (auto vecin : L[nod]){
                    if (vecin == tata)
                        continue;

                    int val = min (dp[vecin][0],min(dp[vecin][2],dp[vecin][1]+1));

                    dp[nod][0] += val;
                    dp[nod][1] += val;
                    dp[nod][2] += val;
                }

            }

        }

    }

}


int cat(int x) {
    v[x] = 1;

    memset (dp,0,sizeof dp);


    dfs (1,0);

    return min (dp[1][0],min(dp[1][1],dp[1][2]));
}

int dog(int x) {
    v[x] = 2;

    memset (dp,0,sizeof dp);

    dfs (1,0);

    return min (dp[1][0],min(dp[1][1],dp[1][2]));
}

int neighbor(int x) {
    v[x] = 0;

    memset (dp,0,sizeof dp);

    dfs (1,0);

    return min (dp[1][0],min(dp[1][1],dp[1][2]));
}

/*
int main (){

    ifstream cin ("date.in");
    ofstream cout ("date.out");


    cin>>n;
    for (i=0;i<n-1;i++){
        int x,y;
        cin>>x>>y;
        a.push_back(x);
        b.push_back(y);
        //cin>>a[i]>>b[i];
    }

    initialize(n,a,b);

    cin>>q;
    for (;q--;){
        cin>>tip>>x;
        if (tip == 1)
            cout<<cat(x)<<"\n";
        else {
            if (tip == 2)
                cout<<dog(x)<<"\n";
            else cout<<neighbor(x)<<"\n";
        }
    }



    return 0;
}
*/
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3792 KB Output is correct
2 Correct 3 ms 3792 KB Output is correct
3 Correct 4 ms 3792 KB Output is correct
4 Correct 5 ms 3792 KB Output is correct
5 Correct 5 ms 3792 KB Output is correct
6 Correct 6 ms 3792 KB Output is correct
7 Correct 6 ms 3792 KB Output is correct
8 Correct 6 ms 3792 KB Output is correct
9 Correct 5 ms 3792 KB Output is correct
10 Correct 6 ms 3828 KB Output is correct
11 Correct 4 ms 3792 KB Output is correct
12 Correct 2 ms 3792 KB Output is correct
13 Correct 4 ms 3804 KB Output is correct
14 Correct 4 ms 3792 KB Output is correct
15 Correct 4 ms 3792 KB Output is correct
16 Correct 6 ms 3792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3792 KB Output is correct
2 Correct 3 ms 3792 KB Output is correct
3 Correct 4 ms 3792 KB Output is correct
4 Correct 5 ms 3792 KB Output is correct
5 Correct 5 ms 3792 KB Output is correct
6 Correct 6 ms 3792 KB Output is correct
7 Correct 6 ms 3792 KB Output is correct
8 Correct 6 ms 3792 KB Output is correct
9 Correct 5 ms 3792 KB Output is correct
10 Correct 6 ms 3828 KB Output is correct
11 Correct 4 ms 3792 KB Output is correct
12 Correct 2 ms 3792 KB Output is correct
13 Correct 4 ms 3804 KB Output is correct
14 Correct 4 ms 3792 KB Output is correct
15 Correct 4 ms 3792 KB Output is correct
16 Correct 6 ms 3792 KB Output is correct
17 Correct 42 ms 3812 KB Output is correct
18 Correct 45 ms 3792 KB Output is correct
19 Correct 29 ms 3876 KB Output is correct
20 Correct 3 ms 3792 KB Output is correct
21 Correct 18 ms 3844 KB Output is correct
22 Correct 22 ms 3848 KB Output is correct
23 Correct 64 ms 3860 KB Output is correct
24 Correct 51 ms 3792 KB Output is correct
25 Correct 37 ms 3792 KB Output is correct
26 Correct 16 ms 3856 KB Output is correct
27 Correct 13 ms 3844 KB Output is correct
28 Correct 12 ms 3904 KB Output is correct
29 Correct 60 ms 3920 KB Output is correct
30 Correct 21 ms 3792 KB Output is correct
31 Correct 7 ms 3792 KB Output is correct
32 Correct 40 ms 3792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3792 KB Output is correct
2 Correct 3 ms 3792 KB Output is correct
3 Correct 4 ms 3792 KB Output is correct
4 Correct 5 ms 3792 KB Output is correct
5 Correct 5 ms 3792 KB Output is correct
6 Correct 6 ms 3792 KB Output is correct
7 Correct 6 ms 3792 KB Output is correct
8 Correct 6 ms 3792 KB Output is correct
9 Correct 5 ms 3792 KB Output is correct
10 Correct 6 ms 3828 KB Output is correct
11 Correct 4 ms 3792 KB Output is correct
12 Correct 2 ms 3792 KB Output is correct
13 Correct 4 ms 3804 KB Output is correct
14 Correct 4 ms 3792 KB Output is correct
15 Correct 4 ms 3792 KB Output is correct
16 Correct 6 ms 3792 KB Output is correct
17 Correct 42 ms 3812 KB Output is correct
18 Correct 45 ms 3792 KB Output is correct
19 Correct 29 ms 3876 KB Output is correct
20 Correct 3 ms 3792 KB Output is correct
21 Correct 18 ms 3844 KB Output is correct
22 Correct 22 ms 3848 KB Output is correct
23 Correct 64 ms 3860 KB Output is correct
24 Correct 51 ms 3792 KB Output is correct
25 Correct 37 ms 3792 KB Output is correct
26 Correct 16 ms 3856 KB Output is correct
27 Correct 13 ms 3844 KB Output is correct
28 Correct 12 ms 3904 KB Output is correct
29 Correct 60 ms 3920 KB Output is correct
30 Correct 21 ms 3792 KB Output is correct
31 Correct 7 ms 3792 KB Output is correct
32 Correct 40 ms 3792 KB Output is correct
33 Execution timed out 3078 ms 7972 KB Time limit exceeded
34 Halted 0 ms 0 KB -