-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
165 lines (130 loc) · 3.18 KB
/
main.cpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
#include <utility>
using namespace std;
struct node {
vector<int> neighbor;
bool visited = false;
};
typedef vector<node> graph_t;
typedef pair<int, int> edge_t;
typedef vector<int> path_t;
void dfsExplore(graph_t& graph, int u, path_t& visitOrder, vector<path_t>& cyclesList);
vector<path_t> getCycles(graph_t& graph);
unsigned int countCommonEdges(path_t path1, path_t path2);
unsigned int mcd(unsigned int x, unsigned int y);
int main()
{
int N, E;
ifstream in("input.txt");
in >> N >> E;
graph_t graph(N);
vector<edge_t> edges;
for(int i = 0; i < E; i++)
{
int source, destination;
in >> source >> destination;
edge_t edge(source, destination);
edges.push_back(edge);
graph[source].neighbor.push_back(destination);
graph[destination].neighbor.push_back(source);
}
vector<path_t> cycleList = getCycles(graph);
unsigned int maxSpecies = cycleList[0].size();
for(size_t i = 0; i < cycleList.size() - 1; i++)
{
unsigned int _maxSpecies = maxSpecies;
maxSpecies = mcd(cycleList[i].size(), cycleList[i + 1].size());
maxSpecies = mcd(maxSpecies, _maxSpecies);
unsigned int commonEdgesCount =
countCommonEdges(cycleList[i], cycleList[i + 1]);
maxSpecies = mcd(
maxSpecies,
cycleList[i].size() + cycleList[i + 1].size() - commonEdgesCount
);
}
vector<int> species(E);
for(path_t cycle:cycleList)
{
int currentSpecie = 0;
cycle.push_back(cycle[0]);
for(size_t i = 0; i < cycle.size() - 1; i++)
{
currentSpecie %= maxSpecies;
int n1, n2;
n1 = cycle[i];
n2 = cycle[i + 1];
if(n1 > n2)
{
swap(n1, n2);
}
pair<int, int> edge = make_pair(n1, n2);
unsigned int pos = find(edges.begin(), edges.end(), edge) - edges.begin();
species[pos] = currentSpecie;
currentSpecie++;
}
}
ofstream out("output.txt");
out << maxSpecies << endl;
for(int specie:species)
out << specie << endl;
out.close();
return 0;
}
void dfsExplore(graph_t& graph, int u, vector<int>& visitOrder, vector<vector<int>>& cyclesList)
{
graph[u].visited = true;
for(int node:graph[u].neighbor)
if(!graph[node].visited && !(graph[node].neighbor.empty()))
{
visitOrder.push_back(node);
dfsExplore(graph, node, visitOrder, cyclesList);
visitOrder.pop_back();
}
else
{
if(node != visitOrder[visitOrder.size() - 2])
{
unsigned int pos = find(
visitOrder.begin(),
visitOrder.end(),
node) - visitOrder.begin();
path_t toPush(visitOrder.begin() + pos, visitOrder.end());
if(!toPush.empty())
cyclesList.push_back(toPush);
}
}
}
vector<path_t> getCycles(graph_t& graph)
{
vector<path_t> cyclesList;
path_t visitOrder;
for(size_t node = 0; node < graph.size(); node++)
if(!graph[node].visited)
{
visitOrder.push_back(node);
dfsExplore(graph, node, visitOrder, cyclesList);
}
return cyclesList;
}
unsigned int countCommonEdges(path_t path1, path_t path2)
{
unsigned int count = 0;
for(int n1:path1)
for(int n2:path2)
if(n1 == n2)
count++;
return (count > 0) ? count - 1 : 0;
}
unsigned int mcd(unsigned int x, unsigned int y)
{
while(y != 0)
{
unsigned int z = x % y;
x = y;
y = z;
}
return x;
}