比赛 |
NOIP模拟赛by mzx Day2 |
评测结果 |
AAAAAAAAAA |
题目名称 |
森林大礼包 |
最终得分 |
100 |
用户昵称 |
njx_07 |
运行时间 |
1.321 s |
代码语言 |
C++ |
内存使用 |
16.26 MiB |
提交时间 |
2016-10-20 21:43:59 |
显示代码纯文本
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <cmath>
#include <ctime>
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while (ch>'9'||ch<'0') {if (ch=='-'){f=-1;} ch=getchar();}
while (ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
int n,len=0;
int q[101000]={};
int nextt[101000][20]={};
int t[101000]={},k[101000]={};
struct edge{int y,next;}e[1001000];
int _link[101000]={};
bool flag[101000]={};
long long num[101000]={};
int maxx=0;
inline void insert(int a,int b)
{
e[++len].next=_link[a];_link[a]=len;e[len].y=b;
}
void init()
{
n=read();
memset(e,0,sizeof(e));
for (int i=1;i<=n;i++){
t[i]=read();k[i]=t[i];
for (int j=1;j<=t[i];j++) {nextt[i][j]=read();insert(nextt[i][j],i);}
}
maxx=1000000007;
}
inline void sum(int now)
{
for (int i=1;i<=t[now];i++) {num[now]+=num[nextt[now][i]];if (num[now]>maxx)num[now]%=maxx;}
}
void bfs()
{
memset(q,0,sizeof(q));
memset(num,0,sizeof(num));
q[1]=0;num[0]=1;
int head=1,tail=1;
while (head<=tail){
for (int i=_link[q[head]];i;i=e[i].next) {
num[e[i].y]+=num[q[head]];if (num[e[i].y]>maxx) num[e[i].y]%=maxx;
if (--k[e[i].y]==0) {q[++tail]=e[i].y;flag[e[i].y]=true;}
}
head++;
}
for (int i=1;i<=n;i++) if (!flag[i]) sum(i);
}
int main()
{
freopen("three_squirrels.in","r",stdin);
freopen("three_squirrels.out","w",stdout);
init();
bfs();
cout<<num[n]<<endl;
return 0;
}