#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class Graph{
public:
vector<int> adjList[10001];
void addEdge(int u,int v){
adjList[u].push_back(v);
adjList[v].push_back(u);
}
};
bool dfs(Graph graph, int n){
vector<int> neighbors;
int curr,parent;
bool visited[10001] = {0};
stack<int> s;
//Depth First Search
s.push(1);
parent = 0;
while(!s.empty()){
curr = s.top();
neighbors = graph.adjList[curr];
s.pop();
//If current is unvisited
if(visited[curr] == false){
for(int j=0; j<neighbors.size(); j++){
//If node connected to itself, then cycle exists
if(neighbors[j] == curr){
return false;;
}
else if(visited[neighbors[j]] == false){
s.push(neighbors[j]);
}
//If the neighbor is already visited, and it is not a parent, then cycle is detected
else if(visited[neighbors[j]] == true && neighbors[j] != parent){
return false;
}
}
//Mark as visited
visited[curr] = true;
parent = curr;
}
}
//Checking if graph is fully connected
for(int i=1; i<=n; i++){
if(visited[i] == false){
return false;
}
}
//Only if there are no cycles, and it's fully connected, it's a tree
return true;
}
int main() {
int m,n,u,v;
cin>>n>>m;
Graph graph = Graph();
//Build the graph
for(int edge=0; edge<m; edge++){
cin>>u>>v;
graph.addEdge(u,v);
}
if(dfs(graph,n)){
cout<<"YES"<<endl;
}
else{
cout<<"NO"<<endl;
}
return 0;
}
I am trying to determine if a given graph is a tree.
I perform DFS and look for cycles, if a cycle is detected, then the given graph is not a tree.
Then I check if all nodes have been visited, if any node is not visited, then given graph is not a tree
The first line of input is:
n m
Then m lines follow, which represent the edges connecting two nodes
n is number of nodes
m is number of edges
example input:
3 2
1 2
2 3
This is a SPOJ question http://www.spoj.com/problems/PT07Y/ and I am getting Wrong Answer. But the DFS seems to be correct according to me.
So I checked your code against some simple test cases in comments, and it seems that for
7 6
3 1
3 2
2 4
2 5
1 6
1 7
you should get YES as answer, while your program gives NO.
This is how neighbours looks like in this case:
1: 3 6 7
2: 3 4 5
3: 1 2
4: 2
5: 2
6: 1
7: 1
So when you visit 1 you push 3,6,7 on the stack. Your parent is set as 1. This is all going good.
You pop 7 from the stack, you don't push anything on the stack and cycle check clears out, so as you exit while loop you set visited[7] as true and set you parent to 7 (!!!!!).
Here is you can see this is not going well, since once you popped 6 from the stack you have 7 saved as parent. And it should be 1. This makes cycle check fail on neighbor[0] != parent.
I'd suggest adding keeping parent in mapped array and detect cycles by applying union-merge.
Related
So this question came to my mind when solving some dijikstra's based leetcode problems.
We have node, distance pairs in priority queue at each step.
Having duplicate nodes in the heap depends on one thing i.e when we mark the node as visited(i.e confirming that we have found the shortest length for it). If we mark it while pushing into the queue we will not have any duplicates, if we mark after popping from the queue, we may have duplicates in the queue.
https://leetcode.com/problems/network-delay-time/
In this question we can mark the node as visited only after popping from the priority Queue, or we will miss some edge that may lead to a shorter path.
Ex:
[[1,2,1],[2,3,2],[1,3,4]]
3
1
If we add while inserting we will get wrong answer while exploring 1's neighbors what we are doing is ,
1->2 queue={2,1} visited={1,2}
1->3 queue{(2,1), (3,4)}
since all nodes are now visited, we will never encounter the path 1->2->3 distance=1+2=3.
But in other questions we can do a dijikstra with visited marked before the insertion into the priority queue, ex:
https://leetcode.com/problems/swim-in-rising-water/
why is dijikstra with visited marked before the insertion valid here
BFS is for blindly visiting nodes (may assume all weight 1), Dijkstra will prioritize with the least weighted path.
When can we have duplicate nodes in the heap in Dijkstra algorithm?
a
4/ \2
/ \
b ---- c
| 1
4 |
d
1. here start Dijkstra from a. queue = (a, 0)
2. b, c pushed with path cost into p_queue. queue = (b, 4), (c, 2)
3. c popped. b with another cost pushed. queue = (b, 4), (b, 3) here the new (b, 3) has (ac + cb) cost
4. least b popped. d pushed queue = (b, 4), (d, 7)
5. As we check and mark after pop. b popped. queue = (d, 7)
6. But already visited so returned
7. Process d
But in other questions we can do a Dijkstra with visited marked before the insertion into the priority queue, ex: https://leetcode.com/problems/swim-in-rising-water
why is Dijkstra with visited marked before the insertion valid here?
Depends largely on the problem itself. As for this particular problem, we get weight directly from the node, and no matter whether we push it before or after, it will be popped at the same time, though keeping visited checking before push will prevent redundant pushing.
Here is my accepted implementation where you can comment out or keep either after pop or before push marking & checking for visited nodes and both will get accepted.
class Solution {
struct PosWeight {
pair<int, int> pos;
int weight;
PosWeight(pair<int, int> a, int weight): pos(a), weight(weight){}
};
int visited[51][51] = {0};
int traverse(vector<vector<int>>& grid) {
int row = size(grid);
int column = size(grid[0]);
vector<pair<int, int>> directions = { {0,1}, {1,0}, {-1,0}, {0,-1} };
auto isValidTo = [&grid, row, column]
(pair<int, int> direction, pair<int, int> pos) {
if (pos.first + direction.first >= row) return false;
if (pos.first + direction.first < 0) return false;
if (pos.second + direction.second >= column) return false;
if (pos.second + direction.second < 0) return false;
return true;
};
auto comp = [](PosWeight &a, PosWeight &b) {
return a.weight > b.weight;
};
int maxx =INT_MIN;
priority_queue<PosWeight, vector<PosWeight>, decltype(comp)> pq(comp);
pq.emplace(make_pair(0,0), grid[0][0]);
while(!pq.empty()) {
auto elem = pq.top();
pq.pop();
// You can comment out this portion and keep the later one before pushing in queue
if (visited[elem.pos.first][elem.pos.second]) continue;
visited[elem.pos.first][elem.pos.second] = 1;
// ---
maxx = max(maxx, elem.weight);
if (elem.pos.first == row - 1 && elem.pos.second == column - 1)
return maxx;
for(auto direction: directions)
if (isValidTo(direction, elem.pos)) {
pair<int,int> toGo = make_pair( direction.first + elem.pos.first,
direction.second + elem.pos.second );
auto weight = grid[toGo.first][toGo.second];
// You can comment out this portion and keep the later one before pushing in queue
// if (visited[toGo.first][toGo.second]) continue;
// visited[toGo.first][toGo.second] = 1;
// -----
pq.emplace(toGo, weight);
}
}
return maxx;
}
public:
int swimInWater(vector<vector<int>>& grid) {
return traverse(grid);
}
};
But, for https://leetcode.com/problems/network-delay-time this problem, the checking must be after pop as doing so before push will cause early all node visits instead of prioritized shortest as you stated in your question.
class Solution {
public:
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
auto comp = [](pair<int,int> &a, pair<int,int> &b) {
return a.second > b.second;
};
priority_queue<pair<int,int>, vector<pair<int,int>>, decltype(comp)> que(comp);
vector<vector<int>> rel(n, vector<int>(n, -1));
for (auto &time: times)
rel[time[0] - 1][time[1] - 1] = time[2];
vector<int> visit(n, 0);
que.push({k-1, 0});
int sz = n;
while (size(que)) {
auto now = que.top();
que.pop();
if (visit[now.first]) continue;
visit[now.first] = 1;
if (!--sz) return now.second;
auto id = now.first, val = now.second;
for (int i = 0; i < n; ++i)
if (rel[id][i] != -1) {
// cant do checking here
que.push({i, val + rel[id][i]});
}
}
return -1;
}
};
So, bottom line, it depends on the nature and requirement of the problem.
Problem link: UVa 539 - The Settlers of Catan
(UVa website occasionally becomes down. Alternatively, you can read the problem statement pdf here: UVa External 539 - The Settlers of Catan)
This problem gives a small general graph and asks to find the longest road. The longest road is defined as the longest path within the network that doesn’t use an edge twice. Nodes may be visited more than once, though.
Input Constraints:
1. Number of nodes: n (2 <= n <= 25)
2. Number of edges m (1 <= m <= 25)
3. Edges are un-directed.
4. Nodes have degrees of three or less.
5. The network is not necessarily connected.
Input is given in the format:
15 16
0 2
1 2
2 3
3 4
3 5
4 6
5 7
6 8
7 8
7 9
8 10
9 11
10 12
11 12
10 13
12 14
The first two lines gives the number of nodes n and the number of edges m for this test case respectively. The next m lines describe the m edges. Each edge is given by the numbers of the two nodes connected by it. Nodes are numbered from 0 to n - 1.
The above test can be visualized by the following picture:
Now I know that finding the longest path in a general graph is NP-hard. But as the number of nodes and edges in this problem is small and there's a degree bound of each node, a brute force solution (recursive backtracking) will be able to find the longest path in the given time limit (3.0 seconds).
My strategy to solve the problem was the following:
1. Run DFS (Depth First Search) from each node as the graph can be disconnected
2. When a node visits its neighbor, and that neighbor visits its neighbor and so on, mark the edges as used so that no edge can be used twice in the process
3. When the DFS routine starts to come back to the node from where it began, mark the edges as unused in the unrolling process
4. In each step, update the longest path length
My implementation in C++:
#include <iostream>
#include <vector>
// this function adds an edge to adjacency matrix
// we use this function to build the graph
void addEdgeToGraph(std::vector<std::vector<int>> &graph, int a, int b){
graph[a].emplace_back(b);
graph[b].emplace_back(a); // undirected graph
}
// returns true if the edge between a and b has already been used
bool isEdgeUsed(int a, int b, const std::vector<std::vector<char>> &edges){
return edges[a][b] == '1' || edges[b][a] == '1'; // undirected graph, (a,b) and (b,a) are both valid edges
}
// this function incrementally marks edges when "dfs" routine is called recursively
void markEdgeAsUsed(int a, int b, std::vector<std::vector<char>> &edges){
edges[a][b] = '1';
edges[b][a] = '1'; // order doesn't matter, the edge can be taken in any order [(a,b) or (b,a)]
}
// this function removes edge when a node has processed all its neighbors
// this lets us to reuse this edge in the future to find newer (and perhaps longer) paths
void unmarkEdge(int a, int b, std::vector<std::vector<char>> &edges){
edges[a][b] = '0';
edges[b][a] = '0';
}
int dfs(const std::vector<std::vector<int>> &graph, std::vector<std::vector<char>> &edges, int current_node, int current_length = 0){
int pathLength = -1;
for(int i = 0 ; i < graph[current_node].size() ; ++i){
int neighbor = graph[current_node][i];
if(!isEdgeUsed(current_node, neighbor, edges)){
markEdgeAsUsed(current_node, neighbor, edges);
int ret = dfs(graph, edges, neighbor, current_length + 1);
pathLength = std::max(pathLength, ret);
unmarkEdge(current_node, neighbor, edges);
}
}
return std::max(pathLength, current_length);
}
int dfsFull(const std::vector<std::vector<int>> &graph){
int longest_path = -1;
for(int node = 0 ; node < graph.size() ; ++node){
std::vector<std::vector<char>> edges(graph.size(), std::vector<char>(graph.size(), '0'));
int pathLength = dfs(graph, edges, node);
longest_path = std::max(longest_path, pathLength);
}
return longest_path;
}
int main(int argc, char const *argv[])
{
int n,m;
while(std::cin >> n >> m){
if(!n && !m) break;
std::vector<std::vector<int>> graph(n);
for(int i = 0 ; i < m ; ++i){
int a,b;
std::cin >> a >> b;
addEdgeToGraph(graph, a, b);
}
std::cout << dfsFull(graph) << '\n';
}
return 0;
}
I was ordering what is the worst case for this problem? (I'm wondering it should be n = 25 and m = 25) and in the worst case in total how many times the edges will be traversed? For example for the following test case with 3 nodes and 2 edges:
3 2
0 1
1 2
The dfs routine will be called 3 times, and each time 2 edges will be visited. So in total the edges will be visited 2 x 3 = 6 times. Is there any way to find the upper bound of total edge traversal in the worst case?
I have been implementing prim's algorithm in c++, but I am not able to figure out why my algorithm is giving the wrong output.
Input Format: The first line has number of Test cases T.For each test case, Number of vertices and Number of edges are given in next line.For the Number of edges, each line consists of 3 numbers a,b,w where there is an undirected,weighted edge between vertices a and b with weight w.
Below is my code :
#include <bits/stdc++.h>
using namespace std;
int spanningTree(int V,int E,vector<vector<int> > graph);
// Driver code
int main()
{
int t;
cin>>t;
while(t--)
{
int V,E;
cin>>V>>E;
vector< vector<int> > graph(V,vector<int>(V,INT_MAX));
while(E--)
{
int u,v,w;
cin>>u>>v>>w;
u--,v--;
graph[u][v] = w;
graph[v][u] = w;
}
cout<<spanningTree(V,E,graph)<<endl;
}
return 0;
}
/*This is a function problem.You only need to complete the function given below*/
// Function to construct and print MST for
// a graph represented using adjacency
// matrix representation, with V vertices.
// graph[i][j] = weight if edge exits else INT_MAX
#include <limits.h>
int *parent;
bool *spt;
int *distance1;
int getMinVertex(int distance1[],bool spt[],int V)
{
int min_weight=INT_MAX,min_vertex=-1;
for(int i=0;i<V;i++)
{
if(!spt[i] && distance1[i]<min_weight)
min_weight=distance1[i],min_vertex=i;
}
cout<<"Min vertex : "<<min_vertex<<endl;
return min_vertex;
}
int spanningTree(int V,int E,vector<vector<int> > graph)
{
// code here
parent=new int[V];
spt=new bool[V];
distance1=new int[V];
for(int i=0;i<V;i++)
parent[i]=-1,spt[i]=false,distance1[i]=INT_MAX;
distance1[0]=0;
parent[0]=-1;
//spt[0]=true;
int current_vertex=getMinVertex(distance1,spt,V);
int mst_weight=0;
while(current_vertex!=-1)
{ cout<<"Visiting : "<<current_vertex<<endl;
for(int i=0;i<V;i++)
{
if(i!=current_vertex) //ignore self-loop
{
int neighbour_weight=graph[current_vertex][i];
if(!spt[i]&&neighbour_weight!=INT_MAX&&distance1[i]>graph[current_vertex][i])
{
distance1[i]=graph[current_vertex][i];
parent[i]=current_vertex;
}
}
}
mst_weight+=distance1[current_vertex];
spt[current_vertex]=true;
current_vertex=getMinVertex(distance1,spt,V);
cout<<"\nDistance array : \n";
for(int i=0;i<V;i++)
cout<<"dist["<<i<<"]:"<<distance1[i]<<" ";
cout<<endl;
cout<<"\nParent Array\n";
for(int i=0;i<V;i++)
cout<<"Parent["<<i<<"]:"<<parent[i]<<" ";
cout<<"\n\n";
} //end of while loop.
delete [] parent;
delete [] spt;
delete [] distance1;
//cout<<mst_weight<<endl;
return mst_weight;
}
Array spt[] consists of set of vertices already included in the set of minimum spanning tree.distance1[] array holds the distance values.Array parent[] is used to keep track of MST tree being formed.
Below is the code output for input:
2
3 2
1 2 3 1 3 3
4 3
1 3 5 1 4 6 2 3 7
Output:
Min vertex : 0
Visiting : 0
Min vertex : 2
Distance array :
dist[0]:0 dist[1]:5 dist[2]:1
Parent Array
Parent[0]:-1 Parent[1]:0 Parent[2]:0
Visiting : 2
Min vertex : 1
Distance array :
dist[0]:0 dist[1]:3 dist[2]:1
Parent Array
Parent[0]:-1 Parent[1]:2 Parent[2]:0
Visiting : 1
Min vertex : -1
Distance array :
dist[0]:0 dist[1]:3 dist[2]:1
Parent Array
Parent[0]:-1 Parent[1]:2 Parent[2]:0
4
Min vertex : 0
Visiting : 0
Min vertex : 1
Distance array :
dist[0]:0 dist[1]:5
Parent Array
Parent[0]:-1 Parent[1]:0
Visiting : 1
Min vertex : -1
Distance array :
dist[0]:0 dist[1]:5
Parent Array
Parent[0]:-1 Parent[1]:0
5
I am completely unable to make out what is going wrong with my code.Can someone please help.
The code works for negative edges too and I have used priority queue
please check it and let me know what's wrong with it and why is this working even for negative edges.
Constraint: edges should be less than 10000 length
Am I doing something wrong here?
As I read Djiktra's algo fails for negative edges but I implemented the same concept and my code is working? Is there some bug?
#include<iostream>
#include<queue>
using namespace std;
struct reach
{
int n;
int weight;
};
struct cmp
{
bool operator()(reach a, reach b)
{
return a.weight>b.weight;
}
};
class sp
{
int *dist;
int n;
int **graph;
int src;
public:
sp(int y)
{
n=y;
src=1;
dist=new int[n+1];
graph=new int*[n+1];
for(int i=0;i<=n;i++)
{
graph[i]=new int[n+1];
}
for(int i=2;i<=n;i++)
{
dist[i]=10000;
}
// cout<<INT_MAX<<endl;
dist[src]=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
graph[i][j]=10000;
}
}
graph[1][1]=0;
}
void read()
{
int a;
cout<<"enter number of edges"<<endl;
cin>>a;
cout<<"now enter the two vertices which has an edge followed by the weight of the edge"<<endl;
while(a--)
{//cout<<"location: "<<i<<" : "<<j<<endl;
int as, ad,val;
cin>>as>>ad>>val;
graph[as][ad]=val;
}
}
void finder()
{cout<<"enetered"<<endl;
priority_queue<reach, vector<reach>, cmp> q;
for(int i=1;i<=n;i++)
{
if(dist[src]+graph[src][i]<dist[i])
{
reach temp;
temp.n=i;
cout<<i<<endl;
temp.weight=graph[src][i];
q.push(temp);
dist[i]=graph[src][i]+dist[src];
}
}
while(q.empty()!=1)
{
reach now =q.top();
//cout<<" we have here: "<<now.n<<endl;
q.pop();
for(int i=1;i<=n;i++)
{
if((dist[now.n] + graph[now.n][i])<dist[i])
{
dist[i]=dist[now.n]+graph[now.n][i];
cout<<"it is: "<<i<<" : "<<dist[i]<<endl;
reach temp;
temp.n=i;
//cout<<temp.n<<endl;
temp.weight=graph[now.n][i];
q.push(temp);
}
}
}
}
void print()
{
for(int i=1;i<=n;i++)
{
cout<<"we have: "<<dist[i]<<" at "<<i;
cout<<endl;
}
cout<<endl;
}
};
int main()
{cout<<"enter no. of vertices"<<endl;
int n;
cin>>n;
sp sp(n);
sp.read();
sp.finder();
sp.print();
}
Consider this example:
Undirected Graph(6v,8e)
Edges (v1-v2 (weight)):
1-2 (2)
2-3 (1)
1-3 (-2)
1-6 (-2)
3-6 (-3)
5-6 (1)
4-5 (2)
2-5 (1)
Now, Let source be 1 and destination be 4. There is one cycle from source to source (1-3-6-1) which weighs (-7). So, lets list a few paths from source to destination with weights:
1-6-5-4 (1)
1-3-6-5-4 (-2)
1-3-6-1-3-6-5-4 (-9)
1-3-6-1-3-6-1-3-6-5-4 (-16)
etc.
So, which path is the shortest? Since it is ambiguous, the algorithm does not work. Now, you can argue that if a node is visited, you will not update it. In this case, you will not get the correct answer. May be there are some cases where in-spite of having negative edges, algo gives correct results, but this is not how Dijkstra works.
A really simple way to understand Dijkstra is that it performs BFS on the graph, from source till destination, and in every step it updates the visited nodes. So, if there is a node n which has cost c and a few levels deep in bfs, its cost becomes k (<c). Then again you will have to update all the nodes visited from n for their shorter paths (because path to n is now shorter). Since graph has negative edges, if it has a cycle, n will keep updating infinitely and will never end.
The simplest graph for which Dijkstra's algorithm fails with negative weights has adjacency matrix
0 1 2
1 0 -3
2 -3 0
and looks for a route from vertex 0 to vertex 1. The first vertex to come off the priority queue is vertex 1 at distance 1, so that's the route returned. But there was a route of total weight -1 via a vertex which is still in the priority queue, with weight 2.
I had a test right now and this was one of the questions:
Input
The places to visit in the labyrinth are numbered from 1 to n. The entry and
the exit correspond to number 1 and number n, respectively; the remaining
numbers correspond to crossings. Note that there are no dead ends and
there is no more than one connection linking a pair of crossings.
For each test case, the first line gives n and the number of connections
between crossings (m). Then, in each of the following m lines, you find a pair
of integers corresponding to the connection between two crossings.
Output
For each test case, your implementation should output one single line
containing "Found!", if it is possible to reach the exit by visiting every
crossing once or "Damn!", otherwise. Other test cases may follow.
Constraints
m < 32
n < 21
Example input:
8 13
1 2
1 3
2 3
2 4
3 4
3 5
4 5
4 6
5 6
5 7
6 7
6 8
7 8
8 8
1 2
1 3
2 4
3 5
4 6
5 7
6 8
7 8
Example output:
Found!
Damn!
I solved the problem using a sort of DFS algorithm but i have a few questions.
Using DFS algorithm, I implemented a recursive function that starts in the given node and tries to visit every node once and the last node must be the exit node. I don't have the full code right now but but it was something like this:
findPath(int current node, int numVisitedNodes, int *visited){
int *tmpVisited = copyArray(visited); //copies the visited array to tmpVisited
//DFS algo here
}
Every recursive call it copies the visited nodes array. I'm doing this because when it finds an invalid path and the recursion goes back to the origin, it can still go because no one overwrote the visited nodes list.
Is there any better way to do this?
How would you solve it? (you can provide code if you want)
Read the crossing
if start or end of the crossing belongs to a reachable set, add both to that set else create a new reachable set.
When input has finished, check if any of the reachable sets contains
both entrance and exit points
HashSet operations complexity is O(1). If every crossing are distinct, complexity is O(n^2),which is the worst case complexity of this algorithm. Space complexity is O(n), there is no recursion so there is no recursion overhead of memory.
Roughly speaking, every node is visited only once.
Java code using valid reachable sets is as follows.
public class ZeManel {
public static void main(String[] args) {
Integer[][] input = {{1,2},{2,3},{4,6}};
zeManel(input);
}
public static void zeManel(Integer[][] input){
List<Set<Integer>> paths = new ArrayList<Set<Integer>>();
int max = 0;
for(int i = 0;i < input.length;i++) {
max = input[i][0] > max ? input[i][0] : max;
max = input[i][1] > max ? input[i][1] : max;
boolean inPaths = false;
for (Set<Integer> set : paths) {
if(set.contains(input[i][0]) || set.contains(input[i][1])) {
set.add(input[i][0]);
set.add(input[i][1]);
inPaths = true;
break;
}
}
if(!inPaths) {
Set<Integer> path = new HashSet<Integer>();
path.add(input[i][0]);
path.add(input[i][1]);
paths.add(path);
}
}
for (Set<Integer> path : paths) {
if(path.contains(1) && path.contains(max)) {
System.out.println("Found!");
return;
}
}
System.out.println("Damn!");
}
}
This was my implementation during the test:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
# define N 21
# define M 32
int i;
int adj[N][N];
int count = 0;
int findPath(int numNodes, int currentNode, int depth, int *visited){
visited[currentNode] = 1;
if(currentNode == numNodes - 1 && depth == numNodes){
return 1;
}
if(depth > numNodes)
return -1;
int r = -1;
if(depth < numNodes){
count++;
int *tmp = (int*) malloc(numNodes*sizeof(int));
for(i = 0; i < numNodes; i++)
tmp[i] = visited[i];
for(i = 0; i < numNodes; i++){
if(adj[currentNode][i] == 1 && tmp[i] == 0 && r == -1){
if(findPath(numNodes, i, depth + 1, tmp) == 1)
r = 1;
}
}
free(tmp);
}
return r;
}
int main(){
int numLigacoes, a, b, numNodes;
int *visited;
while (scanf("%d %d", &numNodes, &numLigacoes) != EOF){
visited = (int*) malloc(numNodes*sizeof(int));
count = 0;
memset(adj, 0, N*N*sizeof(int));
memset(visited, 0, numNodes*sizeof(int));
for (i = 0; i < numLigacoes; i++){
scanf("%d %d", &a, &b);
adj[a - 1][b - 1] = 1;
adj[b - 1][a - 1] = 1;
}
if(findPath(numNodes, 0, 1, visited) == 1)
printf("Found! (%d)\n", count);
else
printf("Damn! (%d)\n", count);
free(visited);
}
return 0;
}
What do you think about that?