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
| #include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 110000;
int n,m,k;
struct Edge{
int from,to,len;
}edge[MAXN];
bool cmp(Edge a,Edge b){
return a.len < b.len;
}
int f[MAXN];
void init(int n){
for(int i = 1;i<=n;i++) f[i] = i;
}
int find(int x){
return f[x] == x?x:f[x] = find(f[x]);
}
void solve(){
static Edge ans[MAXN];
int tot = 0;
init(n);
sort(edge+1,edge+m+1,cmp);
for(int i = 1;i<=m;i++){
Edge &e = edge[i];
int fx = find(e.from),fy = find(e.to);
if(fx != fy){
f[fx] = fy;
if(e.len == 1){
tot++;
ans[tot] = e;
}
}
if(e.len == 1){
e.len = -1;
}
}
init(n);
sort(edge+1,edge+m+1,cmp);
if(tot > k){
printf("no solution\n");
return;
}
for(int i = 1;i<=tot;i++){
int fx = find(ans[i].from),fy = find(ans[i].to);
//printf("%d %d\n",ans[i].from,ans[i].to);
f[fx] = fy;
}
for(int i = 1;i<=m;i++){
Edge &e = edge[i];
if(tot == n-1) break;
int fx = find(e.from),fy = find(e.to);
if(tot == k && e.len == -1) continue;
if(tot < k && e.len != -1){
printf("no solution\n");
return;
}
if(fx != fy){
f[fx] = fy;
ans[++tot] = e;
}
}
if(tot!=n-1){
printf("no solution\n");
return;
}
for(int i = 1;i<=n-1;i++){
printf("%d %d %d\n",ans[i].from,ans[i].to,1-abs(ans[i].len));
}
}
void init(){
scanf("%d %d %d",&n,&m,&k);
for(int i = 1;i<=m;i++){
scanf("%d %d %d",&edge[i].from,&edge[i].to,&edge[i].len);
edge[i].len^=1;
}
}
int main(){
init();
solve();
return 0;
}
|