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

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

Round C APAC Test 2017 Problem C. Evaluation(拓?fù)渑判颍?/h1>
2019-11-14 09:57:18
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

題目鏈接

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

題意

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

思路

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

細(xì)節(jié)

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

代碼

#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;}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

主站蜘蛛池模板: 河间市| 南皮县| 铁岭市| 松阳县| 嘉峪关市| 吴忠市| 吴旗县| 盘山县| 门源| 福鼎市| 宣武区| 汤阴县| 定州市| 德安县| 随州市| 饶阳县| 古交市| 芜湖县| 珲春市| 耿马| 平利县| 泰兴市| 武平县| 运城市| 碌曲县| 汝阳县| 武夷山市| 伊宁县| 乃东县| 平原县| 东光县| 方正县| 云梦县| 吉木乃县| 澄江县| 青龙| 贵南县| 靖州| 祁阳县| 石家庄市| 台江县|