zoukankan      html  css  js  c++  java
  • [APIO2008]免费道路

    [APIO2008]免费道路

    BZOJ
    luogu
    先把必须连的鹅卵石路连上,大于k条no solution
    什么样的鹅卵石路(u,v)必须连?所有水泥路都连上仍然不能使u,v连通的必须连
    补全到k条鹅卵石路,补不全no solution
    最后用水泥路构出生成树,构不出no solution

    #include<bits/stdc++.h>
    using namespace std;
    const int _=1e5+5;
    int re(){
    	int x=0,w=1;char ch=getchar();
    	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
    	while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    	return x*w;
    }
    bool vis[_];
    int n,m,k,cnt,fa[_],ans[_];
    struct edge{int u,v,c;}e[_];
    int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
    void unite(int x,int y){
    	x=find(x);y=find(y);
    	if(x^y)fa[x]=y;
    }
    int main(){
    	n=re(),m=re(),k=re();
    	for(int i=1;i<=n;i++)fa[i]=i;
    	for(int i=1;i<=m;i++){
    		int u=re(),v=re(),c=re();
    		e[i]=(edge){u,v,c};
    		if(c)unite(u,v);
    	}
    	for(int i=1;i<=m;i++){
    		if(find(e[i].u)^find(e[i].v)){
    			unite(e[i].u,e[i].v);
    			ans[++cnt]=i;vis[i]=1;
    		}
    	}
    	if(cnt>k){puts("no solution");return 0;}
    	for(int i=1;i<=n;i++)fa[i]=i;
    	for(int i=1;i<=cnt;i++)
    		unite(e[ans[i]].u,e[ans[i]].v);
    	for(int i=1;i<=m;i++){
    		if(e[i].c||vis[i])continue;
    		if(find(e[i].u)^find(e[i].v)){
    			unite(e[i].u,e[i].v);
    			ans[++cnt]=i;
    			if(cnt==k)break;
    		}
    	}
    	if(cnt<k){puts("no solution");return 0;}
    	for(int i=1;i<=m;i++){
    		if(!e[i].c)continue;
    		if(find(e[i].u)^find(e[i].v)){
    			unite(e[i].u,e[i].v);
    			ans[++cnt]=i;
    			if(cnt==n-1)break;
    		}
    	}
    	if(cnt<n-1){puts("no solution");return 0;}
    	for(int i=1;i<n;i++)
    		printf("%d %d %d
    ",e[ans[i]].u,e[ans[i]].v,e[ans[i]].c);
    	return 0;
    }
    
  • 相关阅读:
    JavaScript数组API
    爱上经典之王梦麟《阿美阿美》
    爱上经典之《蜗牛与黄鹂鸟》
    爱上经典之《兰花草》
    爱上经典之《让我们看云去》
    爱上经典之卓依婷《三月里的小雨》
    爱上经典之孟庭苇《冬季到台北来看雨》
    有故事看SQA作用
    转自scmlife趣谈质量管理与工程改进面试
    Mysql之批处理
  • 原文地址:https://www.cnblogs.com/sdzwyq/p/9898161.html
Copyright © 2011-2022 走看看