SDOJ 3696 Tree
描述
给你一个无向带权连通图,每条边是黑色或白色。让你求一棵最小权的恰好有need条白色边的生成树。 题目保证有解。
输入
第一行V,E,need分别表示点数,边数和需要的白色边数。
接下来E行
每行s,t,c,col表示这边的端点(点从0开始标号),边权,颜色(0白色1黑色)
输出
一行表示所求生成树的边权和。
样例输入
0 1 1 1
0 1 2 0
样例输出
数据规模和约定
0%:V<=10
30%:V<=15
100%:V<=50000,E<=100000
所有数据边权为[1,100]中的正整数。
.......................
太困了!!!!!!
先贴个代码 明天再写思路
耶~
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 50010
using namespace std;
int n,m,k;
int first[N],cnt;
int a[N*],b[N*],c[N*],d[N*];
int fa[N],sum;
struct node
{
int u,v,w,nxt;
int col;
}e[N*];
void ade(int x,int y,int z,int c,int i)
{
e[i].u=x;
e[i].v=y;
e[i].w=z;
e[i].col=c;
}
bool cmp(const node &p,const node &q)
{
if(p.w!=q.w)
return p.w<q.w;
return p.col<q.col;
}
int la(int x)
{
if(fa[x]!=x) fa[x]=la(fa[x]);
return fa[x];
}
bool kruskal(int x)
{
for(int i=;i<n;i++) fa[i]=i;
for(int i=;i<=m;i++)
{
ade(a[i],b[i],c[i],d[i],i);
if(d[i]==) e[i].w+=x;
}
sort(e+,e+m+,cmp);
sum=;
int num=,ans=;
for(int i=;i<=m;++i)
{
if(la(e[i].u)!=la(e[i].v))
{
num++;
sum+=e[i].w;
fa[la(e[i].u)]=la(e[i].v);
if(e[i].col==) ans++;
}
if(num==n-) break;
}
if(ans>=k)
return true;
return false;
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=m;i++)
scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
int l=-,r=,mid;
while(l<r)
{
mid=(l+r+)>>;
if(kruskal(mid)) l=mid;
else r=mid-;
}
kruskal(l);
printf("%d",sum-k*l);
return ;
}
SDOJ 3696 Tree的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
- 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- Game Engine Architecture
- nginx只允许域名访问网址,禁止ip访问
修改nginx配置 文件 在server段里插入如下正则: if ( $host != 'www.baidu.com') { return 403; } 说明:如果访问讨还不是www.baidu.co ...
- jsp之数据提交与获取(传统方法)
package com.java.model; public class Student { private String name; private int age; public String g ...
- LibreOJ #2037. 「SHOI2015」脑洞治疗仪
线段树区间合并问题 恶心... 屠龙宝刀点击就送 #include <cstdio> #define N 200005 struct Segment { int l,r,mid,sum,l ...
- 《spss统计分析与行业应用案例详解》:实例十二 卡方检验
卡方检验的功能与意义 SPSS的卡方检验是非参数检验方法的一种,其基本功能足通过样本的 频数分布来推断总体是否服从某种理论分布或某种假设分布,这种检验过程是通过分析实际的频数与理论的频数之间的差别或是 ...
- UI Testing in Xcode 7
参考文章: UI Testing in Xcode - WWDC 2015https://developer.apple.com/videos/play/wwdc2015-406/ Document ...
- jq 下拉框
<div class="alls"> <div class="item"> <div class="all"& ...
- Mutations-freecodecamp算法题目
Mutations(比较字符串) 要求 如果数组第一个字符串元素包含了第二个字符串元素的所有字符,函数返回true. 不用考虑大小写和字符顺序 思路 将数组中的两个字符串小写化 将第二个数组元素(第二 ...
- shopnc路由功能分析
项目核心文件 core/shopld.php if (!@include(BASE_DATA_PATH.'/config/config.ini.php')) exit('config.ini.php ...
- Qt的由来和发展
一.Qt的由来 Haavard Nord 和Eirik Chambe-Eng于1991年开始开发"Qt",1994年3月4日创立公司,早名为Quasar Technologies, ...