zoukankan      html  css  js  c++  java
  • [CSUOJ1804]有向无环图(树dp)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1804

    题意:中文题面,那个式子也很清楚。

    1e5这个点数量很大了,由于是个DAG,可以认为整张图是一个森林,关注点应该放在每一棵树上的每一个点对整题的贡献。

    记每一个起点u,v是u的后继。v有多个,记作vi,u到vi的贡献是count(u,vi)*a(u)*b(vi),提出a(u),剩下count(u,vi)*b(vi),count(u,vi)可以后序遍历得到,整个结果存在dp(u)中,后序更新答案时dp(u)*a(u)即可。随后可以更新b(vi)部分,每次累加到dp(u)上,因为下一次的访问(u的父亲)必定包括了dp(u)的结果。

      1 /*
      2 ━━━━━┒ギリギリ♂ eye!
      3 ┓┏┓┏┓┃キリキリ♂ mind!
      4 ┛┗┛┗┛┃\○/
      5 ┓┏┓┏┓┃ /
      6 ┛┗┛┗┛┃ノ)
      7 ┓┏┓┏┓┃
      8 ┛┗┛┗┛┃
      9 ┓┏┓┏┓┃
     10 ┛┗┛┗┛┃
     11 ┓┏┓┏┓┃
     12 ┛┗┛┗┛┃
     13 ┓┏┓┏┓┃
     14 ┃┃┃┃┃┃
     15 ┻┻┻┻┻┻
     16 */
     17 #include <algorithm>
     18 #include <iostream>
     19 #include <iomanip>
     20 #include <cstring>
     21 #include <climits>
     22 #include <complex>
     23 #include <fstream>
     24 #include <cassert>
     25 #include <cstdio>
     26 #include <bitset>
     27 #include <vector>
     28 #include <deque>
     29 #include <queue>
     30 #include <stack>
     31 #include <ctime>
     32 #include <set>
     33 #include <map>
     34 #include <cmath>
     35 using namespace std;
     36 #define fr first
     37 #define sc second
     38 #define cl clear
     39 #define BUG puts("here!!!")
     40 #define W(a) while(a--)
     41 #define pb(a) push_back(a)
     42 #define Rint(a) scanf("%d", &a)
     43 #define Rll(a) scanf("%I64d", &a)
     44 #define Rs(a) scanf("%s", a)
     45 #define Cin(a) cin >> a
     46 #define FRead() freopen("in", "r", stdin)
     47 #define FWrite() freopen("out", "w", stdout)
     48 #define Rep(i, len) for(int i = 0; i < (len); i++)
     49 #define For(i, a, len) for(int i = (a); i < (len); i++)
     50 #define Cls(a) memset((a), 0, sizeof(a))
     51 #define Clr(a, x) memset((a), (x), sizeof(a))
     52 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
     53 #define lrt rt << 1
     54 #define rrt rt << 1 | 1
     55 #define pi 3.14159265359
     56 #define RT return
     57 #define lowbit(x) x & (-x)
     58 #define onecnt(x) __builtin_popcount(x)
     59 typedef long long LL;
     60 typedef long double LD;
     61 typedef unsigned long long ULL;
     62 typedef pair<int, int> pii;
     63 typedef pair<string, int> psi;
     64 typedef pair<LL, LL> pll;
     65 typedef map<string, int> msi;
     66 typedef vector<int> vi;
     67 typedef vector<LL> vl;
     68 typedef vector<vl> vvl;
     69 typedef vector<bool> vb;
     70 
     71 const int maxn = 100100;
     72 const LL mod = 1e9+7;
     73 int n, m;
     74 int a[maxn], b[maxn];
     75 vi G[maxn];
     76 bool vis[maxn];
     77 LL dp[maxn];
     78 LL ret;
     79 
     80 void dfs(int u) {
     81     Rep(i, G[u].size()) {
     82         int v = G[u][i];
     83         if(!vis[v]) {
     84             vis[v] = 1;
     85             dfs(v);
     86         }
     87         dp[u] = (dp[u] + dp[v]) % mod;
     88     }
     89     ret = (ret + (dp[u] * a[u] % mod)) % mod;
     90     dp[u] = (dp[u] + b[u]) % mod;
     91 }
     92 
     93 int main() {
     94     // FRead();
     95     int u, v;
     96     while(~Rint(n)&&~Rint(m)) {
     97         ret = 0; Cls(vis); Cls(dp);
     98         For(i, 1, n+1) {
     99             Rint(a[i]), Rint(b[i]);
    100             G[i].clear();
    101         }
    102         Rep(i, m) {
    103             Rint(u); Rint(v);
    104             G[u].pb(v);
    105         }
    106         For(i, 1, n+1) {
    107             if(!vis[i]) dfs(i);
    108         }
    109         For(i, 1, n+1) cout << dp[i] << " ";
    110         cout << endl;
    111         cout << ret << endl;
    112     }
    113     RT 0;
    114 }
  • 相关阅读:
    oracle 插入timestamp
    ?--Porg.springframework.beans.MethodInvocationException: Property 'username' threw exception; nested exception is java.lang.NullPointerException
    把工程部署在tomcat的root路径下
    修改tomcat 启动45秒
    struts2 ,web.xml中配置为/*.action,运行报错Invalid <url-pattern> /*.action in filter mapp
    解决Tomcat 7遇到StackOverflowError的异常
    eclipse 安装svn插件
    WP8_ListBox的用法
    WP8_检测列表是否滑动
    WP8_访问ListBox中的Item项中的某个元素
  • 原文地址:https://www.cnblogs.com/kirai/p/5842435.html
Copyright © 2011-2022 走看看