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

#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. 1.rest之@Get和@Post请求的区别:

    区别: (1)@Get 一般用于查询或获取资源信息, @Post一般是用于更新资源信息. (2)Url不同, @Get 请求的url: http://localhost:8080/imeter-cms ...

  2. APP 安全测试

    http://www.cnblogs.com/wetest/p/6694529.html

  3. Java爬虫学习(2)之用对象保存文件demo(1)

    package com.mieba.spider; import java.util.ArrayList; import java.util.List; import java.util.Vector ...

  4. react生命周期方法有哪些?

    react生命周期方法有哪些? React 16.3+ getDerivedStateFromProps:在调用render()之前调用,并在每次渲染时调用.需要使用派生状态的情况是很罕见的 comp ...

  5. mac visual stdio 2019 自动格式化代码的选项

    1 打开Visual Stdio >点开菜单的visual stdio 的选项卡>首选项>文本编辑>行为>保存时格式化文档 (将这个选项 勾上)>确定 2 以后代码 ...

  6. 每天进步一点点------Allegro PCB命名规则

    PCB命名规则-allegro 一.焊盘命名规则 1. 贴片矩形焊盘  命名规则:SMD+长(L)+宽(W)(mil) 举例:SMD90X60 2. 贴片圆焊盘   命名规则:SMDC+焊盘直径(D) ...

  7. 阿里云打包成zip格式,批量下载

    /** * 从阿里云下载文件 (以附件形式下载) 多个文件进行zip下载 第二种 * @param request * @param response */ @ResponseBody @Reques ...

  8. threading 官方 线程对象和锁对象以及条件对象condition

    官方地址:https://docs.python.org/2/library/threading.html#thread-objects 以下只截取condition部分,其他Lock()以及thre ...

  9. Linux之Socket编程

    1.什么是Socket? socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,都可以用“打开open –> 读写write/read –> 关闭close”模 ...

  10. drf三大组件之认证组件与权限组件

    复习 """ 视图家族 1.视图类:APIView.GenericAPIView APIView:作为drf的基础view:as_view()禁用csrf:dispatc ...