标签:str hid code typedef eof sts iso int pen
Description
Input
Output
Sample Input
7 1 1 1 1 1 1 1 1 3 2 3 6 4 7 4 4 5 3 5 0 0
Sample Output
5
树型DP.
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<cstdlib> 7 #include<vector> 8 using namespace std; 9 typedef long long ll; 10 typedef long double ld; 11 typedef pair<int,int> pr; 12 #define rep(i,a,n) for(int i=a;i<=n;i++) 13 #define per(i,n,a) for(int i=n;i>=a;i--) 14 #define Rep(i,u) for(int i=head[u];i;i=Next[i]) 15 #define clr(a) memset(a,0,sizeof(a)) 16 #define pb push_back 17 #define mp make_pair 18 #define fi first 19 #define sc second 20 #define pq priority_queue 21 #define pqb priority_queue <int, vector<int>, less<int> > 22 #define pqs priority_queue <int, vector<int>, greater<int> > 23 #define vec vector 24 ld eps=1e-9; 25 ll pp=1000000007; 26 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;} 27 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;} 28 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); } 29 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; } 30 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1}; 31 ll read(){ ll ans=0; char last=‘ ‘,ch=getchar(); 32 while(ch<‘0‘ || ch>‘9‘)last=ch,ch=getchar(); 33 while(ch>=‘0‘ && ch<=‘9‘)ans=ans*10+ch-‘0‘,ch=getchar(); 34 if(last==‘-‘)ans=-ans; return ans; 35 } 36 const int N=10000; 37 int v[N],Next[N],head[N],dp[N][2],a[N],in[N],e; 38 void add(int x,int y){ v[++e]=y; Next[e]=head[x]; head[x]=e; } 39 void dfs(int x,int f){ 40 for (int i=head[x];i;i=Next[i]) 41 if (v[i]){ 42 dfs(v[i],x); 43 dp[x][1]+=dp[v[i]][0]; 44 dp[x][0]+=max(dp[v[i]][0],dp[v[i]][1]); 45 } 46 } 47 int main(){ 48 int n=read(); 49 for (int i=1;i<=n;i++) a[i]=read(),dp[i][1]=a[i]; 50 int a_,b_,root; 51 while (~scanf("%d%d",&a_,&b_)){ 52 if (a_==0 && b_==0) break; 53 add(b_,a_); in[a_]++; 54 } 55 for (int i=1;i<=n;i++) 56 if (!in[i]) root=i; 57 dfs(root,-1); 58 printf("%d",max(dp[root][1],dp[root][0])); 59 return 0; 60 }
标签:str hid code typedef eof sts iso int pen
原文地址:http://www.cnblogs.com/SXia/p/7638583.html