I am working on a simple DFS problem where I have to return the number of respective child nodes having more child nodes as compared to their parents (Child nodes>Parent nodes:Count++).
I have been facing the problem I mentioned in the title and I haven't been able to work around it. I used unsigned int too, but to no avail. I am clear with my logic for this question, but fairly new to coding and hence face difficulties with errors such as this. I will attach only my DFS function below, where the problem seems to be occurring. Please help me with this. (Using adjacency list)
vector<int> adj[100001];
bool visited[100001]={false};
int parent=0;
int child=INT_MAX;
int counterr=0;
/*Recursive DFS:*/
void dfs(int s){
child=adj[s].size()-1;
if(child>parent)
counterr++;
if(s==1)
parent=adj[s].size();
else
parent=adj[s].size()-1;
visited[s]=true;
for(int i=0;i<adj[s].size();i++)
{
if(visited[adj[s][i]==false])
dfs(adj[s][i]);
}
}
For the most part, you can ignore this. However, if your integer exceeds the normal integer limit, or your adj[s] vector is empty, this may become a problem. If parent is never negative you can just change the line
int parent;
to
unsigned int parent;
which will completely prevent this warning.
for(int i=0;i<adj[s].size();i++)
You get this warning because adj[s].size()
is unsigned integer, but i
is signed.
For fix that warning use this instead, and i
became unsigned.
for (size_t i=0; i < adj[s].size(); i++)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.