国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Round C APAC Test 2017 Problem C. Evaluation(拓撲排序)

2019-11-14 09:52:41
字體:
來源:轉載
供稿:網友

題目鏈接

https://code.google.com/codejam/contest/6274486/dashboard#s=p2

題意

給定若干個等式,這些等式的順序可以交換。 等式左邊的值依賴于右邊的值,要求判斷等式是否合法。

思路

我們要解決的問題就是判斷這些變量是否存在相互依賴的關系。 假如a = f(b, c)。那么a依賴于b和c,我們就從b和c分別連一條邊到a。最后要判斷的問題就是這個圖是否存在環。

細節

注意假如a = f(b, c),即a依賴于b和c,那么b和c必須要在等式左邊出現過(即b,c一定能夠被算出來)

代碼

#include <bits/stdc++.h>using namespace std;inline int in() {int x; scanf("%d", &x); return x;}#define PR(x) {cout << #x << ' ' << x << endl;}const int maxn = 100000 + 5;const int maxm = 2005;vector<int> G[maxn];int In[maxn], n, tot, musthas[maxn];vector<int> has;char s[maxm];map<string, int> mmp;void init() { for (int i = 0; i < maxn; i++) G[i].clear(); memset(In, 0, sizeof(In)); memset(musthas, 0, sizeof(musthas)); mmp.clear(); has.clear(); n = 0; tot = 0;}void pre(string s) { bool flag = false; int k = s.find("="); //create to string sv = s.substr(0, k); if (mmp.find(sv) == mmp.end()) mmp[sv] = ++n; int v = mmp[sv]; has.push_back(v); //create from string ss = ""; vector<int> tmp; for (int i = k + 1; i < s.length(); i++) { if (s[i] == '(') {flag = true; continue;} if (s[i] == ')') break; if (flag) { if (s[i] == ',' || i == s.length() - 2) { if (i == s.length() - 2 && s[i] != ',') ss += s[i]; if (mmp.find(ss) == mmp.end()) mmp[ss] = ++n; int u = mmp[ss]; musthas[u] = 1; G[u].push_back(v); In[v]++; ss = ""; } else { ss += s[i]; } } }}bool nocircle() { queue<int> q; int cnt = 0; for (int i = 1; i <= n; i++) if (!In[i]) q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); cnt++; for (auto v : G[u]) { In[v]--; if (!In[v]) q.push(v); } } return cnt == n;}int main() { int T = in(); int kase = 0; while (T--) { init(); int ca = in(); while (ca--) { string s; cin >> s; pre(s); } cout << "Case #" << ++kase << ": "; bool fl = false; for (auto x : has) { if (musthas[x]) musthas[x] = 0; } for (int i = 1; i <= n; i++) { if (musthas[i]) { cout << "BAD" << endl; fl = true; break; } } if (!fl) { if (nocircle()) cout << "GOOD" << endl; else cout << "BAD" << endl; } } return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿勒泰市| 伊宁市| 衡东县| 镶黄旗| 平遥县| 福清市| 沭阳县| 双柏县| 鹿泉市| 洞头县| 太湖县| 全州县| 大厂| 翁牛特旗| 南和县| 金溪县| 商都县| 廊坊市| 左贡县| 铜梁县| 巴东县| 新兴县| 南阳市| 财经| 仪陇县| 张家港市| 汝城县| 和田县| 吴江市| 博乐市| 大港区| 渑池县| 昭平县| 龙陵县| 镇坪县| 隆昌县| 达拉特旗| 郸城县| 江华| 松江区| 北票市|