-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1101.cpp
More file actions
56 lines (46 loc) · 1.05 KB
/
Copy path1101.cpp
File metadata and controls
56 lines (46 loc) · 1.05 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
class Solution {
public:
struct node
{
int ts;
int a;
int b;
node (int ts, int a, int b): a(a), b(b), ts(ts){};
bool operator< (node o){return ts < o.ts;};
};
bool un(int i, int j)
{
while(u[i] != i)
{
u[i] = u[u[i]];
i = u[i];
}
while(u[j] != j)
{
u[j] = u[u[j]];
j = u[j];
}
//此次没有添加新的连结
if(i == j)return false;
u[i] = j;
return true;
}
int earliestAcq(vector<vector<int>>& logs, int N) {
vector<node> v;
for(int i = 0; i < N; ++i)u[i] = i;
for(int i = 0; i < logs.size(); ++i)
{
node n(logs[i][0], logs[i][1], logs[i][2]);
v.push_back(n);
}
sort(v.begin(), v.end());
int cnt = 0;
for(int i = 0; i < v.size(); ++i)
{
if(un(v[i].a, v[i].b))cnt++;
if(cnt == N-1)return v[i].ts;
}
return -1;
}
int u[100];
};