QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#566545#9319. Bull FarmllleiWA 0ms3652kbC++206.2kb2024-09-16 00:27:312024-09-16 00:27:32

Judging History

你现在查看的是最新测评结果

  • [2024-09-16 00:27:32]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3652kb
  • [2024-09-16 00:27:31]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int n, l, q;

int calc(char a, char b) {
	return (a - 48) * 50 + (b - 48);
}
using B = bitset<2001>;
int main() {
	cin.tie(0);
	ios::sync_with_stdio(0);
	int T;
	cin >> T;
	while (T--) {
		cin >> n >> l >> q;
        vector<string> s(l + 1);
        vector<vector<array<int, 3>>> c(l + 1);
		for (int i = 1; i <= l; i++) {
			cin >> s[i];
		}
		for (int i = 1; i <= q; i++) {
			string t;
			cin >> t;
			c[calc(t[4], t[5])].push_back({calc(t[0], t[1]), calc(t[2], t[3]), i});
		}
        
        vector<int> ans(q + 1);
        
        vector<vector<int>> e(n + 1);
        vector<int> id(n + 1);
        iota(id.begin(), id.end(), 0);
        for (auto [x, y, id] : c[0]) {
            if (x == y) {
                ans[id] = 1;
            } else {
                ans[id] = 0;
            }
        }
		for (int i = 1; i <= l; i++) {
            vector<int> nxt(n + 1);
            vector<int> pre(n + 1);
            vector<int> du(n + 1);

			for (int j = 0; j < n; j++) {
				int u = j + 1, v = calc(s[i][j * 2], s[i][j * 2 + 1]);
             
				du[v]++;
				pre[v] = u;
                nxt[u] = v;
			}
			int cnt = 0;
			for (int j = 1; j <= n; j++) {
                if (du[j] == 0) {
                    cnt++;
                }
            }
            vector<bool> vis(n + 1);
            
            vector<int> dfn(n + 1), low(n + 1);
            int cntt = 0;
            vector<int> stk(n + 1);
            int top = 0;
            int tot = 0;
            vector<int> idd(n + 1);

            auto tarjan = [&](auto &&self, int u) -> void {
                dfn[u] = low[u] = ++cntt;
                vis[u] = true;
                stk[++top] = u;
                    
                for (int v : e[u]) {
                    if (!dfn[v]) {
                        self(self, v);
                        low[u] =min(low[u], low[v]);
                    } else if (vis[v]) {
                        low[u] = min(low[u], dfn[v]);
                    }
                }
                
                if (low[u] == dfn[u]) {
                    tot++;
                    int x;
                    do {
                      
                        x = stk[top];
                        vis[x] = false;
                        top--;
                        idd[x] = tot;
                    } while (x != u);
                }
            };
           
            
			if (cnt == 0) {
                for (int i = 1; i <= n; ++i) {
                    int t = nxt[i];
                    if (id[t] != id[i]) {
                        e[id[t]].push_back(id[i]);
                    }
                }
			} else if (cnt == 1) {
                int u = -1, v = -1;
                for (int i = 1; i <= n; ++i) {
                    if (du[i] == 0) {
                        u = i;
                    } else if (du[i] == 2) {
                        v = i;
                    }
                }
                int x = v;
                while (nxt[v] != x) {
                    v = nxt[v];
                }
                
                if (id[v] != id[u]) {
                    e[id[u]].push_back(id[v]);
                }

                int t1 = u;
                while (nxt[t1] != x) {
                    t1 = nxt[t1];
                }

                if (id[t1] != id[u]) {
                    e[id[u]].push_back(id[t1]);
                }
			} else {
            }

            for (int i = 1; i <= n; ++i) {
                if (!dfn[id[i]]) {
                    tarjan(tarjan, id[i]);
                }
            }
            
            vector<bool> vis1(n + 1, 0);
            vector<vector<int>> ee(n + 1);
            for (int i = 1; i <= n; ++i) {
                if (!vis1[id[i]]) {
                    continue;
                }
                int u = id[i];
                vis1[u] = true;
                
                for (int v : e[u]) {
                    int t1 = idd[u];
                    int t2 = idd[v];
                    
                    if (t1 != t2 && !vis[t2]) {
                        ee[t1].push_back(t2);
                        vis[t2] = true;
                    }
                }

                for (int v : e[u]) {
                    int t2 = idd[v];
                    vis[t2] = false;
                }
            }
            
 
            e = move(ee);
            for (int i = 1; i <= n; ++i) {
                id[i] = idd[id[i]];
                assert(id[i] != 0);
            }
            
            vector<B> b(n + 1);
            vector<int> dd(n + 1);
            vis.assign(n + 1, 0);
            for (int i = 1; i <= n; ++i) {
                if (!vis[id[i]]) {
                    int u = id[i];
                    vis[u] = true;
                    b[u][u] = true;
                    for (int v : e[u]) {
                        dd[v]++;
                    }
                }
            }
            
            queue<int> q;
            vis.assign(n + 1, 0);
            
            for (int i = 1; i <= n; ++i) {
                if (!vis[id[i]] && dd[id[i]] == 0) {
                    vis[id[i]] = true;
                    q.push(id[i]);
                }
            }
            while (q.size()) {
                int u = q.front();
                q.pop();
                
                for (int v : e[u]) {
                    b[v] |= b[u];
                    dd[v]--;
                    if (dd[v] == 0) {
                        q.push(v);
                    }
                }
            }
            
            for (auto [u, v, idd] : c[i]) {
                u = id[u];
                v = id[v];
                
                if (b[u][v]) {
                    ans[idd] = 1;
                } else {
                    ans[idd] = 0;
                }
            }

		}
        for (int i = 1; i <= q; ++i) {
            cout << ans[i];
        }
        cout << '\n';
	}
}

/*
1
6 2 4
030603010601
010203060504
030202
060402
050602
060401

1
3 3 2
020202
030301
030201
020303
010202
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3588kb

input:

2
5 2 4
0305040201
0404040404
030300
020500
050102
020501
6 2 4
030603010601
010203060504
030202
060402
050602
060401

output:

1011
0100

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3652kb

input:

1
3 3 6
020202
030301
030201
020102
030203
010201
010303
020303
010202

output:

000100

result:

wrong answer 1st lines differ - expected: '010101', found: '000100'