結果
問題 | No.1078 I love Matrix Construction |
ユーザー |
|
提出日時 | 2020-06-12 21:42:24 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 360 ms / 2,000 ms |
コード長 | 2,338 bytes |
コンパイル時間 | 2,623 ms |
コンパイル使用メモリ | 205,712 KB |
最終ジャッジ日時 | 2025-01-11 02:22:34 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 22 |
ソースコード
#include <bits/stdc++.h>using namespace std;struct SCC{int N;vector<bool> used;vector<int> vs;vector<int> cmp;void dfs(int v, const vector<int> G[]){used[v] = true;for(auto u : G[v]){if(!used[u]) dfs(u, G);}vs.push_back(v);}void rdfs(int v, int k, const vector<vector<int>>& rG){used[v] = true;cmp[v] = k;for(auto u : rG[v]){if(!used[u]) rdfs(u, k, rG);}}SCC(int N, const vector<int> G[]):N(N){vector<vector<int>> rG(N);for(int i=0; i<N; i++) for(int j : G[i]) rG[j].push_back(i);used.resize(N);fill(used.begin(), used.end(), false);for(int v=0; v<N; v++){if(!used[v]) dfs(v, G);}fill(used.begin(), used.end(), false);cmp.resize(N);int k = 0;for(int i = vs.size()-1; i>=0; i--){if(!used[vs[i]]) rdfs(vs[i], k++, rG);}}int dump_graph(const vector<int> G[], vector<int> edges2[]){for(int i=0; i<N; i++){for(auto j : G[i]){if(cmp[i] != cmp[j]){edges2[cmp[i]].push_back(cmp[j]);}}}return *max_element(cmp.begin(), cmp.end()) + 1;}};int main(){int N;cin >> N;vector<int> S(N), T(N), U(N);for(int i=0; i<N; i++) cin >> S[i];for(int i=0; i<N; i++) cin >> T[i];for(int i=0; i<N; i++) cin >> U[i];static vector<int> edges[500*500*2];auto encode = [&](int i, int j, int b){ return b*N*N+i*N+j; };for(int i=0; i<N; i++){S[i]--; T[i]--;int b1 = U[i]%2, b2 = U[i]/2;for(int j=0; j<N; j++){edges[encode(S[i], j, b1)].push_back(encode(j, T[i], 1-b2));edges[encode(j, T[i], b2)].push_back(encode(S[i], j, 1-b1));}}SCC scc(N*N*2, edges);int ans[500][500] = {0};for(int i=0; i<N; i++) for(int j=0; j<N; j++){int a = encode(i, j, 0), b = encode(i, j, 1);if(scc.cmp[a] == scc.cmp[b]){cout << -1 << endl;return 0;}if(scc.cmp[a] < scc.cmp[b]) ans[i][j] = 1;}for(int i=0; i<N; i++) for(int j=0; j<N; j++) cout << ans[i][j] << " \n"[j==N-1];return 0;}