forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1101.cpp
More file actions
32 lines (30 loc) · 718 Bytes
/
Copy path1101.cpp
File metadata and controls
32 lines (30 loc) · 718 Bytes
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
class Solution
{
public:
int earliestAcq(vector<vector<int>>& logs, int N)
{
sort(logs.begin(), logs.end(), [](const vector<int>& a, const vector<int>& b){
return a[0] < b[0];
});
s = vector<int>(N + 1, 0);
for (int i = 0; i <= N; ++i) s[i] = i;
int cnt = 0;
for (auto& log : logs)
{
int rx = find(log[1]), ry = find(log[2]);
if (rx != ry)
{
s[rx] = ry;
cnt++;
}
if (cnt == N - 1) return log[0];
}
return -1;
}
vector<int> s;
int find(int x)
{
if (s[x] != x) s[x] = find(s[x]);
return s[x];
}
}