建立结构体保存每个结点的前驱,操作符,来回两遍拓扑排序~

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
struct node {
vector<int> pre;
double data;
int fuhao=;
}Node[maxn];
vector<int> g[maxn];
int a[maxn];
int inDegree[maxn];
int N;
stack<int> topOrder;
double getnum (node a) {
if (a.pre.size()==) return a.data;
else if (a.pre.size()==) {
if (a.fuhao==) return exp(Node[a.pre[]].data);
else if (a.fuhao==) return log(Node[a.pre[]].data);
else if (a.fuhao==) return sin(Node[a.pre[]].data);
}
else if (a.pre.size()==) {
if (a.fuhao==) return Node[a.pre[]].data+Node[a.pre[]].data;
else if (a.fuhao==) return Node[a.pre[]].data-Node[a.pre[]].data;
else if (a.fuhao==) return Node[a.pre[]].data*Node[a.pre[]].data;
}
}
double topSort () {
queue<int> q;
for (int i=;i<N;i++)
if (inDegree[i]==) q.push(i);
while (!q.empty()) {
int u=q.front();
q.pop();
topOrder.push(u);
Node[u].data=getnum(Node[u]);
for (int i=;i<g[u].size();i++)
if (--inDegree[g[u][i]]==) q.push(g[u][i]);
if (q.empty()) return Node[u].data;
}
}
double dfs (int u,int x) {
if (Node[u].fuhao==) {
if (u==x) return ;
else return ;
}
else if (Node[u].fuhao==) return dfs(Node[u].pre[],x)+dfs(Node[u].pre[],x);
else if (Node[u].fuhao==) return dfs(Node[u].pre[],x)-dfs(Node[u].pre[],x);
else if (Node[u].fuhao==) return dfs(Node[u].pre[],x)*Node[Node[u].pre[]].data+dfs(Node[u].pre[],x)*Node[Node[u].pre[]].data;
else if (Node[u].fuhao==) return exp(Node[Node[u].pre[]].data)*dfs(Node[u].pre[],x);
else if (Node[u].fuhao==) return 1.0/Node[Node[u].pre[]].data*dfs(Node[u].pre[],x);
else if (Node[u].fuhao==) return cos(Node[Node[u].pre[]].data)*dfs(Node[u].pre[],x);
}
int main () {
scanf ("%d",&N);
int fuhao;
double x,y;
vector<int> v1;
for (int i=;i<N;i++) {
scanf ("%d",&fuhao);
if (fuhao==) {
scanf ("%lf",&x);
Node[i].data=x;
v1.push_back(i);
}
else if (fuhao>=&&fuhao<=) {
scanf ("%lf %lf",&x,&y);
Node[i].pre.push_back((int)x);
Node[i].pre.push_back((int)y);
inDegree[i]+=;
g[(int)x].push_back(i);
g[(int)y].push_back(i);
Node[i].fuhao=fuhao;
}
else if (fuhao>=&&fuhao<=) {
scanf ("%lf",&x);
Node[i].pre.push_back((int)x);
inDegree[i]+=;
g[(int)x].push_back(i);
Node[i].fuhao=fuhao;
}
}
printf ("%.3f\n",topSort());
int u=topOrder.top();
for (int i=;i<v1.size();i++) {
if (i!=) printf (" ");
printf ("%.3f",dfs(u,v1[i]));
}
return ;
}

