-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathABC_199_D.cpp
131 lines (106 loc) · 1.81 KB
/
ABC_199_D.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
#include <iostream>
#include <vector>
using namespace std;
int n, m;
vector<int> edges[20];
int colors[20];
int group[20];
vector<int> groups[21];
vector<int> newGroups[21];
const int RED = 1;
const int GREEN = 2;
const int BLUE = 3;
bool isSafe(int node, int color)
{
for (int i = 0; i < edges[node].size(); i++)
{
int adj = edges[node][i];
if (colors[adj] == color)
{
return 0;
}
}
return 1;
}
long long dfs(int groupIdx, int nodeIdx) {
if (nodeIdx >= groups[groupIdx].size() - 1)
{
return 1;
}
long long ret = 0;
bool used[4] = { false };
int node = groups[groupIdx][nodeIdx + 1];
for (int c = 1; c <= 3; c++)
{
bool ok = true;
for (int i = 0; i < edges[node].size(); i++)
{
int adj = edges[node][i];
if (colors[adj] == c)
{
ok = false;
break;
}
}
if (ok)
{
colors[groups[groupIdx][nodeIdx + 1]] = c;
ret += dfs(groupIdx, nodeIdx + 1);
colors[groups[groupIdx][nodeIdx + 1]] = 0;
}
}
return ret;
}
int grouping(int node, int g) {
int ret = 1;
for (int i = 0; i < edges[node].size(); i++)
{
int next = edges[node][i];
if (group[next] == 0)
{
group[next] = g;
groups[g].push_back(next);
grouping(next, g);
}
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
while (m--)
{
int a, b;
cin >> a >> b;
a--; b--;
edges[a].push_back(b);
edges[b].push_back(a);
}
for (int i = 0; i < n; i++)
{
if (group[i] == 0)
{
group[i] = i + 1;
groups[i + 1].push_back(i);
grouping(i, i + 1);
}
}
long long res = 1;
for (int i = 0; i <= n; i++)
{
long long temp = 0;
if (groups[i].size() == 0)
{
continue;
}
for (int c = 1; c <= 1; c++)
{
colors[groups[i][0]] = 1;
temp += 3 * dfs(i, 0);
colors[groups[i][0]] = 0;
}
res *= temp;
}
cout << res << endl;
}