Submission #658864

# Submission time Handle Problem Language Result Execution time Memory
658864 2022-11-15T08:21:02 Z Foxyy Kutije (COCI21_kutije) C++17
70 / 70
218 ms 17544 KB
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define Foxyy cin.tie(0); cout.sync_with_stdio(0); cout.tie(0);
 
struct Solver {
	int& n;
	int& m;
	int& q;
	vector<vector<int>>& p;
	
	vector<vector<int>> adj{};
	vector<int> scc{};
	int sccCnt = 0;
	vector<vector<int>> sccAdj{};
	vector<set<int>> reachable{};
	
	void getAdj() {
		adj.resize(n);
		for (auto& vec : p) {
			for (int i = 0; i < n; i++) if (i != vec[i]) {
				adj[vec[i]].push_back(i);
			}
		}
		for (int i = 0; i < n; i++) {
			sort(adj[i].begin(), adj[i].end());
			adj[i].resize(unique(adj[i].begin(), adj[i].end()) - adj[i].begin());
		}
	}
	
	void getSCC() {
		vector<int> dep(n), low(n);
		stack<int, vector<int>> stk;
		vector<bool> inStk(n);
		int ts = 0;
		scc.resize(n);
		
		function<void(int)> dfs = [&](int u) {
//			cerr << "Entering dfs(" << u << ")...\n";
			dep[u] = low[u] = ++ts;
			stk.push(u);
			inStk[u] = true;
			
			for (int v : adj[u]) {
				if (not dep[v]) {
					dfs(v);
				}
				if (inStk[v]) {
					low[u] = min(low[u], low[v]);
				}
			}
			
//			cerr << "low[" << u << "] = " << low[u] << ", dep[" << u << "] = " << dep[u] << '\n';
						
			if (low[u] == dep[u]) {
				while (true) {
					int x = stk.top();
					stk.pop();
					inStk[x] = false;
					scc[x] = sccCnt;
					if (x == u) {
						break;
					}
				}
				sccCnt++;
			}
			
//			cerr << "Exiting from dfs(" << u << ")...\n";
		};
		
		for (int i = 0; i < n; i++) {
			if (not dep[i]) {
//				cerr << "Calling dfs(" << i << ")...\n";
				dfs(i);
			}
		}
	}
	
	void getSCCAdj() {
		sccAdj.resize(sccCnt);
		for (int i = 0; i < n; i++) {
			for (int j : adj[i]) {
				if (scc[i] != scc[j]) {
					adj[scc[i]].push_back(scc[j]);
				}
			}
		}
	}
	
	void getReachable() {
		reachable.resize(sccCnt);
		for (int i = 0; i < sccCnt; i++) {
			reachable[i].insert(i);
			for (int j : sccAdj[i]) {
				for (int x : reachable[j]) {
					reachable[i].insert(x);
				}
			}
		}
	}
	
	void solve() {
//		cerr << "Calling getAdj()...\n";
		getAdj();
//		cerr << "Calling getSCC()...\n";
		getSCC();
//		cerr << "sccCnt = " << sccCnt << '\n';
//		cerr << "Calling getSCCAdj()...\n";
		getSCCAdj();
//		cerr << "Calling getReachable()...\n";
		getReachable();
		
		for (int _ = 0; _ < q; _++) {
			int a, b;
			cin >> a >> b;
			a--, b--;
			if (reachable[scc[a]].find(scc[b]) != reachable[scc[a]].end()) {
				cout << "DA\n";
			} else {
				cout << "NE\n";
			}
		}
	}
};
 
signed main() {
	Foxyy
	
	int T = 1;
//	cin >> T;
	while(T--) {
		int n, m, q;
		cin >> n >> m >> q;
		vector<vector<int>> p(m, vector<int>(n));
		for (auto& vec : p) {
			for (auto& x : vec) {
				cin >> x;
				x--;
			}
		}
		
		Solver solver{n, m, q, p};
		solver.solve();
	}
}
# Verdict Execution time Memory Grader output
1 Correct 74 ms 1932 KB Output is correct
2 Correct 65 ms 1760 KB Output is correct
3 Correct 62 ms 1624 KB Output is correct
4 Correct 104 ms 1860 KB Output is correct
5 Correct 76 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 2 ms 440 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 1932 KB Output is correct
2 Correct 65 ms 1760 KB Output is correct
3 Correct 62 ms 1624 KB Output is correct
4 Correct 104 ms 1860 KB Output is correct
5 Correct 76 ms 1884 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 2 ms 440 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 195 ms 10360 KB Output is correct
27 Correct 169 ms 10256 KB Output is correct
28 Correct 161 ms 11952 KB Output is correct
29 Correct 218 ms 17416 KB Output is correct
30 Correct 139 ms 14104 KB Output is correct
31 Correct 154 ms 15620 KB Output is correct
32 Correct 204 ms 17544 KB Output is correct
33 Correct 161 ms 17476 KB Output is correct
34 Correct 147 ms 13600 KB Output is correct
35 Correct 151 ms 15768 KB Output is correct