L3-023 计算图的更多相关文章

  1. PTA L3-023 计算图 (dfs+数学推导)

    “计算图”(computational graph)是现代深度学习系统的基础执行引擎,提供了一种表示任意数学表达式的方法,例如用有向无环图表示的神经网络. 图中的节点表示基本操作或输入变量,边表示节点 ...

  2. MegEngine计算图、MatMul优化解析

    MegEngine计算图.MatMul优化解析 本文针对天元在推理优化过程中所涉及的计算图优化与 MatMul 优化进行深度解读,希望能够帮助广大开发者在利用天元 MegEngine「深度学习,简单开 ...

  3. 配置 L3 agent - 每天5分钟玩转 OpenStack(99)

    上一节我们介绍了路由服务(Routing)的基本功能,今天教大家如何配置. Neutron 的路由服务是由 l3 agent 提供的. 除此之外,l3 agent 通过 iptables 提供 fir ...

  4. Neutron 理解 (6): Neutron 是怎么实现虚拟三层网络的 [How Neutron implements virtual L3 network]

    学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...

  5. 理解 OpenStack 高可用(HA)(2):Neutron L3 Agent HA 之 虚拟路由冗余协议(VRRP)

    本系列会分析OpenStack 的高可用性(HA)概念和解决方案: (1)OpenStack 高可用方案概述 (2)Neutron L3 Agent HA - VRRP (虚拟路由冗余协议) (3)N ...

  6. php大力力 [023节]CREATE TABLE创建新表sql写字段备注(2015-08-27)

    2015-08-27 php大力力023.CREATE TABLE创建新表sql写字段备注 http://www.cnblogs.com/dalitongxue/p/4762182.html 参考: ...

  7. [Freescale]Freescale L3.14.52_1.1.0 yocto build

    可参照:http://blog.csdn.net/wince_lover/article/details/51456745 1. Refer to <基于i.mx6处理器的Yocto项目及Lin ...

  8. Tensorflow计算模型 —— 计算图

    转载自:http://blog.csdn.net/john_xyz/article/details/69053626 Tensorflow是一个通过计算图的形式来表述计算的编程系统,计算图也叫数据流图 ...

  9. Neutron路由篇:L3 agent+Namespace

    Neutron 的路由服务是由 l3 agent 提供的. 除此之外,l3 agent 通过 iptables 提供 firewall 和 floating ip 服务.     l3 agent 需 ...

  10. TensorFlow框架(一) 张量、计算图、会话

    参考:中国大学MOOC 北京大学 曹健<TensorFlow笔记> 基于TensorFlow的NN:用张量表示数据,用计算图搭建神经网络,用会话执行计算图,优化线上的权重(参数),得到模型 ...

随机推荐

  1. C++-HDU1394-Minimum Inversion Number[数据结构][树状数组]

    给出0~n-1的一个排列,可以整体移动,求逆序对最小值 把数字num[i]的加入,等价于树状数组的第n-num[i]位加1 因为num[i]是第 (n-1)-num[i]+1=n-num[i]大的数字 ...

  2. AcWing 5. 多重背包问题 II

    //二进制优化 最后变为01背包 #include <iostream> #include <algorithm> using namespace std; , M = ; i ...

  3. BeautifulReport报告

    Project description BeautifulReport 一个基于unittest.TestResult模块实现的测试用例模板, 可把测试中的结果通过BeautifulReport整合成 ...

  4. (c#)奇数值单元格的数量

    题目 解

  5. HTML学习(11)表格

    HTML表格由<table>标签定义,下面是一个2行3列的表格: <table> <tr> <td>11</td> <td>12 ...

  6. STA之RC Corner拾遗

    Q:还有一种RC corner 带后缀『_T』,只用于setup signoff,T指的是什么? A:T代表tighten,在rc的variation上的sigma分布比不带T的更紧,因此只能用于se ...

  7. Hadoop2.0之YARN组件

    官方文档:https://hadoop.apache.org/docs/stable/,目前官方已经是3.x,但yarn机制没有太大变化 一.简介 在Hadoop1.0中,没有yarn,所有的任务调度 ...

  8. 全网最详细!搭建Hexo+Coding/Gitee,实现一键生成,同步部署

    全网最全小白搭建Hexo+Gitee/Coding/Github 全网最全小白搭建Hexo+Gitee/Coding/Github 本站内容已全部转移到https://www.myyuns.ltd,具 ...

  9. docker 初识1

    学习网址 https://git.oschina.net/yangllsdev/docker-training https://docs.docker.com/engine/installation/ ...

  10. HDU 3823 Prime Friend(线性欧拉筛+打表)

    Besides the ordinary Boy Friend and Girl Friend, here we define a more academic kind of friend: Prim ...