-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathMST Edge Check.cpp
More file actions
117 lines (103 loc) · 2.38 KB
/
MST Edge Check.cpp
File metadata and controls
117 lines (103 loc) · 2.38 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
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
#include "bits/stdc++.h"
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
#define endl '\n'
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define F first
#define S second
const int N = 1e5+5;
struct Edge {
int u, v, w, id, x;
bool operator<(const Edge& other) const {
return w < other.w;
}
};
int parent[N];
int rnk[N];
void Dsu(int n){
for(int i=0;i<=n;i++) {
parent[i] = i;
rnk[i] = 0;
}
}
int Find(int v) {
if (v == parent[v])
return v;
return parent[v] = Find(parent[v]);
}
void Union(int a, int b) {
a = Find(a);
b = Find(b);
if (a != b) {
if (rnk[a] < rnk[b])
swap(a, b);
parent[b] = a;
if (rnk[a] == rnk[b])
rnk[a]++;
}
}
void solve() {
int n, m;
cin >> n >> m;
Edge edges[m];
for (int i = 0; i < m; i++) {
int uu, vv, w;
cin >> uu >> vv >> w;
edges[i] = {uu, vv, w, i, 0};
}
Dsu(n);
sort(edges, edges + m);
int l = 0, r = 0;
while(r < m) {
if ( edges[r].w != edges[l].w ) {
for(int i = l; i < r; i++) {
int uu = edges[i].u;
int vv = edges[i].v;
if (Find(uu) != Find(vv)) {
edges[i].x = 1; // Mark this edge as part of the MST
}
}
for(int i = l; i < r; i++) {
int uu = edges[i].u;
int vv = edges[i].v;
if (Find(uu) != Find(vv)) {
Union(uu, vv);
}
}
l = r;
}
r++;
}
for(int i = l; i < r; i++) {
int uu = edges[i].u;
int vv = edges[i].v;
if (Find(uu) != Find(vv)) {
edges[i].x = 1; // Mark this edge as part of the MST
}
}
for(int i = l; i < r; i++) {
int uu = edges[i].u;
int vv = edges[i].v;
if (Find(uu) != Find(vv)) {
Union(uu, vv);
}
}
sort(edges, edges + m, [](const Edge& a, const Edge& b) {
return a.id < b.id;
});
for(int i = 0; i < m; i++) {
int uu = edges[i].u;
int vv = edges[i].v;
if( edges[i].x ) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
}
int main() {
FASTIO;
solve();
}