NC14306. Debug
描述
输入描述
The first line is the number of test cases. For each test case, there is the code as described before. Each case contains no more than 100 labels, 100 atomic sentence, and 500 others sentences.
输出描述
For each test case, output a line containing the minimum time Bob need. If there is no solution, output -1.
示例1
输入:
3 STATEMENT IF GOTO L1 STATEMENT GOTO L2 L1: STATEMENT L2: END STATEMENT IF GOTO L1 STATEMENT GOTO L2 L1: IF GOTO L1 L2: END STATEMENT L0: IF GOTO L1 STATEMENT GOTO L2 L1: STATEMENT L2: IF GOTO L0 END
输出:
2 1 1
C++11(clang++ 3.9) 解法, 执行用时: 78ms, 内存消耗: 700K, 提交时间: 2019-01-07 17:15:01
#include <cstdio> #include <cstring> #include <iostream> #include <string> #include <map> using namespace std; const int MAXN = 260; const int MAXM = 11000; const int MAXS = 30; map<string, int> h; int n; int m; bool a[MAXN][MAXN]; bool ena[MAXN]; bool g[MAXN][MAXN]; bool chk[MAXN]; int mat[MAXN]; int q[MAXN]; int f[MAXN]; int getHash(string &s) { if (h[s] == 0) return h[s] = (++n); else return h[s]; } void init() { h.clear(); string s; int cur = 1; bool d = true; n = 1; memset(a, false, sizeof(a)); memset(ena, false, sizeof(ena)); memset(f, 0xff, sizeof(f)); memset(q, 0xff, sizeof(q)); while (cin >> s && s != "END") { if (s == "STATEMENT") { if (!d) { a[cur][++n] = 1; cur = n; } ena[cur] = true; } else if (s == "GOTO") { cin >> s; a[cur][getHash(s)] = true; cur = 0; } else if (s == "IF") { cin >> s; cin >> s; a[cur][getHash(s)] = true; d = false; } else { int l = s.size(); s = s.substr(0, l - 1); int v = getHash(s); a[cur][v] = true; cur = v; d = true; } } ena[1] = true; } int cnt; void dfs(int v) { if (chk[v]) return; chk[v] = true; for (int i = 1; i <= n; ++i) if (ena[i] && a[v][i]) dfs(i); q[++cnt] = v; } void dfs2(int v) { if (chk[v]) return; f[v] = cnt; chk[v] = true; for (int i = 1; i <= n; ++i) if (ena[i] && a[i][v]) dfs2(i); } bool dfs3(int v) { for (int i = 1; i <= cnt; ++i) if (g[v][i] && !chk[i]) { chk[i] = true; int tmp = mat[i]; mat[i] = v; if (tmp == -1) return true; if (dfs3(tmp)) return true; mat[i] = tmp; } return false; } void solve() { if (ena[0]) { printf("-1\n"); return; } int ne = 0; for (int i = 1; i <= n; ++i) { //if (!ena[i]) { for (int j = 1; j <= n; ++j) for (int k = 1; k <= n; ++k) if (a[j][i] && a[i][k]) a[j][k] = 1; } if (ena[i]) ++ne; } cnt = 0; memset(chk, 0, sizeof(chk)); dfs(1); if (cnt != ne) { printf("-1\n"); return; } memset(chk, 0, sizeof(chk)); cnt = 0; for (int i = ne; i >= 1; --i) if (!chk[q[i]]) { ++cnt; dfs2(q[i]); } memset(g, 0, sizeof(g)); for (int i = 1; i <= n; ++i) if (ena[i]) for (int j = 1; j <= n; ++j) if (ena[j] && a[i][j] && f[i] != f[j]) g[f[i]][f[j]] = true; memset(mat, 0xff, sizeof(mat)); int ans = cnt; for (int i = 1; i <= cnt; ++i) { memset(chk, 0, sizeof(chk)); if (dfs3(i)) --ans; } printf("%d\n", ans); } int main() { int tt; cin >> tt; while (tt--) { init(); solve(); } return 0; }