結果

問題 No.1078 I love Matrix Construction
ユーザー snukesnuke
提出日時 2020-06-12 21:33:16
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 440 ms / 2,000 ms
コード長 4,053 bytes
コンパイル時間 3,135 ms
コンパイル使用メモリ 219,872 KB
実行使用メモリ 92,800 KB
最終ジャッジ日時 2023-09-06 09:53:37
合計ジャッジ時間 9,165 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 39 ms
16,164 KB
testcase_03 AC 138 ms
38,528 KB
testcase_04 AC 210 ms
50,384 KB
testcase_05 AC 180 ms
45,416 KB
testcase_06 AC 37 ms
15,608 KB
testcase_07 AC 13 ms
8,004 KB
testcase_08 AC 175 ms
42,700 KB
testcase_09 AC 7 ms
5,388 KB
testcase_10 AC 440 ms
92,800 KB
testcase_11 AC 227 ms
53,212 KB
testcase_12 AC 375 ms
77,920 KB
testcase_13 AC 429 ms
87,368 KB
testcase_14 AC 291 ms
61,680 KB
testcase_15 AC 410 ms
82,872 KB
testcase_16 AC 12 ms
7,112 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 29 ms
12,740 KB
testcase_19 AC 85 ms
25,032 KB
testcase_20 AC 74 ms
24,872 KB
testcase_21 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
inline int getInt() { int x; scanf("%d",&x); return x;}
template<typename T>inline istream& operator>>(istream&i,v(T)&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const v(T)&v)
{stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>inline ostream& operator<<(ostream&o,const v(T)&v)
{if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<typename T1,typename T2>inline ostream& operator<<(ostream&o,const pair<T1,T2>&v)
{return o<<v.fi<<","<<v.se;}
template<typename T>inline ll suma(const v(T)& a) { ll res(0); for (auto&& x : a) res += x; return res;}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
const int MX = 200005;

// 2-SAT
// SCC
// upstream is smaller id
struct scc {
  int n, k;
  vvi to, ot, d, gt; // to, rev_to, groups, group_to
  vi g, used, kv; // group, gomi, topo_ord
  scc(int n=0):n(n),to(n),ot(n){}
  int inc() { to.pb(vi()); ot.pb(vi()); return n++;}
  void add(int a, int b) { to[a].pb(b); ot[b].pb(a);}
  void dfs(int v) {
    if (used[v]) return;
    used[v] = 1;
    rep(i,sz(to[v])) dfs(to[v][i]);
    kv[--k] = v;
  }
  void rfs(int v) {
    if (g[v] != -1) return;
    g[v] = k; d[k].pb(v);
    rep(i,sz(ot[v])) rfs(ot[v][i]);
  }
  void init() {
    k = n;
    used = kv = vi(n);
    g = vi(n,-1);
    rep(i,n) dfs(i);
    rep(i,n) if (g[kv[i]] == -1)  {
      d.pb(vi());
      rfs(kv[i]);
      k++;
    }
    gt = vvi(k);
    rep(i,n)rep(j,sz(ot[i])) {
      int v = g[ot[i][j]], u = g[i];
      if (v != u) gt[v].pb(u);
    }
    rep(i,k) {
      sort(rng(gt[i]));
      gt[i].erase(unique(rng(gt[i])),gt[i].end());
    }
  }
};
//
struct sat {
  int n, n2;
  scc g;
  vi d;
  sat() {}
  sat(int n):n(n),n2(n*2),g(n2) {}
  int inv(int a) { return n2-1-a;}
  void add(int a, int b) {
    g.add(inv(a),b); g.add(inv(b),a);
  }
  void imply(int a, int b) { add(inv(a),b);}
  void must(int a, bool x=true) { if (x) add(a,a); else must(inv(a));}
  bool solve() {
    g.init();
    rep(i,n) if (g.g[i] == g.g[inv(i)]) return false;
    d = vi(n2);
    rep(i,n2) d[i] = g.g[i] > g.g[inv(i)];
    return true;
  }
};
//



int main() {
  int n;
  scanf("%d",&n);
  vi sa(n);
  vi sb(n);
  vi sc(n);
  cin>>sa>>sb>>sc;
  rep(i,n) sa[i]--;
  rep(i,n) sb[i]--;
  sat g(n*n);
  rep(i,n) {
    int a = sa[i];
    int b = sb[i];
    int c = sc[i];
    int ca = c&1;
    int cb = c>>1;
    rep(j,n) {
      int va = a*n+j;
      int vb = j*n+b;
      if (ca) va = g.inv(va);
      if (cb) vb = g.inv(vb);
      g.add(va,vb);
    }
  }
  if (!g.solve()) dame;
  vvi ans(n,vi(n));
  rep(i,n)rep(j,n) {
    int v = i*n+j;
    ans[i][j] = g.d[v];
  }
  rep(i,n) cout<<ans[i]<<endl;
  return 0;
}




















0