BZOJ4531: [Bjoi2014]路径
Description
Input
第一行三个整数N,M,K,表示点的数量,边的数量和走的节点数。
Output
输出一行一个整数,表示走的方法数。这个数可能比较大,你只需要输出
Sample Input
)(1*+0
1 2
1 3
1 4
2 3
3 4
2 5
3 5
3 6
4 6
5 6
Sample Output
//一共有10条路径,构成的表达式依次是101, (1), 1+1, 1+0, 1*1, 1*0, 0+0,
0+1, 0*0, 0*1
#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
inline int read() {
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
typedef long long ll;
const int mod=1000000007;
int f[25][35][35][2];//f[x][t][v] '(' - ')'
int n,k,m,e[25][25];
char s[25];
int iscal(char c) {return c=='+'||c=='-'||c=='*'||c=='/';}
int dp(int x,int t,int c,int pre) {
if(t>=k) return (!c)&&!iscal(s[x]);
int& ans=f[x][t][c][pre];
if(ans>=0) return ans;
ans=0;
rep(i,1,n) if(e[x][i]) {
if(s[i]>='0'&&s[i]<='9') {
if(s[x]>='0'&&s[x]<='9') {
if(!pre) (ans+=dp(i,t+1,c,0))%=mod;
}
else if(iscal(s[x])||s[x]=='(') (ans+=dp(i,t+1,c,s[i]=='0'))%=mod;
}
else if(s[i]=='('||s[i]==')') {
if(s[i]=='('&&(s[x]=='('||iscal(s[x]))) (ans+=dp(i,t+1,c+1,0))%=mod;
else if(s[i]==')'&&c&&(isdigit(s[x])||s[x]==')')) (ans+=dp(i,t+1,c-1,0))%=mod;
}
else {
if(s[x]>='0'&&s[x]<='9') (ans+=dp(i,t+1,c,s[i]=='0'))%=mod;
if(s[x]==')') (ans+=dp(i,t+1,c,0))%=mod;
if(s[x]=='('&&s[i]=='-') (ans+=dp(i,t+1,c,0))%=mod;
}
}
return ans;
}
int main() {
memset(f,-1,sizeof(f));
n=read();m=read();k=read();
scanf("%s",s+1);
rep(i,1,m) {
int x=read(),y=read();
e[x][y]=e[y][x]=1;
}
int ans=0;
rep(i,1,n) {
if(s[i]=='(') (ans+=dp(i,1,1,0))%=mod;
if(s[i]=='-') (ans+=dp(i,1,0,0))%=mod;
if(s[i]>='1'&&s[i]<='9') (ans+=dp(i,1,0,0))%=mod;
if(s[i]=='0') (ans+=dp(i,1,0,1))%=mod;
}
printf("%d\n",ans);
return 0;
}
BZOJ4531: [Bjoi2014]路径的更多相关文章
- BZOJ 4531: [Bjoi2014]路径
Description 一个无向图,每个节点有一个字符,问形成长度为k的的合法表达式的方案数. Sol DP. \(f[i][o][p][0/1]\) 表示走 \(i\) 步,到 \(o\) ,有 \ ...
- BZOJ4531 && BJOI2014 trace
#include<cstdio> #include<cctype> using namespace std ; struct state { int len ; int p ; ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- [BZOJ4530][Bjoi2014]大融合 LCT + 启发式合并
[BZOJ4530][Bjoi2014]大融合 试题描述 小强要在N个孤立的星球上建立起一套通信系统.这套通信系统就是连接N个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是 ...
- BZOJ_4530_[Bjoi2014]大融合_LCT
BZOJ_4530_[Bjoi2014]大融合_LCT Description 小强要在N个孤立的星球上建立起一套通信系统.这套通信系统就是连接N个点的一个树. 这个树的边是一条一条添加上去的.在某个 ...
- 洛谷 P4219 [BJOI2014]大融合 解题报告
P4219 [BJOI2014]大融合 题目描述 小强要在\(N\)个孤立的星球上建立起一套通信系统.这套通信系统就是连接\(N\)个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的 ...
- LOJ#2230. 「BJOI2014」大融合
LOJ#2230. 「BJOI2014」大融合 题目描述 小强要在$N$个孤立的星球上建立起一套通信系统.这套通信系统就是连接$N$个点的一个树.这个树的边是一条一条添加上去的. 在某个时刻,一条边的 ...
- 洛谷P4219 - [BJOI2014]大融合
Portal Description 初始有\(n(n\leq10^5)\)个孤立的点,进行\(Q(Q\leq10^5)\)次操作: 连接边\((u,v)\),保证\(u,v\)不连通. 询问有多少条 ...
- [BJOI2014]大融合(Link Cut Tree)
[BJOI2014]大融合(Link Cut Tree) 题面 给出一棵树,动态加边,动态查询通过每条边的简单路径数量. 分析 通过每条边的简单路径数量显然等于边两侧节点x,y子树大小的乘积. 我们知 ...
随机推荐
- 【转载】 JQuery.Gantt(甘特图) 开发指南
转载来自: http://www.cnblogs.com/liusuqi/archive/2013/06/09/3129293.html JQuery.Gantt是一个开源的基于JQuery库的用于实 ...
- [LeetCode] Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...
- android 入门-布局
android:gravity 针对本view 的位置. android:layout_gravity 本view相对于父布局view的位置. android:layout_alignParentRi ...
- 用C获得当前系统时间(转)
#include <stdio.h> #include <time.h> void main () { time_t rawtime; struct tm * timeinfo ...
- IIS 内部运行机制
ASP.NET是一个非常强大的构建Web应用的平台,它提供了极大的灵活性和能力以致于可以用它来构建所有类型的Web应用. 绝大多数的人只熟悉高层的框架如: WebForms 和 WebServices ...
- hdu 4597 + uva 10891(一类区间dp)
题目链接:http://vjudge.net/problem/viewProblem.action?id=19461 思路:一类经典的博弈类区间dp,我们令dp[l][r]表示玩家A从区间[l, r] ...
- barabasilab-networkScience学习笔记1-网络科学简介
第一次接触复杂性科学是在一本叫think complexity的书上,Allen博士很好的讲述了数据结构与复杂性科学,barabasi是一个知名的复杂性网络科学家,barabasilab则是他所主导的 ...
- form表单验证和事件
1.表单验证<form></form> (1).非空验证(去空格) (2).对比验证(跟一个值对比) (3).范围验证(根据一个范围进行判断) (4).固定格式验证:电话号码, ...
- linux安装配置SVN并设置钩子
安装说明 系统环境:CentOS-6.3安装方式:yum install (源码安装容易产生版本兼容的问题)安装软件:系统自动下载SVN软件 检查已安装版本 #检查是否安装了低版本的SVN 1 rpm ...
- Webstorm & PhpStorm
WebStorm注册码 User Name: EMBRACE License Key: ===== LICENSE BEGIN ===== 24718-12042010 00001h6wzKLpfo3 ...