CENS20F - Editorial

It passed. My Submission

Can somebody help me out finding what is wrong with this solution. Thanks in advance!
https://www.codechef.com/viewsolution/37003547

Can anybody explain me this…It is written in the editorial that “if visited[vertex][1] == 1, all the nodes at odd distance from the current vertex are zero. So there is no need to go further.” but, aren’t we interested in the nodes at even distance from the vertex so why no need to go further?

My Sol with just one simple DFS to do the work :):slight_smile: :grinning:

CODE LINK

`//
// author : Abhishek
//
#include <bits/stdc++.h>
#include

#define ll long long
#define db double
#define deb(x) cout<<#x<<“: “<<x<<”\n”
#define all(x) x.begin(),x.end()
#define fo(i, n) for(i=0 ; i<n ; i++)
#define Fo(i, k, n) for(i=k ; i<n ; i++)
#define setBits(x) __builtin_popcountll(x)
#define parity(x) __builtin_parity(x)

using namespace std;
using namespace chrono;

const ll MOD = 1000000007;
const ll N = 300005; // array size
const ll inf = 10000000000000000;
const db PI = 3.14159265358979323846264338327950288419716939937510;

void print(vector v) {
ll i;
fo(i, v.size())cout << v[i] << " ";
cout << “\n”;
}

void print(vector<pair<ll, ll> > v) {
ll i;
fo(i, v.size()) {
cout << v[i].first << " " << v[i].second << “\n”;
}
}

vector read(ll n) {
ll i;
vector arr(n, 0);
fo(i, n)cin >> arr[i];
return arr;
}

vector read(ll n, ll k) {
ll i;
vector arr(n + k, 0);
Fo(i, k, n + k)cin >> arr[i];
return arr;
}

bool isEven(ll n) {
return n % 2 == 0;
}

bool isOdd(ll n) {
return n % 2 != 0;
}

vector adj[N];
vector vis(N, 0);
vector taken(N, 0);
vector ans(N, 0);
vector depth(N, 0);

pair<ll, ll> dfs(ll cur, bool takenEv, bool takenOd) {

vis[cur] = true;

pair<ll, ll> forThis = {0, 0};

if (isEven(depth[cur])) {
    forThis.first += ans[cur];
} else {
    forThis.second += ans[cur];
}

for (auto next: adj[cur]) {
    if (!vis[next]) {
        depth[next] = 1 + depth[cur];
        pair<ll, ll> ct;
        if (taken[cur]) {
            if (isEven(depth[cur])) {
                ct = dfs(next, true, takenOd);
            } else {
                ct = dfs(next, takenEv, true);
            }
        } else {
            ct = dfs(next, takenEv, takenOd);
        }
        forThis.first += ct.first;
        forThis.second += ct.second;
    }
}

if (takenEv && isEven(depth[cur])) {
    ans[cur] = 0;
} else if (takenOd && isOdd(depth[cur])) {
    ans[cur] = 0;
} else if (taken[cur]) {
    ans[cur] = (isEven(depth[cur]) ? forThis.first : forThis.second);
}

return forThis;

}

void solve() {
ll t = 1, cs = 1;
cin >> (t);
while (t–) {

    ll i, j, k, n, m, p, q, x, y, a, b, l, r;
    cin >> n >> q;
    Fo(i, 1, n + 1) {
        adj[i].clear();
        taken[i] = 0;
        ans[i] = 0;
        vis[i] = false;
    }

    Fo(i, 1, n + 1) {
        cin >> ans[i];
    }

    fo(i, n - 1) {
        cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

    while (q--) {
        cin >> x;
        taken[x] = 1;
    }

    dfs(1, 0, 0);

// Fo(i, 1, n + 1) {
// cout << depth[i] << " ";
// }
// cout << “\n”;

    Fo(i, 1, n + 1) {
        cout << ans[i] << " ";
    }
    cout << "\n";

}

}

int main() {

ios_base::sync_with_stdio(false);
cin.tie(NULL);

#ifndef ONLINE_JUDGE
freopen(“input.txt”, “r”, stdin);
freopen(“output.txt”, “w”, stdout);
freopen(“error.txt”, “w”, stderr);
#endif

auto start = high_resolution_clock::now();

solve();

ll i, j, k, n, m, p, q, x, y, a, b, l, r;


auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);

#ifndef ONLINE_JUDGE
cerr << "Time: " << duration.count() / 1000.0 << endl;
#endif

return 0;

}

`

bro! anyone there who can help me . i have written code using euler tour but still showing tle. and i have checked it for many test cases and even given in the comments too.
here is my ideone link

Hello community
Mine approach is also similar, however I am still getting Wrong Answer. Can anyone please help me figure out my mistake.

My solution : CodeChef: Practical coding for everyone

Thank You :pray:

solution link

I have done the same thing but i am getting TLE. Please someone help me

I am getting WA for the below solution … i dont know whats wrong. please help!
https://www.codechef.com/viewsolution/37019870

Can someone explain how a star-shaped tree would result in a TLE?

Can anyone please tell me where my code fails… I cannot find it. I’m getting TLE on submission
https://www.codechef.com/viewsolution/37027990

https://www.codechef.com/viewsolution/37029733
My solution is almost same but I keep getting the please help

for each query,i made a dfs and added even distnce of subtree values to vertex.
As it is mentioned sum of all N and Q over all test cases doen’t exceed 200000 ,its no where near to get a TLE,but i did get TLE :grimacing:,can anyone look into my code and say what’s wrong?
https://www.codechef.com/viewsolution/37033454

Not Accepted, WA… Please someone provide any test case which it cannot pass

@aert Do you have more test cases? It worked for all you’ve given above

1
3 1
1 1 1
3 2
2 1
1
You are creating a kind of directed tree so it is unable to go further from node 1.
Your output - 1 1 1
Correct output - 2 1 0

1 Like

https://www.codechef.com/viewsolution/37040905
Thanks man, Really confused by the question statement bc just started trees.

Why we simply can’t use BFS in directed graph and maintain distance array and update the A[i] values at the even distanced node ?? What’s wrong in this approach can anyone help me i am new to the graph and not able to figure out the mistake ,…

why my solution is giving WA i hv used bfs to calculate distances of all nodes then using dfs i calculated in and out time to check wether 2 nodes lie on same path or not then i run a loop checking all even distances nodes and same path nodes then shifting them
https://www.codechef.com/viewsolution/37056580

you can check my code I have also done this ques with BFS CodeChef: Practical coding for everyone

https://www.codechef.com/viewsolution/37119800
Could you tell me on which test case is my solution failing?
I have tried both of the test cases, that you have commented in this thread, and in both my code outputs the correct answer. Also, the logic seems to be what I implemented. Could you please check @ae