-
Notifications
You must be signed in to change notification settings - Fork 17
/
1627. Graph Connectivity With Threshold.cpp
174 lines (138 loc) · 4.18 KB
/
1627. Graph Connectivity With Threshold.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
166
167
168
169
170
171
172
173
174
//graph
//WA
//57 / 66 test cases passed.
//indirectly connection also considered true?
class Solution {
public:
int gcd(int x, int y){
if(y == 0) return x;
return gcd(y, x%y);
}
int encode(int& i, int& j, int& n){
return i*(n+1)+j;
}
vector<bool> areConnected(int n, int threshold, vector<vector<int>>& queries) {
int m = queries.size();
vector<bool> ans(m, true);
if(threshold == 0){
return ans;
}
unordered_set<int> edges;
for(int i = 1; i <= n; ++i){
for(int j = i+1; j <= n; ++j){
if(gcd(i, j) > threshold){
edges.insert(encode(i, j, n));
}
}
}
for(int i = 0; i < m; ++i){
vector<int>& q = queries[i];
if(q[0] > q[1]) swap(q[0], q[1]);
ans[i] = (edges.find(encode(q[0], q[1], n)) != edges.end());
}
return ans;
}
};
//graph
//fix above: indirectly connected cities should also be seen as connected
//TLE
//64 / 66 test cases passed.
class Solution {
public:
int gcd(int x, int y){
if(y == 0) return x;
return gcd(y, x%y);
}
int encode(int& i, int& j, int& n){
return i*(n+1)+j;
}
vector<bool> areConnected(int n, int threshold, vector<vector<int>>& queries) {
int m = queries.size();
vector<bool> ans(m, true);
if(threshold == 0){
return ans;
}
vector<unordered_set<int>> adjList(n+1);
for(int i = 1; i <= n; ++i){
for(int j = i+1; j <= n; ++j){
if(gcd(i, j) > threshold){
adjList[i].insert(j);
adjList[j].insert(i);
}
}
}
for(int i = 0; i < m; ++i){
vector<int>& query = queries[i];
if(query[0] > query[1]) swap(query[0], query[1]);
bool connected = false;
queue<int> q;
vector<bool> visited(n+1, false);
int cur;
q.push(query[0]);
visited[query[0]] = true;
while(!q.empty()){
cur = q.front(); q.pop();
if(cur == query[1]){
connected = true;
break;
}
for(const int& nei : adjList[cur]){
if(visited[nei]) continue;
visited[nei] = true;
q.push(nei);
}
}
ans[i] = connected;
}
return ans;
}
};
//DSU
//from hint
//Runtime: 360 ms, faster than 57.32% of C++ online submissions for Graph Connectivity With Threshold.
//Memory Usage: 65.4 MB, less than 6.13% of C++ online submissions for Graph Connectivity With Threshold.
class DSU{
public:
vector<int> parent;
DSU(int n){
parent = vector<int>(n);
iota(parent.begin(), parent.end(), 0);
}
int find(int x){
if(parent[x] == x) return x;
return find(parent[x]);
}
void unite(int x, int y){
int px = find(x);
int py = find(y);
parent[py] = px;
}
};
class Solution {
public:
int gcd(int x, int y){
if(y == 0) return x;
return gcd(y, x%y);
}
vector<bool> areConnected(int n, int threshold, vector<vector<int>>& queries) {
int m = queries.size();
vector<bool> ans(m, true);
if(threshold == 0){
return ans;
}
DSU dsu(n+1);
for(int i = threshold+1; i <= n; ++i){
for(int j = i*2; j <= n; j += i){
//gcd(i, j) > threshold must hold!
//if(gcd(i, j) > threshold){
dsu.unite(i, j);
//}
}
}
for(int i = 0; i < m; ++i){
vector<int>& q = queries[i];
ans[i] = dsu.find(q[0]) == dsu.find(q[1]);
}
return ans;
}
};