1 solutions

  • 0
    @ 2025-11-5 17:29:56

    C++ :

    #include<cstdio>
    #include<algorithm>
    
    #define MAX(a,b) ((a)>(b)?(a):(b))
    
    using namespace std;
    
    int n,m,k,father[10010],tot,ans,b[10010];
    
    struct at{
    	int x,y,w,last,k;
    }a[40010];
    
    int getf(int x)
    {
    	return x==father[x] ? x : father[x]=getf(father[x]);
    }
    
    void add(int x,int y,int w,int k)
    {
    	tot++;
    	a[tot].x=x;
    	a[tot].y=y;
    	a[tot].w=w;
    	a[tot].k=k;
    	a[tot].last=b[x];
    	b[x]=tot;
    }
    
    bool cmp(at a,at b)
    {
    	return a.w<b.w;
    }
    
    int main()
    {
    	scanf("%d%d%d",&n,&k,&m);
    	for(int i=1;i<=n;i++)	father[i]=i;
    	for(int i=1;i<m;i++){
    		int x,y,w1,w2;
    		scanf("%d%d%d%d",&x,&y,&w1,&w2);
    		add(x,y,w1,1);
    		add(x,y,w2,2);
    	}
    	sort(a+1,a+tot+1,cmp);
    	int sk=0;
    	if(k>0)
    		for(int i=1;i<=tot;i++)
    		{
    			if(a[i].k==1){
    				int x=getf(a[i].x),y=getf(a[i].y);
    				if(x==y)	continue;
    				ans=MAX(a[i].w,ans);
    				father[x]=y;
    				sk++;
    			}
    			if(sk==k)
    				break;
    		}
    	for(int i=1;i<=tot;i++){
    		int x=getf(a[i].x),y=getf(a[i].y);
    		if(x!=y){
    			father[x]=y;
    			ans=MAX(ans,a[i].w);
    			sk++;
    		}
    		if(sk==n-1)
    			break;
    	}
    	printf("%d",ans);
    	getchar(),getchar();
    	return 0;
    }
    
    
    • 1

    Information

    ID
    18487
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By