hdu 5452(树链刨分)
看到题目,想了挺长时间,发现不会,然后看着样子像是树上成段操作,所以查了下树链刨分,结果真的就是这个东西。。。
Minimum Cut
Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)
Total Submission(s): 453 Accepted Submission(s): 180
We say that a cut in G respects T if it cuts just one edges of T.
Since love needs good faith and hypocrisy return for only grief, you should find the minimum cut of graph G respecting the given spanning tree T.
The first line of the input is a single integer t (1≤t≤5) which is the number of test cases.
Then t test cases follow.
Each test case contains several lines.
The first line contains two integers n (2≤n≤20000) and m (n−1≤m≤200000).
The following n−1 lines describe the spanning tree T and each of them contains two integers u and v corresponding to an edge.
Next m−n+1 lines describe the undirected graph G and each of them contains two integers u and v corresponding to an edge which is not in the spanning tree T.
4 5
1 2
2 3
3 4
1 3
1 4
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define N 20020 int n,m;
struct node
{
int to,next;
}edge[2*N]; int pre[N],cnt;
int siz[N],son[N],top[N],dep[N],fa[N],w[N];
int index;
int cnt1[N]; void add_edge(int u,int v)
{
edge[cnt].to=v;
edge[cnt].next=pre[u];
pre[u]=cnt++;
} void dfs(int s,int deep,int father)
{
dep[s]=deep;
siz[s]=1;
fa[s]=father;
int mx=-1;
son[s]=0;
for(int p=pre[s];p!=-1;p=edge[p].next)
{
int v=edge[p].to;
if(v==father) continue;
dfs(v,deep+1,s);
if(siz[v]>mx)
{
mx=siz[v];
son[s]=v;
}
siz[s]+=siz[v];
}
} void dfs1(int s,int father)
{
if(son[s]!=0)
{
w[ son[s] ]=++index;
top[ son[s] ]=top[s];
dfs1(son[s],s);
}
for(int p=pre[s];p!=-1;p=edge[p].next)
{
int v=edge[p].to;
if(v==father||v==son[s]) continue;
w[v]=++index;
top[v]=v;
dfs1(v,s);
}
} int Scan() //输入外挂
{
int res=0,ch,flag=0;
if((ch=getchar())=='-')
flag=1;
else if(ch>='0'&&ch<='9')
res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')
res=res*10+ch-'0';
return flag?-res:res;
} void Out(int a)//输出外挂
{
if(a>9)
Out(a/10);
putchar(a%10+'0');
} int in()
{
int flag = 1;
char ch;
int a = 0;
while((ch = getchar()) == ' ' || ch == '\n');
if(ch == '-') flag = -1;
else
a += ch - '0';
while((ch = getchar()) != ' ' && ch != '\n')
{
a *= 10;
a += ch - '0';
}
return flag * a;
} //你卡这个复杂度真的好吗,不过也怪自己学的少。 int main()
{
int T;
scanf("%d",&T);
int tt=1;
while(T--)
{
memset(pre,-1,sizeof(pre));
cnt=0;
scanf("%d%d",&n,&m);
for(int i=0;i<n-1;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add_edge(x,y);
add_edge(y,x);
}
//然后就是进行树链刨分
dfs(1,1,1);
top[1]=1;
index=0;
w[1]=0;
dfs1(1,-1); memset(cnt1,0,sizeof(cnt1)); for(int i=n-1;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y); while(top[x]!=top[y])
{
if( top[x] > top[y] )
{
cnt1[w[top[x]] ]++;
cnt1[w[x]+1]--;
x = fa[ top[x] ];
}
else
{
cnt1[ w[ top[y] ] ]++;
cnt1[ w[y]+1 ]--;
y = fa[ top[y] ];
}
}
if(dep[x] < dep[y])
{
cnt1[ w[son[x] ] ]++;
cnt1[ w[y]+1 ]--;
}
else if(dep[x] >dep[y] )
{
cnt1[w[son[y]] ]++;
cnt1[w[x]+1 ]--;
} } int ans=10000000;
for(int i=1;i<=index;i++)
{
cnt1[i]+=cnt1[i-1];
ans=min(ans,cnt1[i]);
}
printf("Case #%d: ",tt++);
printf("%d\n",ans+1);
}
return 0;
}
hdu 5452(树链刨分)的更多相关文章
- HDU - 3966 树链刨分
题目传送门 操作就是询问某个点的值, 然后就是对一条路径上的值全部修改. 最基本的树刨题目了. 树刨的思想: 1. 对于每个点找到他的重儿子. void dfs1(int o, int u){ sz[ ...
- bzoj 5210(树链刨分下做个dp)
5210: 最大连通子块和 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 211 Solved: 65[Submit][Status][Discus ...
- xcoj 1103 插线板(树链刨分求最大子段和)
1103: 插线板 时间限制: 1 Sec 内存限制: 128 MB提交: 14 解决: 7 标签提交统计讨论版EditTestData 题目描述 从前有一堆古老的插线板,任意两个插线板之间只有一 ...
- 树链刨分(class版)
class版树链剖(刨)分 感谢沙华大佬的赞助 其实没什么太大变化,就是用了几次一顿乱指... CODE: #include<iostream> #include<cstdio> ...
- hdu 3804树链剖分+离线操作
/* 树链刨分+离线操作 题意:给你一棵树,和询问x,y 从节点x--节点1的小于等于y的最大值. 解:先建一个空树,将树的边权值从小到大排序,将询问y按从小到大排序 对于每次询问y将小于等于y的边权 ...
- HDU 3966 Aragorn's Story 树链拋分
一.写在前面 终于开始开坑link-cut-tree这个了,对于网上找到的大佬的前进路线,进行了一番研发,发现实际上可以实现对于树链拋分的制作.经历了若干长时间之后终于打了出来(为什么每次学什么东西都 ...
- hdu 5893 (树链剖分+合并)
List wants to travel Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/O ...
- hdu 5052 树链剖分
Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- hdu 4897 树链剖分(重轻链)
Little Devil I Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
随机推荐
- Bitnami 2015
WordPress WordPress is one of the world's most popular web publishing platforms for building blogs a ...
- [TypeScript] Use the TypeScript "unknown" type to avoid runtime errors
The "any" type can be very useful, especially when adding types to an existing JavaScript ...
- Rails时间扩展方法
1.时间扩展方法: beginning_of_day,end_of_day等等 Time.now.end_of_day 2.ruby类型判断 is_a?(类型) 如:b.is_a?(Array)
- nmap小技巧[1] 探测大网络空间中的存活主机
url: nmap是所有安全爱好者应该熟练掌握的扫描工具,本篇介绍其在扫描大网络空间时的用法. 为什么要扫描大网络空间呢? 有这样的情形: 内网渗透 攻击者单点突破,进入内网后,需进一步扩大成果, ...
- 配置 mybatis的 log4j.properties
log4j.rootLogger=debug,stdout,logfile ### 把日志信息输出到控制台 ### log4j.appender.stdout=org.apache.log4j.Con ...
- JUnit4.8.2源码分析-1 说明
阅读本系列文章时须要知道的: JUnit是由GOF 之中的一个的Erich Gamma和 Kent Beck 编写的一个开源的单元測试框架,分析JUnit源码的主要目的是学习当中对设计模式的运用.JU ...
- 切换默认Activity和Fragment的动画
Activity中 public void click(View view){ Intent intent = new Intent(); intent.setClass(this, TwoActiv ...
- 使用sql语句实现添加、删除约束
--主键约束(Primary Key constraint):要求主键列的数据唯一,并且不允许为空. --唯一约束(Unique Constraint):要求该列唯一,允许为空,但只能出现一个空值. ...
- jQuery find() 搜索所有段落中的后代 C# find() 第一个匹配元素 Func 有返回值 Action是没有返回值 Predicate 只有一个参数且返回值为bool 表达式树Expression
所有p后代span Id为 TotalProject 的 select 标签 的后代 option标签 为选中的 text using System; using System.Collections ...
- windows服务器下配置memcache最大内存值
windows服务器下修改memcache配置要求如下: 1.用内网ip的方式提供web应用服务器调用,不允许直接通过外网调用,如将memcache服务放在192.168.1.168的服务器上 2.修 ...