-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetectingCycles.cpp
More file actions
executable file
·130 lines (123 loc) · 3.5 KB
/
Copy pathDetectingCycles.cpp
File metadata and controls
executable file
·130 lines (123 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstdlib>
using namespace std;
class Node
{
public:
int value;
int parent;
};
class DFSNode
{
public:
int value;
int index;
vector<int> children;
};
int dfsCyclesCount = 0;
void dfs(vector<DFSNode> &dfsNodes, int parentIndex, int currentNodeIndex, vector<bool> &visited)
{
visited[currentNodeIndex] = true;
if(dfsNodes[currentNodeIndex].children.empty() != true)
{
vector<int>::iterator it = dfsNodes[currentNodeIndex].children.begin();
for(;it != dfsNodes[currentNodeIndex].children.end();)
{
if(*it != parentIndex)
{
if(visited[*it] == true)
{
vector<int>::iterator position = find(dfsNodes[*it].children.begin(), dfsNodes[*it].children.end(), currentNodeIndex);
if(position != dfsNodes[*it].children.end())
{
dfsNodes[*it].children.erase(position);
}
it = dfsNodes[currentNodeIndex].children.erase(it);
dfsCyclesCount++;
}
else
{
dfs(dfsNodes, currentNodeIndex, *it, visited);
++it;
}
}
else
{
it = dfsNodes[currentNodeIndex].children.erase(it);
}
}
}
}
int main()
{
int testcases;
cin >> testcases;
for(int j = 0; j < testcases; j++)
{
int inputSize;
cin >> inputSize;
vector<Node> inputValues(inputSize);
int numberOfQueries;
cin >> numberOfQueries;
int cyclesCount = 0;
// vector<int> input2(input);
//Using Disjoint groups
for(int i = 0; i < inputSize; i++)
{
inputValues[i].value = i + 1;
inputValues[i].parent = i;
}
for(int i = 0; i < numberOfQueries; i++)
{
int first, second;
cin >> first >> second;
int parentValue1 = inputValues[first].parent;
while(inputValues[parentValue1].parent != parentValue1)
{
parentValue1 = inputValues[parentValue1].parent;
}
int parentValue2 = inputValues[second].parent;
while(inputValues[parentValue2].parent != parentValue2)
{
parentValue2 = inputValues[parentValue2].parent;
}
if(parentValue1 == parentValue2)
{
cyclesCount++;
}
else
{
inputValues[parentValue2].parent = parentValue1;
}
}
cout << cyclesCount << endl;
//Uisng DFS
vector<DFSNode> dfsNodes(inputSize);
for(int i = 0; i < inputSize; i++)
{
dfsNodes[i].value = i;
dfsNodes[i].index = i;
}
for(int i = 0; i < numberOfQueries; i++)
{
int first, second;
cin >> first >> second;
dfsNodes[first].children.push_back(second);
dfsNodes[second].children.push_back(first);
}
vector<bool> visited(inputSize, false);
for(int i = 0; i < inputSize; i++)
{
if(visited[i] == false)
{
dfs(dfsNodes, -1, i, visited);
}
}
cout << dfsCyclesCount << endl;
}
}