有点绕,两层拓扑排序。
有空再补详细。
甚至有点丑,因为绕,为了区分,当时变量名写得很长。
#include<cstdio> #include<algorithm> #include<cstring> #include<queue> #include<vector> #define debug printf("!") using namespace std; typedef __int64 ll; const int mod=1e9+7; const int maxn=2e4+5; const int inf=0x3f3f3f3f; int main() { int m,n,u,v,i,j,k,l,y,r,e; int K[maxn]; ll Q[maxn]={0}; vector<int> pointtextdepend[maxn][110]; vector<int> pointdepend[maxn]; vector<int> pointtextque[maxn]; vector<int> pointque; int In[maxn]={0}; int textqua[maxn][110][2]; scanf("%d%d",&n,&m); for(u=1;u<=n;u++) { int in[110]={0}; scanf("%d",&k);K[u]=k; for(i=1;i<=k;i++) { scanf("%d",&l); for(j=1;j<=l;j++) { scanf("%d",&y); pointtextdepend[u][i].push_back(y); in[y]++; } scanf("%d%d",&textqua[u][i][0],&textqua[u][i][1]); if(textqua[u][i][0]) { pointdepend[u].push_back(textqua[u][i][1]); In[textqua[u][i][1]]++; } } queue<int>q; for(i=1;i<=k;i++) if(in[i]==0)q.push(i); while(!q.empty()) { int p=q.front(); q.pop(); pointtextque[u].push_back(p); for(int i=0;i<pointtextdepend[u][p].size();i++) { y=pointtextdepend[u][p][i]; in[y]--; if(in[y]==0) q.push(y); } } } queue<int>q; for(i=1;i<=n;i++) if(In[i]==0) q.push(i); while(!q.empty()) { int p=q.front(); q.pop(); pointque.push_back(p); for(i=0;i<pointdepend[p].size();i++) { y=pointdepend[p][i]; In[y]--; if(In[y]==0) q.push(y); } } for(v=n-1;v>=0;v--) { u=pointque[v]; ll ans=0; ll textcaltime[110]={0}; for(i=K[u]-1;i>=0;i--) { ll x,y=0; if(textqua[u][pointtextque[u][i]][0])x=(Q[textqua[u][pointtextque[u][i]][1]]+1)%mod; else x=textqua[u][pointtextque[u][i]][1]; if(pointtextdepend[u][pointtextque[u][i]].size()==0) { textcaltime[pointtextque[u][i]]=x%mod; ans=max(ans,x)%mod; continue; } for(j=0;j<pointtextdepend[u][pointtextque[u][i]].size();j++) { y=max(y,(ll)textcaltime[pointtextdepend[u][pointtextque[u][i]][j]]); } textcaltime[pointtextque[u][i]]=(x+y)%mod; ans=max(ans,(x+y)%mod); } Q[u]=ans; } for(i=1;i<=m;i++) { scanf("%d",&v); printf("%I64d\n",Q[v]); } }
2019-09-06
原文地址:https://www.cnblogs.com/kkkek/p/11470637.html
时间: 2024-10-12 15:31:14