BZOJ 1063 道路设计NOI2008
http://www.lydsy.com/JudgeOnline/problem.php?id=1063
题意:给你一棵树,也有可能是不连通的,把树分成几个链,求每个点到根经过的最大链数最小,而且要输出方案数。
思路:考虑dp,f[i][j]代表第i个节点,最大链数是j,那么有
f[i][j][0]代表已经向子树连接了0个链
f[i][j][1]代表已经向子树连接了1个链
f[i][j][2]代表已经向子树连接了2个链
这样转移就是
f1=f[pur][b][0]+f[pur][b][1]
f2=f[pur][b-1][0]+f[pur][b-1][1]+f[pur][b-1][2]
f[x][b][2]=f2*f[x][b][2]+f1*f[x][b][1]
f[x][b][1]=f1*f[x][b][0]+f2*f[x][b][1]
f[x][b][0]=f[x][b][0]*f2
pur代表x的儿子
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#define ll long long
ll Mod;
int first[],next[],go[],tot;
int n,m,fa[];
ll f[][][];
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>'') {if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<='') {t=t*+ch-'';ch=getchar();}
return t*f;
}
void insert(int x,int y){
tot++;
go[tot]=y;
next[tot]=first[x];
first[x]=tot;
}
void add(int x,int y){
insert(x,y);insert(y,x);
}
int find(int x){
if (fa[x]==x) return x;else return fa[x]=find(fa[x]);
}
ll get(ll x){
if (x%Mod!=) return x%Mod;
if (x!=) return Mod;
return ;
}
void dfs(int x,int b,int fa){
f[x][b][]=;
f[x][b][]=;
f[x][b][]=;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==fa) continue;
dfs(pur,b,x);
ll f1=(f[pur][b][]+f[pur][b][]);
ll f2;
if (b) f2=f[pur][b-][]+f[pur][b-][]+f[pur][b-][];else f2=;
f[x][b][]=get(f2*f[x][b][]+f1*f[x][b][]);
f[x][b][]=get(f1*f[x][b][]+f2*f[x][b][]);
f[x][b][]=get(f[x][b][]*f2);
}
}
int main(){
n=read();m=read();Mod=read();
for (int i=;i<=n;i++) fa[i]=i;
for (int i=;i<=m;i++){
int x=read(),y=read();
add(x,y);
int p=find(x),q=find(y);
if (p!=q) fa[q]=p;
}
for (int i=;i<=n;i++)
if (find(i)!=find()){printf("-1\n-1\n");return ;}
for (int i=;;i++){
dfs(,i,);
if (f[][i][]+f[][i][]+f[][i][]){
printf("%d\n",i);
printf("%lld\n",((f[][i][]+f[][i][])%Mod+f[][i][])%Mod);
return ;
}
}
}
BZOJ 1063 道路设计NOI2008的更多相关文章
- [BZOJ]1063 道路设计(Noi2008)
省选一试后的第一篇blog! Description Z国坐落于遥远而又神奇的东方半岛上,在小Z的统治时代,公路成为这里主要的交通手段.Z国共有n座城市,一些城市之间由双向的公路所连接.非常神奇的是Z ...
- BZOJ 1063 道路设计(树形DP)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1063 题意:给出一个无环图( 也就是树,但是也有可能是森林),代表一个国家的城市.1是首 ...
- 【NOI题解】【bzoj题解】NOI2008 bzoj1063 道路设计
@ACMLCZH学长出的毒瘤题T3.再也不是“善良”的出题人了. 题意:bzoj. 题解: 经典的树形DP题目,屡见不鲜了,然而我还是没有写出来. 这一类的题目有很多,例如这里的C题. 主要套路是把对 ...
- bzoj 1064【noi2008】假面舞会
题意:http://www.lydsy.com/JudgeOnline/problem.php?id=1064 给一个有向图染色,每个点的后继必须相同,问至少&至多有多少种染色方案 sol: ...
- 【BZOJ】1061: [Noi2008]志愿者招募
题解 可能是世界上最裸的一个单纯形 (话说全幺模矩阵是啥我到现在都不知道) 假装我们已经看过了算导,或者xxx的论文,知道了单纯形是怎么实现的 扔一个blog走掉..https://www.cnblo ...
- 【BZOJ】1064: [Noi2008]假面舞会(判环+gcd+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1064 表示想到某一种情况就不敢写下去了.... 就是找环的gcd...好可怕.. 于是膜拜了题解.. ...
- 【BZOJ】1061: [Noi2008]志愿者招募(费用流+数学)
http://www.lydsy.com/JudgeOnline/problem.php?id=1061 好神的一题! 学会了一种建模方式: 当方程组内的任意变量都在其中两个方程出现且一正一负,可以建 ...
- dp专练
dp练习. codevs 1048 石子归并 区间dp #include<cstdio> #include<algorithm> #include<cstring> ...
- BZOJ1000-1099板刷计划+一句话题解 73/100
1000-1009 1000A+B Problem 这个还要写??? 1001 狼抓兔子 平面图最小割转化为对偶图最短路 #include<bits/stdc++.h> #define i ...
随机推荐
- Codeforces 596D Wilbur and Trees
http://codeforces.com/contest/596/problem/D 题目大意: 有n棵树排成一排,高度都为h. 主人公要去砍树,每次等概率地随机选择没倒的树中最左边的树或者最右边的 ...
- CCI_chapter 19 Moderate
19 1 Write a function to swap a number in place without temporary variables void swap(int &a, i ...
- Qt自定义sleep延时函数(巧妙的使用时间差,但这样似乎CPU满格,而不是沉睡)
Qt不像VC++的win32/MFC编程那样,提供了现成的sleep函数可供调用.Qt把sleep函数封装在QThread类中.子线程可以调用sleep函数.但是如果用户想在主线程实现延时功能,该怎么 ...
- 为什么Application_BeginRequest会执行两次
大家也看到了,很奇怪的是我们明明就请求了一个页面,页面中也没有其他的图片请求.为什么Application_BeginRequest会被执行了两次呢?!既然他请求,那我们看看他到底在请求什么就是 ...
- spoj1812-Longest Common Substring II(后缀自动机)
Description A string is finite sequence of characters over a non-empty finite set Σ. In this problem ...
- samsungGalaxyS4USB驱动
http://www.samsung.com/cn/support/usefulsoftware/KIES/JSP
- java多个listener监听
java 多个listener 监听方法 在class 名称上一行添加@Listeners 括号中用逗号隔开 @Listeners({com.example.MyListener.class,com. ...
- art.dialog
关闭指定弹出窗: art.dialog({ id: 'hetong' }).close(); 关闭所有的iframe弹出窗,art.dia.close();
- Hibernate试题解析
1.在Hibernate中,以下关于主键生成器说法错误的是(AC). A.increment可以用于类型为long.short或byte的主键(byte类型不可以) B.identity用于如SQL ...
- 初学者学Java设计模式(一)------单例设计模式
单例设计模式 单例设计模式是指一个类只会生成一个对象,优点是他可以确保所有对象都访问唯一实例. 具体实现代码如下: public class A { public static void main(S ...