[CSUOJ1804] A direction-free graph (tree dp)

Source: Internet
Author: User

Title Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1804

Test instructions: The Chinese side, the formula is also very clear.

1e5 This is a very large number, because it is a DAG, it can be thought that the whole picture is a forest, the focus should be placed on each tree each point of the contribution of the whole problem.

Remember that each beginning U,v is the successor of U. V has multiple, credited Vi,u to VI contribution is COUNT (U,VI) *a (u) *b (vi), presents a (U), remaining count (U,vi) *b (vi), COUNT (U,VI) can be post-traversal to get, the entire result exists in DP (U), post-update the answer when the DP (u) *a (u). You can then update the B (vi) section, which is incremented to DP (U) each time, because the next visit (The father of U) must include the results of DP (U).

1 /*2 ━━━━━┒ギリギリ♂eye! 3 ┓┏┓┏┓┃キリキリ♂mind! 4 ┛┗┛┗┛┃\0/5 ┓┏┓┏┓┃/6 ┛┗┛┗┛┃ノ)7 ┓┏┓┏┓┃8 ┛┗┛┗┛┃9 ┓┏┓┏┓┃Ten ┛┗┛┗┛┃ One ┓┏┓┏┓┃ A ┛┗┛┗┛┃ - ┓┏┓┏┓┃ - ┃┃┃┃┃┃ the ┻┻┻┻┻┻ - */ -#include <algorithm> -#include <iostream> +#include <iomanip> -#include <cstring> +#include <climits> A#include <complex> at#include <fstream> -#include <cassert> -#include <cstdio> -#include <bitset> -#include <vector> -#include <deque> in#include <queue> -#include <stack> to#include <ctime> +#include <Set> -#include <map> the#include <cmath> * using namespacestd; $ #defineFr FirstPanax Notoginseng #defineSC Second - #defineCL Clear the #defineBUG puts ("Here!!!") + #defineW (a) while (a--) A #definePB (a) push_back (a) the #defineRint (a) scanf ("%d", &a) + #defineRll (a) scanf ("%i64d", &a) - #defineRs (a) scanf ("%s", a) $ #defineCIN (a) CIN >> a $ #defineFRead () freopen ("in", "R", stdin) - #defineFWrite () freopen ("Out", "w", stdout) - #defineRep (i, Len) for (int i = 0; i < (len); i++) the #defineFor (I, A, Len) for (int i = (a); I < (len); i++) - #defineCls (a) memset ((a), 0, sizeof (a))Wuyi #defineCLR (A, X) memset ((a), (x), sizeof (a)) the #defineFull (a) memset ((a), 0x7f7f7f, sizeof (a)) - #defineLRT RT << 1 Wu #defineRRT RT << 1 | 1 - #definePi 3.14159265359 About #defineRT return $ #defineLowbit (x) x & (-X) - #defineONECNT (x) __builtin_popcount (x) -typedefLong LongLL; -typedefLong DoubleLD; Atypedef unsignedLong LongULL; +typedef pair<int,int>PII; thetypedef pair<string,int>psi; -typedef PAIR&LT;LL, Ll>PLL; $typedef map<string,int>MSI; thetypedef vector<int>VI; thetypedef vector<ll>VL; thetypedef vector<vl>VVL; thetypedef vector<BOOL>vb; -  in Const intMAXN =100100; the ConstLL mod = 1e9+7; the intN, M; About intA[MAXN], B[MAXN]; the VI G[MAXN]; the BOOLVIS[MAXN]; the LL DP[MAXN]; + LL ret; -  the voidDfsintu) {Bayi Rep (i, g[u].size ()) { the         intv =G[u][i]; the         if(!Vis[v]) { -VIS[V] =1; - Dfs (v); the         } theDp[u] = (Dp[u] + dp[v])%MoD; the     } theRET = (ret + (dp[u] * a[u]% MoD))%MoD; -Dp[u] = (Dp[u] + b[u])%MoD; the } the  the intMain () {94     //FRead (); the     intu, v; the      while(~rint (n) &&~Rint (M)) { theRET =0; Cls (VIS); Cls (DP);98for (I,1, n+1) { About Rint (A[i]), Rint (B[i]); - g[i].clear ();101         }102 Rep (i, m) {103 Rint (U); Rint (v);104 G[U].PB (v); the         }106for (I,1, n+1) {107             if(!Vis[i]) DFS (i);108         }109for (I,1, n+1) cout << Dp[i] <<" "; thecout <<Endl;111cout << ret <<Endl; the     }113Rt0; the}

[CSUOJ1804] A direction-free graph (tree dp)

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.