我是链接

看到这道题,2个点和一个权值,然后想到图,但是leetcode就是这样,没给数据范围,感觉写起来很费劲,然后就开始用图来做,添加边的时候,注意正向边和反向变,然后查询的时候,先判断2个点是否都出现,然后判断是不是自己到自己,最后用dfs或者bfs到图里面去搜索,第一次交的时候,忘记用vis标记访问过的点,导致tle,加上之后,就ac了,套路一般吧,不知道还有什么trick。

double dfs(vector<vector<pair<int, double> > > &e, int x, int y) {
int n = e.size();
vector<double> dis(n, 1);
queue<int> q;
q.push(x);
vector<bool> tag(n, 0);
while(!q.empty()) {
int u = q.front(); q.pop();
//cout << u << endl;
for (int i = 0; i < e[u].size(); i++) {
int a = e[u][i].first;
double b = e[u][i].second;
if(tag[a]) continue;
dis[a] = dis[u] * b;
if(a == y) return dis[a];
q.push(a);
tag[a] = 1;
}
}
return -1.0;
}
vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> query) {
vector<vector<pair<int, double> > > e; map<string, int> m;
for (int i = 0; i < equations.size(); i++) {
string x = equations[i].first, y = equations[i].second;
if(!m.count(x)) {
vector<pair<int, double> > t;
m[x] = e.size(); e.push_back(t);
}
if(!m.count(y)) {
vector<pair<int, double> > t;
m[y] = e.size(); e.push_back(t);
}
int a = m[x],b = m[y];
e[a].push_back({b, values[i]});
e[b].push_back({a, 1.0 / values[i]});
}
//cout << "ad" << endl;
//cout << e.size() << endl;
vector<double> res;
for (int i = 0; i < query.size(); i++) {
string x = query[i].first, y = query[i].second;
//cout <<x << " " << y << endl;
if(!m.count(x) || !m.count(y)) {
res.push_back(-1.0);
} else {
if(x == y) {
res.push_back(1.0);
} else {
int a = m[x], b = m[y];
double t = dfs(e, a, b);
res.push_back(t);
}
}
}
return res; }

[leetcode] 399. Evaluate Division的更多相关文章

  1. LN : leetcode 399 Evaluate Division

    lc 399 Evaluate Division 399 Evaluate Division Equations are given in the format A / B = k, where A ...

  2. [LeetCode] 399. Evaluate Division 求除法表达式的值

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

  3. [Leetcode Week3]Evaluate Division

    Evaluate Division题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/evaluate-division/description/ Desc ...

  4. 【LeetCode】399. Evaluate Division 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 【leetcode】399. Evaluate Division

    题目如下: Equations are given in the format A / B = k, whereA and B are variables represented as strings ...

  6. 399. Evaluate Division

    图像题,没觉得有什么简单的办法,貌似可以用Union Find来解. 感觉有2种思路,一种是先全部构建好每2个点的weight,然后直接遍历queires[][]来抓取答案. 一种是只构建简单的关系图 ...

  7. [LeetCode] Evaluate Division 求除法表达式的值

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

  8. Leetcode: Evaluate Division

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

  9. [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

随机推荐

  1. 【STL源码学习】STL算法学习之一

    第一章:引子 STL包含的算法头文件有三个:<algorithm><numeric><functional>,其中最大最常用的是<algorithm>, ...

  2. power designer 水电费缴纳系统的设计

    alter table POWER drop constraint FK_POWER_REFERENCE_USERS; drop table POWER cascade constraints; /* ...

  3. zxing 生成二维码

    一.zxing介绍 zxing是google提供生成.解析一维码.二维码的开源库. 二.使用 2.1 maven pom 配置 <dependency> <groupId>co ...

  4. PostgreSQL的 create index concurrently

    对于PostgreSQL的 "create index concurrently". 我个人认为其中存在一个bug. 我的验证过程如下: 我有两个表,tab01和 tab02,这两 ...

  5. 数据库中DDL、DML、DCL和TCP概念

    1.DDL(Data Definition Language)数据库定义语言statements are used to define the database structure or schema ...

  6. 实例源码--Android通讯录源码

      下载源码   技术要点: 1.通讯录联 系人的管理 2.接听.打电话 3.发短信 4. 源码带详细的 中文注释    ...... 详细介绍: 1.通讯录联系人的管理 播放器具有播放本地音乐的功能 ...

  7. PHP中的全局变量$_SERVER

    1.常用 $_SERVER['PHP_SELF'] #当前正在执行脚本的文件名,与 document root相关.$_SERVER['argv'] #传递给该脚本的参数.$_SERVER['argc ...

  8. FactoryBean的使用--转

    一般情况下,Spring通过反射机制利用bean的class属性指定实现类来实例化bean .在某些情况下,实例化bean过程比较复杂,如果按照传统的方式,则需要在<bean>中提供大量的 ...

  9. Android Studio无法启动 打开, Android Studio gradle下载不了

    Google在2013年I/O大会上发布了Android Studio,AndroidStudio是一个基于IntelliJ思想的新的Android开发工具.下面介绍一下Android Studio安 ...

  10. Android NDK STL

    相信Android开发者都喜欢用C++编写一些高效的应用,有关Android NDK的C++开发相关知识总结如下:       从Android NDK r5开始支持了STL Port,在这个版本开始 ...