-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16950.cpp
63 lines (50 loc) · 1.46 KB
/
16950.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
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
struct Road{
int weight, par, node;
};
struct compare{
bool operator()(Road &a, Road &b){
return a.weight < b.weight;
}
};
int main(){
cin.tie(NULL);
ios_base::sync_with_stdio();
int n, m, k, f, t, w;
char c;
vector<pair<int, int>> v[1010];
priority_queue<Road, vector<Road>, compare> kPQ;
vector<pair<int, int>> maxTree;
int visited[1010] = {0};
cin >> n >> m >> k;
for(int i=0; i<m; i++){
cin >> c >> f >> t;
if(c == 'R') w = 0; else w = 1;
v[f].push_back({w, t});
v[t].push_back({w, f});
}
int kWeights = 0;
int numVisited = 1;
int lastVisited = 1;
bool isK = true;
visited[1] = true;
while(maxTree.size() < n-1){
for(auto node : v[lastVisited]) if(!visited[node.second]) kPQ.push({node.first, lastVisited, node.second});
while(visited[kPQ.top().node]) kPQ.pop();
if(kWeights == k) while(kPQ.top().weight == 1&&!kPQ.empty()) kPQ.pop();
if(kPQ.size() == 0){
isK = false;
break;
}
kWeights += kPQ.top().weight;
visited[kPQ.top().node] = true;
maxTree.push_back({kPQ.top().par, kPQ.top().node});
lastVisited = kPQ.top().node;
kPQ.pop();
}
if(kWeights==k && isK) for(auto p : maxTree) cout << p.first << ' ' << p.second << '\n';
else cout << 0;
}