免费道路

【输入样例】

5 7 2
1 3 0
4 5 1
3 2 0
5 3 1
4 3 0
1 2 1
4 2 1

【输出样例】

3 2 0
4 3 0
5 3 1
1 2 1


题解:

题意即为求一棵刚好拥有k条鹅卵石路的生成树

那么我们先将所有水泥路加入图中

就可以知道必须要加入的鹅卵石路

将这些边加入新树中

接下来再随意地按树的结构加入至k条鹅卵石路

并再更加随意按树结构加水泥路至连通

那么就得到了合法方案

判断过程中无解的情况:

1.所有边加入都无法连通

2.鹅卵石路不足k条

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
const int me = ;
struct shape
{
int x, y, z;
};
shape a[me], ans[me];
int tot, num, cnt;
int n, m, k;
int fat[me];
inline int Get()
{
int x = ;
char c = getchar();
while('' > c || c > '') c = getchar();
while('' <= c && c <= '')
{
x = (x << ) + (x << ) + c - '';
c = getchar();
}
return x;
}
int Find(int x)
{
return (fat[x] != x) ? fat[x] = Find(fat[x]) : x;
}
int main()
{
n = Get(), m = Get(), k = Get();
for(int i = ; i <= n; ++i) fat[i] = i;
for(int i = ; i <= m; ++i)
{
a[i].x = Get(), a[i].y = Get(), a[i].z = Get();
if(a[i].z)
{
int u = Find(a[i].x);
int v = Find(a[i].y);
if(u != v) fat[u] = v, ++cnt;
}
}
for(int i = ; i <= m; ++i)
if(!a[i].z)
{
int u = Find(a[i].x);
int v = Find(a[i].y);
if(u != v)
{
fat[u] = v;
ans[++tot] = a[i];
}
}
if(cnt + tot != n - )
{
printf("no solution\n");
return ;
}
for(int i = ; i <= n; ++i) fat[i] = i;
for(int i = ; i <= tot; ++i)
{
int u = Find(ans[i].x);
int v = Find(ans[i].y);
if(u != v) fat[u] = v;
}
num = tot;
if(num != k)
for(int i = ; i <= m; ++i)
if(!a[i].z)
{
int u = Find(a[i].x);
int v = Find(a[i].y);
if(u != v)
{
++num;
fat[u] = v;
ans[++tot] = a[i];
if(num == k) break;
}
}
if(num != k)
{
printf("no solution\n");
return ;
}
for(int i = ; i <= m; ++i)
if(a[i].z)
{
int u = Find(a[i].x);
int v = Find(a[i].y);
if(u != v)
{
fat[u] = v;
ans[++tot] = a[i];
if(tot == n - ) break;
}
}for(int i = ; i <= tot; ++i)
printf("%d %d %d\n", ans[i].x, ans[i].y, ans[i].z);
}

最新文章

  1. PV 与 并发数 之间的故事
  2. Leetcode Find K Pairs with smallest sums
  3. 微信支付官方.net版之坑你没商量
  4. hdu 1005 简单题
  5. discuz X2.0教程]教你快速了解Discuz!程序文件功能,修改文件从此不用再求人
  6. kindeditor-4.1.10 结合 Asp.Net MVC 添加图片功能
  7. js自定义排序
  8. External file changes sync may be slow: Project files cannot be watched (are they under network mount?)
  9. .net生成随机验证码图片
  10. python 文件系统
  11. windbg命令学习2
  12. lua 安装配置
  13. Python编程预约参观北京行动纲要
  14. Android Parcelable理解与使用(对象序列化)
  15. jquery ui-----弹出窗口 dialog
  16. Tomcat 改端口
  17. go标准库的学习-path/filepath
  18. Confluence 6 避免和清理垃圾
  19. Linux TCP并发请求溺出 调优
  20. tomcat的调优管理

热门文章

  1. 异步任务队列Celery在Django中的使用
  2. Win10 IIS本地部署MVC网站时不能运行?
  3. ASP.NET MVC5+EF6+EasyUI 后台管理系统(64)-补充WebApi与Unity注入-配置文件
  4. 算法与数据结构(十六) 快速排序(Swift 3.0版)
  5. .NET应用程序域
  6. autocomplete的使用
  7. win7下利用ftp实现华为路由器的上传和下载
  8. Mysql - 查询之关联查询
  9. SpringMVC入门
  10. 【腾讯Bugly经验分享】程序员的成长离不开哪些软技能?