5 7 2
1 3 0
4 5 1
3 2 0
5 3 1
4 3 0
1 2 1
4 2 1
标签:生成树 algo can pen 需要 define splay col 必须
1 #include <iostream> 2 #include <cstdlib> 3 #include <cstdio> 4 #include <cstring> 5 #include <queue> 6 #include <algorithm> 7 #include <cmath> 8 #include <map> 9 #define N 20005 10 #define M 100005 11 using namespace std; 12 int n,m,t,fa[N]; 13 struct ro 14 { 15 int to,from,l; 16 bool bj; 17 }road[M]; 18 bool px1(ro a,ro b) 19 { 20 return a.l>b.l; 21 } 22 bool px2(ro a,ro b) 23 { 24 return a.l<b.l; 25 } 26 int find(int x) 27 { 28 if(fa[x]==x)return x; 29 return fa[x]=find(fa[x]); 30 } 31 void hb(int x,int y) 32 { 33 int a=find(x),b=find(y); 34 fa[a]=b; 35 } 36 int ans[N]; 37 int main() 38 { 39 scanf("%d%d%d",&n,&m,&t); 40 int sum=0; 41 for(int i=1;i<=n;i++)fa[i]=i; 42 for(int i=1;i<=m;i++) 43 { 44 scanf("%d%d%d",&road[i].from,&road[i].to,&road[i].l); 45 if(!road[i].l)sum++; 46 } 47 sort(road+1,road+1+m,px1); 48 int js1=0,js2=0; 49 for(int i=1;i<=m;i++) 50 { 51 int x=road[i].from,y=road[i].to; 52 if(find(x)!=find(y)) 53 { 54 js1++; 55 if(!road[i].l) 56 { 57 road[i].bj=1; 58 js2++; 59 } 60 hb(x,y); 61 } 62 if(js1==n-1)break; 63 } 64 if(js1!=n-1||js2>t) 65 { 66 printf("no solution\n"); 67 exit(0); 68 } 69 sort(road+1,road+1+m,px2); 70 js1=0,js2=0; 71 for(int i=1;i<=n;i++)fa[i]=i; 72 for(int i=1;i<=sum;i++) 73 { 74 if(road[i].bj) 75 { 76 int x=road[i].from,y=road[i].to; 77 hb(x,y); 78 js2++; 79 js1++; 80 ans[js1]=i; 81 } 82 } 83 for(int i=1;i<=m;i++) 84 { 85 int x=road[i].from,y=road[i].to; 86 if(find(x)!=find(y)) 87 { 88 if(!road[i].l) 89 { 90 js2++; 91 } 92 js1++; 93 ans[js1]=i; 94 hb(x,y); 95 } 96 if(js2==t)i=sum,js2=-1; 97 } 98 if(js2!=-1) 99 { 100 printf("no solution\n"); 101 exit(0); 102 } 103 for(int i=1;i<=n-1;i++) 104 { 105 printf("%d %d %d\n",road[ans[i]].from,road[ans[i]].to,road[ans[i]].l); 106 } 107 return 0; 108 }
Bzoj 3624: [Apio2008]免费道路 (贪心+生成树)
标签:生成树 algo can pen 需要 define splay col 必须
原文地址:http://www.cnblogs.com/liutianrui/p/7652760.html