Advertisement
Guest User

Untitled

a guest
May 15th, 2012
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <vector>
  4. #include <stack>
  5. #include <queue>
  6. using namespace std;
  7. const int nmax = 100010;
  8. int N;
  9. vector < int > adj[nmax];
  10. int points[nmax],ind=1,counter=1;
  11. int lowlink[nmax],index[nmax],z,y,inds;
  12. vector < int > graph[nmax];
  13. int scc[nmax];
  14. bool fix[nmax],stackfix[nmax];
  15. int S[nmax];
  16. int ans,curans;
  17. long long pc[nmax];
  18.  
  19. int X[nmax];
  20. bool fdfs[nmax];
  21. long long answer[nmax];
  22. queue < int > que;
  23. void tarjan(int v)
  24. {
  25. index[v] = lowlink[v] = ind++;
  26. fix[v] = stackfix[v] = true;
  27. S[inds] = v;
  28. inds+=1;
  29.  
  30. for(int i = 0; i < adj[v].size(); i++)
  31. {
  32. int w = adj[v][i];
  33. if (fix[w])
  34. {
  35. if (stackfix[w])
  36. lowlink[v] = min(lowlink[v], index[w]);
  37. } else
  38. {
  39. tarjan(w);
  40. lowlink[v] = min(lowlink[v], lowlink[w]);
  41. }
  42. }
  43.  
  44. if (index[v] == lowlink[v])
  45. {
  46. int w;
  47. do
  48. {
  49. w = S[inds-1];
  50. inds-=1;
  51. stackfix[w] = false;
  52. scc[w] = counter;
  53. if(w==N) z = counter;
  54. if(w==1) y = counter;
  55. } while(w != v);
  56. ++counter;
  57. }
  58. }
  59. void dfs(int u,int z)
  60. {
  61. answer[u] = z+pc[u];
  62. fdfs[u] = true;
  63. for(int i=0;i<graph[u].size();i++)
  64. if(!fdfs[graph[u][i]])
  65. dfs(graph[u][i],answer[u]);
  66. }
  67. main()
  68. {
  69. freopen("manaobros.in","r",stdin);
  70. freopen("manaobros.out","w",stdout);
  71. int i,j,u,v,K;
  72. scanf("%d",&N);
  73. for(i=1;i<=N;i++) scanf("%d",&points[i]);
  74. for(i=1;i<N;i++)
  75. {
  76. scanf("%d",&K);
  77. for(j=0;j<K;j++)
  78. {
  79. scanf("%d",&v);
  80. if(i!=v)adj[i].push_back(v);
  81. }
  82. }
  83. for(i=1;i<=N;i++)
  84. if(!fix[i])
  85. tarjan(i);
  86. for(i=1;i<=N;i++)
  87. {
  88. pc[scc[i]]+=(long long)points[i];
  89. for(j=0;j<adj[i].size();j++)
  90. if(scc[i]!=scc[adj[i][j]] && X[scc[adj[i][j]]]!=i)
  91. {
  92. graph[scc[i]].push_back(scc[adj[i][j]]);
  93. X[scc[adj[i][j]]]=i;
  94. }
  95. }
  96.  
  97. dfs(y,0);
  98. cout << answer[z] << endl;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement