In the first snippet, you’re checking only the type of query inside if. But in the second snippet, you’re also checking another condition !popped[x - 1]. This will impact the flow of control. Consider a case where t == 1 is true but !popped[x - 1] is false. In this case, the control will not enter both if and else if. Instead, it will enter else which is not what we want. You should also make sure that the control enters else only when t = 3.
Was it guaranteed that no two u, v will be the same in input? no.
I did this problem by just using two unordered sets instead of a queue but I have used a map to ensure distinct u,v pair (gives tle). After removing that map solution gets ac.
Well, I missed the line where the graph was given as connected, but it didn’t make any real difference to my solution. I maintained a “front” of where the freezing had just happened - whether by spread or by initiation - and renewed it for each step of spread. Eventually the front becomes empty and I don’t need to do more updates. Of course already-frozen nodes do not get added to the front.
can please tell me why my code is giving wrong answer and TLE in some cases.
I have taken reference from youtube video solution.
CODE-
#include <bits/stdc++.h>
using namespace std;
queue<int> q; // contain index to be frozen on next turn
void frozen_func(vector<int> &frozen, vector<int> arr[])
{
int n = q.size();
while (n--)
{
int cur = q.front();
if (frozen[cur])
{
continue;
}
else
{
frozen[cur] = 1;
}
q.pop();
for (int it : arr[cur])
{
q.push(it);
}
}
}
int main()
{
// your code goes here
int n, m, k;
cin >> n >> m >> k;
vector<int> arr[n + 1];
vector<int> frozen(n + 1);
queue<int> qq; // contain parent index which has been frozen on query 1 and whose neighbour has to be given to the q
for (int i = 0; i < m; i++)
{
int a, b;
cin >> a >> b;
arr[a].push_back(b);
arr[b].push_back(a);
}
int maxTime = n - 1; // max for every node to get frozen
while (k--)
{
int query, i;
cin >> query >> i;
if (query == 1)
{
if(!frozen[i])
{frozen[i] = 1;
qq.push(i);}
}
else if (query == 2)
{
while (!qq.empty())
{
int cur = qq.front();
qq.pop();
for (int it : arr[cur])
{
q.push(it);
}
}
if(q.empty() || maxTime==0) continue;
while (i-- && maxTime != 0)
{
frozen_func(frozen, arr);
maxTime--;
}
}
else
{
if (frozen[i])
{
cout << "YES\n";
}
else
{
cout << "NO\n";
}
}
}
return 0;
}
Can you tell why is the Dijkstra based solution failing ? All that was done is , added. extra edges from source node(node which was frozen before all others) to other nodes which are getting froze at later timings , and edge weight = (time of source node - time of other node) , solution is failing same test-cases as yours : CodeChef: Practical coding for everyone
After that , I tried the simple bfs solution , which passed all the cases. Any idea why dijkstra solution fails ? @cherry0697@iimda
here I used similar to bfs approach and my freeze array element i.e. freeze[i] can take any of the three values : -1, 0 and 1 with their meanings as follows :
-1 this node is unfrozen, since none of its’s neighbor nodes are frozen
0 means this node is in queue, since it has at least one frozen neighbor and we haven’t pushed this node’s unfrozen neighbors into the queue
1 means this node is frozen as well as we have pushed all its unfrozen neighbors into queue
rest of the logic is pretty similar to video editorial I guess.
I see… I got it why DFS is wrong. I wasn’t thinking of cycles in graph. That’s why. It seems like I am too used to trees now. Thank you so much. It was really insightful for me learn why this was wrong.