72分蒟蒻第四个点和第五个点没过
查看原帖
72分蒟蒻第四个点和第五个点没过
648772
Liyuqiao11楼主2023/8/21 18:58
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
#define int long long
int n,m,k,fa[N],ans[N],tot,vis[N],dc;
struct E{
    int x;
    int y;
    int w;
}a[N];
bool cmp(E b,E c){
    return b.w<c.w;
}
bool cmp2(E b,E c){
    return b.w>c.w;
}
int find(int x){
    if(fa[x]==x) return x;
    else return fa[x]=find(fa[x]);
}
bool check(){
    for(int i=1;i<=n;i++){
        if(find(1)!=find(i)){
            return false;
        }
    }
    return true;
}
signed main(){
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=1;i<=m;i++){
        cin>>a[i].x>>a[i].y>>a[i].w;
        a[i].w=a[i].w+1;
    }
    sort(a+1,a+m+1,cmp2);
    for(int i=1;i<=m;i++){
        int fx=find(a[i].x),fy=find(a[i].y);
        if(fx!=fy){
            fa[fx]=fy;
            if(a[i].w==1){
                a[i].w=0;
                tot++;
            }
        }
    }
    if(!check()||tot>k){
        cout<<"no solution"<<endl;
        exit(0);
    }
    for(int i=1;i<=n;i++) fa[i]=i;
    sort(a+1,a+m+1,cmp);
    tot=0;
    for(int i=1;i<=tot;i++){
        int fx=find(a[i].x),fy=find(a[i].y);
        if(fx!=fy){
            fa[fx]=fy;
            ans[++tot]=i;
        }
    }
    for(int i=tot+1;i<=m;i++){
        int fx=find(a[i].x),fy=find(a[i].y);
        if(fx!=fy){
            if(a[i].w==1&&tot<k){
                fa[fx]=fy;
                ans[++tot]=i;
            }
            else if(a[i].w==2){
                fa[fx]=fy;
                dc++;
                ans[tot+dc]=i;
            }
        }
    }
    if(tot<k||!check()){
        cout<<"no solution"<<endl;
        exit(0);
    }
    for(int i=1;i<=n-1;i++){
        if(a[ans[i]].w>=1) cout<<a[ans[i]].x<<" "<<a[ans[i]].y<<" "<<a[ans[i]].w-1<<endl;
        else cout<<a[ans[i]].x<<" "<<a[ans[i]].y<<" "<<a[ans[i]].w<<endl;
    }
    return 0;
}
2023/8/21 18:58
加载中...