首页 技术 正文
技术 2022年11月13日
0 收藏 994 点赞 3,161 浏览 2145 个字

  一眼题…

  f[i][0]表示在i连接一个子树的最小值,f[i][1]表示在i连接两个子树的最小值,随便转移…

  样例挺强的1A了美滋滋…

UPD:学习了2314的写法之后短了好多T T

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=, inf=1e9;
struct poi{int too, pre;}e[maxn];
int n, T, tot, x, y;
int f[maxn][], last[maxn];
void read(int &k)
{
int f=; k=; char c=getchar();
while(c<'' || c>'') c=='-' && (f=-), c=getchar();
while(c<='' && c>='') k=k*+c-'', c=getchar();
k*=f;
}
inline void add(int x, int y){e[++tot]=(poi){y, last[x]}; last[x]=tot;}
inline int min(int a, int b){return a<b?a:b;}
void dfs(int x, int fa)
{
f[x][]=maxn; f[x][]=; int sum=;
for(int i=last[x], too;i;i=e[i].pre)
if((too=e[i].too)!=fa)
{
dfs(too, x);
f[x][]=min(f[x][]+min(f[too][], f[too][]), f[x][]+f[too][]-);
f[x][]=min(f[x][]+min(f[too][], f[too][]), sum+f[too][]);
sum+=min(f[too][], f[too][]);
}
}
int main()
{
read(T);
while(T--)
{
read(n); memset(last, , (n+)<<); tot=;
for(int i=;i<n;i++) read(x), read(y), add(x, y), add(y, x);
dfs(, ); printf("%d\n", min(f[][], f[][]));
}
}

旧代码:

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=, inf=1e9;
struct poi{int too, pre;}e[maxn];
int n, T, tot, x, y;
int f[maxn][], last[maxn];
void read(int &k)
{
int f=; k=; char c=getchar();
while(c<'' || c>'') c=='-' && (f=-), c=getchar();
while(c<='' && c>='') k=k*+c-'', c=getchar();
k*=f;
}
inline void add(int x, int y){e[++tot]=(poi){y, last[x]}; last[x]=tot;}
inline int min(int a, int b){return a<b?a:b;}
void dfs(int x, int fa)
{
int mn1=inf, mn2=inf, mni1=, mni2=, tmp=; f[x][]=; f[x][]=-;
for(int i=last[x], too;i;i=e[i].pre)
if((too=e[i].too)!=fa)
{
dfs(too, x);
f[x][]+=min(f[too][], f[too][]);
if(f[too][]==min(f[too][], f[too][])) tmp=;
if(f[too][]-min(f[too][], f[too][])<mn1) mn1=f[too][]-min(f[too][], f[too][]), mni1=too;
else if(f[too][]-min(f[too][], f[too][])<mn2) mn2=f[too][]-min(f[too][], f[too][]), mni2=too;
}
f[x][]+=tmp;
if(!(f[x][]-tmp)) {f[x][]=; f[x][]=inf; return;}
if(mn2==inf) {f[x][]=inf; return;}
for(int i=last[x], too;i;i=e[i].pre)
if((too=e[i].too)!=fa)
{
if(too==mni1 || too==mni2) f[x][]+=f[too][];
else f[x][]+=min(f[too][], f[too][]);
}
}
int main()
{
read(T);
while(T--)
{
read(n); memset(last, , (n+)<<); tot=;
for(int i=;i<n;i++) read(x), read(y), add(x, y), add(y, x);
dfs(, ); printf("%d\n", min(f[][], f[][]));
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,090
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,567
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,415
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,187
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,823
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,906