这道题并不是很难,关键在于读入;
其余只需一个遍历;(考的时候傻逼兮兮的没写出来)
另外,学到了一个 isdigit()用来判断是否是0-9的数字;
#include <bits/stdc++.h> #define read read() #define up(i,l,r) for(register int i = (l);i <= (r); i++) #define re(i,u) for(register int i = head[u]; i; i = e[i].nxt) using namespace std; const int N = 10055,M = 1000055; int n,m,t[N],head[N],ans,size,vis[N]; string s; struct edge{int v,nxt;}e[M]; void add(int u,int v) {e[++size].v = v; e[size].nxt = head[u]; head[u] = size;} void dfs(int u) { ans+=t[u]; vis[u] = 1; re(i,u) {int v = e[i].v; if(!vis[v]) dfs(v);} } int main() { freopen("work.in","r",stdin); scanf("%d%d",&n,&m); up(i,1,n) { int x = 0; scanf("%d",&t[i]); getline(cin,s); up(j,0,(s.size())) { if(!isdigit(s[j])) { if(x!=0) add(i,x), x = 0; continue; } else x = x * 10 + s[j] - 48; } } dfs(m); printf("%d",ans); return 0; }