結果

問題 No.1078 I love Matrix Construction
ユーザー yukudoyukudo
提出日時 2020-06-13 00:05:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 209 ms / 2,000 ms
コード長 2,450 bytes
コンパイル時間 1,649 ms
コンパイル使用メモリ 171,980 KB
実行使用メモリ 50,696 KB
最終ジャッジ日時 2023-09-06 11:30:41
合計ジャッジ時間 6,118 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
29,880 KB
testcase_01 AC 14 ms
29,688 KB
testcase_02 AC 32 ms
32,820 KB
testcase_03 AC 73 ms
37,608 KB
testcase_04 AC 104 ms
40,356 KB
testcase_05 AC 88 ms
38,588 KB
testcase_06 AC 29 ms
32,724 KB
testcase_07 AC 19 ms
31,060 KB
testcase_08 AC 85 ms
38,916 KB
testcase_09 AC 17 ms
30,296 KB
testcase_10 AC 209 ms
50,696 KB
testcase_11 AC 104 ms
41,028 KB
testcase_12 AC 167 ms
46,972 KB
testcase_13 AC 187 ms
49,028 KB
testcase_14 AC 118 ms
43,624 KB
testcase_15 AC 182 ms
48,188 KB
testcase_16 AC 18 ms
30,752 KB
testcase_17 AC 13 ms
29,700 KB
testcase_18 AC 24 ms
31,824 KB
testcase_19 AC 41 ms
34,672 KB
testcase_20 AC 39 ms
34,604 KB
testcase_21 AC 13 ms
29,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}

ll nextLong() { ll x; scanf("%lld", &x); return x;}

const int MAX_V = 500 * 500 * 2 + 10;
vector<int> g[MAX_V];
vector<int> r[MAX_V];


const int MAX_N = 505;
int A[MAX_N][MAX_N];
int N;
int nodeId(int r, int c, int f) {
  return (r * N + c) * 2 + f;
}

int sccid[MAX_V];
int sccnum;
void dfs(int p, vector<int>& order) {
  if (sccid[p] != 0) return;
  sccid[p] = 1;
  for (int i : g[p]) dfs(i, order);
  order.push_back(p);
}
void rdfs(int p, int id) {
  if (sccid[p] != -1) return;
  sccid[p] = id;
  for (int i : r[p]) rdfs(i, id);
}
void scc() {
  vector<int> order;
  CLR(sccid, 0);
  REP(i, 2*N*N) dfs(i, order);
  CLR(sccid, -1);
  sccnum = 0;
  for (int i = (int)order.size() - 1; i >= 0; i--) if (sccid[order[i]] == -1) {
    rdfs(order[i], sccnum);
    sccnum++;
  }
}


int main2() {
  REP(i, MAX_V) g[i].clear();
  REP(i, MAX_V) r[i].clear();

  N = nextLong();

  vector<int> S(N), T(N), U(N);
  REP(i, N) S[i] = nextLong()-1;
  REP(i, N) T[i] = nextLong()-1;
  REP(i, N) U[i] = nextLong();


  REP(i, N) REP(j, N) {
    int a = nodeId(S[i], j, U[i] & 1);
    int b = nodeId(j, T[i], U[i] >> 1);
    g[b].push_back(a^1);
    g[a].push_back(b^1);
    r[a^1].push_back(b);
    r[b^1].push_back(a);
  }

  scc();

  // REP(i, N*N) cout << sccid[i * 2] << " "; cout << endl;
  // REP(i, N*N) cout << sccid[i * 2 + 1] << " "; cout << endl;

  bool ng = false;
  REP(r, N) REP(c, N) {
    if (sccid[nodeId(r, c, 0)] != -1 && sccid[nodeId(r, c, 0)] == sccid[nodeId(r, c, 1)]) {
      ng = true;
    }
  }
  if (ng) {
    cout << -1 << endl;
  } else {

    REP(i, N*N) {
      int r = i / N;
      int c = i % N;
      A[r][c] = (sccid[nodeId(r, c, 1)] > sccid[nodeId(r, c, 0)]) ? 1 : 0;
    }

    REP(r, N) {
      REP(c, N) {
        if (c) cout << ' ';
        cout << A[r][c];
      }
      cout << '\n';
    }
  }
  return 0;
}

int main() {

#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}
0