Codeforces Round #302 (Div. 1 D)

Source: Internet
Author: User

Http://codeforces.ru/contest/543/problem/DProblem gives a tree, n nodes, and n-1 edges. Each point as the capital, output a result, a total of output n results. There are two forms of the side, one is good, the other is bad, and when a point is regarded as the capital, the number of bad sides in the path to each point is <=1, asking how many kinds of trees are in the form. N:10^5 level solution is obviously a tree DP. Set Dp[i] Indicates the number of sub-trees with the root of I. If only need to calculate dp[1], direct DFS again, backtracking, d P[I]=∏(d P[J ]+1) , where J is the son of I. What about the DP values for each point? With the first DFS, I can figure out the dp[i of each point], representing the number of schemes for the subtree with the root of I, and then DFS again, calculating the number of scenarios centered on I. The brain to fill the DP equation is out, do not write. Note that you cannot use the inverse, Fermat theorem a p 1 ≡1(mod  P) When gCd (a,P)≠1 , is not established, that is, a=0 There will be an accident. Think of a way to use a prefix product, suffix product, instead of the inverse element.
//hello. I ' m Peter.#pragma COMMENT (linker, "/stack:102400000,102400000")#include <cstdio>#include <iostream>#include <sstream>#include <cstring>#include <string>#include <cmath>#include <cstdlib>#include <algorithm>#include <functional>#include <cctype>#include <ctime>#include <stack>#include <queue>#include <vector>#include <set>#include <map>using namespace STD;typedef Long Longll#define Peter cout<< "I am Peter" <<endl#define INPUT Freopen ("Data.txt", "R", stdin)#define Randin srand ((unsigned int) time (NULL))#define INT (0x3f3f3f3f) * *#define LL (0x3f3f3f3f3f3f3f3f)#define MAXN#define N 200100#define Minline intRead () {intx=0, f=1;CharCh=getchar (); while(ch>' 9 '|| ch<' 0 '){if(ch=='-') f=-1; Ch=getchar ();} while(ch>=' 0 '&&ch<=' 9 ') {x=x*Ten+ch-' 0 '; Ch=getchar ();}returnX*f;}Const intMod=1e9+7;intNstructedge{intFrom,to,next;} edge[n<<1];intHead[n],num_edge;inline voidAdd_edge (intFromintTo) {intT=++num_edge;    Edge[t].from=from;    Edge[t].to=to;    Edge[t].next=head[from]; head[from]=t;} ll Dp[n],pre[n],suf[n],ans[n];inline voidDfsintNowintComfrom) {dp[now]=1; for(inti=head[now];i!=-1; i=edge[i].next) {intto=edge[i].to;if(To==comfrom)Continue;        DFS (To,now); dp[now]=dp[now]* (dp[to]+1)%mod; }}intFa[n];inline voidBFS () { Queue<int>Q Q.push (1); fa[1]=0; ans[1]=dp[1]; dp[1]=0; vector<int>Son while(!q.empty ()) {intNow=q.front ();        Q.pop (); Son.clear (); for(inti=head[now];i!=-1; i=edge[i].next) {intto=edge[i].to;if(fa[now]==to)Continue;            Fa[to]=now;        Son.push_back (to); }intLen= (int) Son.size (); ll nowv=1; for(intI=0; i<len;i++) {PRE[I]=NOWV; nowv=nowv* (dp[son[i]]+1)%mod; } nowv=1; for(inti=len-1; i>=0; i--) {SUF[I]=NOWV; nowv=nowv* (dp[son[i]]+1)%mod; }intt=0; for(inti=head[now];i!=-1; i=edge[i].next) {intto=edge[i].to;if(fa[now]==to)Continue;            ll Nowv=pre[t]*suf[t]%mod; nowv=nowv* (dp[now]+1)%mod; ans[to]=dp[to]* (nowv+1)%mod;            Q.push (to);            t++;        DP[TO]=NOWV; }    }}intMain () {n=read (); Num_edge=0; for(intI=1; i<=n;i++) {head[i]=-1; } for(intI=2; i<=n;i++) {intP=read ();        Add_edge (P,i);    Add_edge (I,P); } DFS (1,0); BFS (); for(intI=1; i<=n;i++) {if(i!=1)printf(" ");printf("%d",(int) ans[i]); }printf("\ n");return 0;}

Codeforces Round #302 (Div. 1 D)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.