社区讨论

坤坤塌房导致治我的评测鸡被压到房梁下,ac变mle20分

P1111修复公路参与者 2已保存回复 4

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
4 条
当前快照
1 份
快照标识符
@m0i1vg1k
此快照首次捕获于
2024/08/31 19:19
2 年前
此快照最后确认于
2025/11/04 21:55
4 个月前
查看原帖
CPP
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e3+4;
int f[MAXN];
int getroot(int n){
    if(n==f[n])return n;
    else return f[n]=getroot(f[n]);
}
void merge(int a,int b){
    f[a]=b;
    //f[b]=a;
}
struct node{
    int x,y,t;
};
bool cmp(node a,node b){
    return a.t<b.t;
}
int main(){
	//freopen("P1111_2(1).in","r",stdin);
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)f[i]=i;
    node a[m];
    for(int i=0;i<m;i++)cin>>a[i].x>>a[i].y>>a[i].t;
    sort(a,a+m,cmp);
    bool flag2=1;
    for(int i=0;i<m;i++){
        merge(a[i].x,a[i].y);
        bool flag=1;
        for(int j=1;j<=n;j++){
            if(getroot(1)!=getroot(j)){
				flag=0;
				break;
			}
        }if(flag){
            cout<<a[i].t;
            flag2=0;
            break;
        }
    }if(flag2)cout<<-1;
}

回复

4 条回复,欢迎继续交流。

正在加载回复...