답안 #303152

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
303152 2020-09-19T23:16:20 Z VROOM_VARUN 슈퍼트리 잇기 (IOI20_supertrees) C++14
100 / 100
262 ms 26244 KB
/*
ID: varunra2
LANG: C++
TASK: supertrees
*/

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

#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
  cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif

#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second

const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions

int n;
VVI p;
VVI ret;

void dfs(int i, vector<bool>& vis, int bnd, VI& cmp) {
  if (vis[i]) return;
  vis[i] = true;
  cmp.PB(i);
  for (int ii = 0; ii < n; ii++) {
    if (i == ii) continue;
    if (p[i][ii] >= bnd) {
      dfs(ii, vis, bnd, cmp);
    }
  }
}

bool isBad() {
  trav(x, p) trav(y, x) if (y == 3) return false;

  for (int i = 0; i < n; i++) {
    if (p[i][i] != 1) return false;
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (p[i][j] != p[j][i]) return false;
    }
  }

  vector<bool> vis(n, false);

  for (int i = 0; i < n; i++) {
    if (vis[i]) continue;
    VI comp;
    dfs(i, vis, 1, comp);
    trav(x, comp) trav(y, comp) if (p[x][y] == 0) return false;
  }

  return true;
  // change/update later
}

// void solve(VI& comp) {

// }

struct dsu {
  VI par;
  VI siz;
  int n;
  void init(int _n) {
    n = _n;
    par.resize(n);
    siz.resize(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      siz[i] = 1;
    }
  }

  bool ispar(int x) { return x == par[x]; }

  int find(int x) {
    while (par[x] != par[par[x]]) par[x] = par[par[x]];
    return par[x];
  }

  bool same(int x, int y) { return find(x) == find(y); }

  void merge(int x, int y) {
    x = find(x);
    y = find(y);
    // if (siz[x] < siz[y]) swap(x, y);
    par[y] = x;
    siz[x] += siz[y];
  }
};

void edge(int u, int v) {
  // make an edge between u and v
  if (u < 0 or v < 0 or u >= n or v >= n) return;

  ret[u][v] = 1;
  ret[v][u] = 1;
}

int buildcycle(VI& comp) {
  // build a cycle, first element is the parent
  if (sz(comp) == 0) return 1;
  if (sz(comp) == 1) return 1;
  if (sz(comp) == 2) return 0;
  edge(comp[0], comp.back());
  for (int i = 0; i < sz(comp) - 1; i++) {
    edge(comp[i], comp[i + 1]);
  }
  return 1;
}

void buildtree(VI& comp) {
  // build tree, first element is root
  for (int i = 1; i < sz(comp); i++) {
    edge(comp[0], comp[i]);
  }
}

// void build(VVI& a) {
//   // bruh
//   // debug(a);
//   debug("here");
//   for (int i = 0; i < n; i++) {
//     debug(i, a[i]);
//   }
// }

int construct(VVI asdf) {
  n = sz(asdf);
  p = asdf;

  ret.resize(n);

  for (int i = 0; i < n; i++) {
    ret[i].resize(n);
  }


  if (!isBad()) return 0;


  dsu a;

  a.init(n);

  rep(i, 0, n) rep(j, 0, n) {
    if (p[i][j] == 1 and !a.same(i, j)) {
      a.merge(i, j);
    }
  }


  rep(i, 0, n) {
    if (!a.ispar(i)) {
      ret[i][a.find(i)] = 1;
      ret[a.find(i)][i] = 1;
    }
  }


  VI b(n);

  for (int i = 0; i < n; i++) {
    if (a.ispar(i))
      b[i] = i;
    else
      b[i] = -1;
  }


  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (p[i][j] == 2 and b[i] != b[j] and b[i] != -1 and b[j] != -1) {
        int bb = b[i];
        for (int k = 0; k < n; k++) {
          if (b[k] == bb) b[k] = b[j];
        }
      }
    }
  }

  for (int i = 0; i < n; i++) {
    VI comp;
    for (int j = 0; j < n; j++) {
      if (b[j] == i) comp.PB(j);
    }
    int x = buildcycle(comp);
    if (x == 0) return 0;
  }


  build(ret);
  return 1;
}

// int main() {
// #ifndef ONLINE_JUDGE
//   freopen("supertrees.in", "r", stdin);
//   freopen("supertrees.out", "w", stdout);
// #endif
//   cin.sync_with_stdio(0);
//   cin.tie(0);

//   int n;
//   cin >> n;

//   VVI vals(n, VI(n));

//   for (int i = 0; i < n; i++) {
//     for (int j = 0; j < n; j++) {
//       cin >> vals[i][j];
//     }
//   }

//   int x = construct(vals);

//   debug(x);

//   return 0;
// }
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 11 ms 1408 KB Output is correct
7 Correct 254 ms 26104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 11 ms 1408 KB Output is correct
7 Correct 254 ms 26104 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 10 ms 1408 KB Output is correct
13 Correct 244 ms 26104 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 5 ms 1024 KB Output is correct
17 Correct 125 ms 16120 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 61 ms 6776 KB Output is correct
21 Correct 245 ms 26104 KB Output is correct
22 Correct 244 ms 26244 KB Output is correct
23 Correct 257 ms 26116 KB Output is correct
24 Correct 243 ms 26104 KB Output is correct
25 Correct 102 ms 16120 KB Output is correct
26 Correct 101 ms 16120 KB Output is correct
27 Correct 262 ms 26128 KB Output is correct
28 Correct 243 ms 26072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 12 ms 1408 KB Output is correct
9 Correct 244 ms 26104 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 11 ms 1408 KB Output is correct
13 Correct 250 ms 26072 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 5 ms 1024 KB Output is correct
17 Correct 115 ms 16212 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 0 ms 256 KB Output is correct
21 Correct 61 ms 6776 KB Output is correct
22 Correct 246 ms 26104 KB Output is correct
23 Correct 245 ms 26104 KB Output is correct
24 Correct 258 ms 26108 KB Output is correct
25 Correct 104 ms 16120 KB Output is correct
26 Correct 100 ms 16120 KB Output is correct
27 Correct 244 ms 26232 KB Output is correct
28 Correct 255 ms 26096 KB Output is correct
29 Correct 106 ms 16120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 62 ms 6776 KB Output is correct
5 Correct 249 ms 26104 KB Output is correct
6 Correct 249 ms 26104 KB Output is correct
7 Correct 261 ms 26204 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 62 ms 6776 KB Output is correct
10 Correct 245 ms 26104 KB Output is correct
11 Correct 254 ms 26104 KB Output is correct
12 Correct 262 ms 26104 KB Output is correct
13 Correct 1 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 61 ms 6776 KB Output is correct
17 Correct 253 ms 26108 KB Output is correct
18 Correct 248 ms 26104 KB Output is correct
19 Correct 243 ms 25976 KB Output is correct
20 Correct 240 ms 26232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 11 ms 1408 KB Output is correct
7 Correct 254 ms 26104 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 10 ms 1408 KB Output is correct
13 Correct 244 ms 26104 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 5 ms 1024 KB Output is correct
17 Correct 125 ms 16120 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 61 ms 6776 KB Output is correct
21 Correct 245 ms 26104 KB Output is correct
22 Correct 244 ms 26244 KB Output is correct
23 Correct 257 ms 26116 KB Output is correct
24 Correct 243 ms 26104 KB Output is correct
25 Correct 102 ms 16120 KB Output is correct
26 Correct 101 ms 16120 KB Output is correct
27 Correct 262 ms 26128 KB Output is correct
28 Correct 243 ms 26072 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 0 ms 256 KB Output is correct
31 Correct 0 ms 256 KB Output is correct
32 Correct 1 ms 384 KB Output is correct
33 Correct 0 ms 256 KB Output is correct
34 Correct 0 ms 256 KB Output is correct
35 Correct 0 ms 256 KB Output is correct
36 Correct 12 ms 1408 KB Output is correct
37 Correct 244 ms 26104 KB Output is correct
38 Correct 0 ms 256 KB Output is correct
39 Correct 0 ms 256 KB Output is correct
40 Correct 11 ms 1408 KB Output is correct
41 Correct 250 ms 26072 KB Output is correct
42 Correct 0 ms 256 KB Output is correct
43 Correct 0 ms 256 KB Output is correct
44 Correct 5 ms 1024 KB Output is correct
45 Correct 115 ms 16212 KB Output is correct
46 Correct 0 ms 256 KB Output is correct
47 Correct 0 ms 256 KB Output is correct
48 Correct 0 ms 256 KB Output is correct
49 Correct 61 ms 6776 KB Output is correct
50 Correct 246 ms 26104 KB Output is correct
51 Correct 245 ms 26104 KB Output is correct
52 Correct 258 ms 26108 KB Output is correct
53 Correct 104 ms 16120 KB Output is correct
54 Correct 100 ms 16120 KB Output is correct
55 Correct 244 ms 26232 KB Output is correct
56 Correct 255 ms 26096 KB Output is correct
57 Correct 106 ms 16120 KB Output is correct
58 Correct 0 ms 256 KB Output is correct
59 Correct 1 ms 256 KB Output is correct
60 Correct 5 ms 1024 KB Output is correct
61 Correct 114 ms 16180 KB Output is correct
62 Correct 0 ms 256 KB Output is correct
63 Correct 0 ms 256 KB Output is correct
64 Correct 0 ms 256 KB Output is correct
65 Correct 61 ms 6776 KB Output is correct
66 Correct 105 ms 16120 KB Output is correct
67 Correct 102 ms 16120 KB Output is correct
68 Correct 102 ms 16120 KB Output is correct
69 Correct 102 ms 16120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 11 ms 1408 KB Output is correct
7 Correct 254 ms 26104 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 10 ms 1408 KB Output is correct
13 Correct 244 ms 26104 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 5 ms 1024 KB Output is correct
17 Correct 125 ms 16120 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 61 ms 6776 KB Output is correct
21 Correct 245 ms 26104 KB Output is correct
22 Correct 244 ms 26244 KB Output is correct
23 Correct 257 ms 26116 KB Output is correct
24 Correct 243 ms 26104 KB Output is correct
25 Correct 102 ms 16120 KB Output is correct
26 Correct 101 ms 16120 KB Output is correct
27 Correct 262 ms 26128 KB Output is correct
28 Correct 243 ms 26072 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 0 ms 256 KB Output is correct
31 Correct 0 ms 256 KB Output is correct
32 Correct 1 ms 384 KB Output is correct
33 Correct 0 ms 256 KB Output is correct
34 Correct 0 ms 256 KB Output is correct
35 Correct 0 ms 256 KB Output is correct
36 Correct 12 ms 1408 KB Output is correct
37 Correct 244 ms 26104 KB Output is correct
38 Correct 0 ms 256 KB Output is correct
39 Correct 0 ms 256 KB Output is correct
40 Correct 11 ms 1408 KB Output is correct
41 Correct 250 ms 26072 KB Output is correct
42 Correct 0 ms 256 KB Output is correct
43 Correct 0 ms 256 KB Output is correct
44 Correct 5 ms 1024 KB Output is correct
45 Correct 115 ms 16212 KB Output is correct
46 Correct 0 ms 256 KB Output is correct
47 Correct 0 ms 256 KB Output is correct
48 Correct 0 ms 256 KB Output is correct
49 Correct 61 ms 6776 KB Output is correct
50 Correct 246 ms 26104 KB Output is correct
51 Correct 245 ms 26104 KB Output is correct
52 Correct 258 ms 26108 KB Output is correct
53 Correct 104 ms 16120 KB Output is correct
54 Correct 100 ms 16120 KB Output is correct
55 Correct 244 ms 26232 KB Output is correct
56 Correct 255 ms 26096 KB Output is correct
57 Correct 106 ms 16120 KB Output is correct
58 Correct 1 ms 256 KB Output is correct
59 Correct 0 ms 256 KB Output is correct
60 Correct 1 ms 256 KB Output is correct
61 Correct 62 ms 6776 KB Output is correct
62 Correct 249 ms 26104 KB Output is correct
63 Correct 249 ms 26104 KB Output is correct
64 Correct 261 ms 26204 KB Output is correct
65 Correct 0 ms 256 KB Output is correct
66 Correct 62 ms 6776 KB Output is correct
67 Correct 245 ms 26104 KB Output is correct
68 Correct 254 ms 26104 KB Output is correct
69 Correct 262 ms 26104 KB Output is correct
70 Correct 1 ms 256 KB Output is correct
71 Correct 0 ms 256 KB Output is correct
72 Correct 0 ms 256 KB Output is correct
73 Correct 61 ms 6776 KB Output is correct
74 Correct 253 ms 26108 KB Output is correct
75 Correct 248 ms 26104 KB Output is correct
76 Correct 243 ms 25976 KB Output is correct
77 Correct 240 ms 26232 KB Output is correct
78 Correct 0 ms 256 KB Output is correct
79 Correct 1 ms 256 KB Output is correct
80 Correct 5 ms 1024 KB Output is correct
81 Correct 114 ms 16180 KB Output is correct
82 Correct 0 ms 256 KB Output is correct
83 Correct 0 ms 256 KB Output is correct
84 Correct 0 ms 256 KB Output is correct
85 Correct 61 ms 6776 KB Output is correct
86 Correct 105 ms 16120 KB Output is correct
87 Correct 102 ms 16120 KB Output is correct
88 Correct 102 ms 16120 KB Output is correct
89 Correct 102 ms 16120 KB Output is correct
90 Correct 0 ms 256 KB Output is correct
91 Correct 0 ms 256 KB Output is correct
92 Correct 5 ms 896 KB Output is correct
93 Correct 106 ms 16120 KB Output is correct
94 Correct 0 ms 256 KB Output is correct
95 Correct 0 ms 256 KB Output is correct
96 Correct 0 ms 256 KB Output is correct
97 Correct 25 ms 4220 KB Output is correct
98 Correct 110 ms 16120 KB Output is correct
99 Correct 98 ms 16120 KB Output is correct
100 Correct 97 ms 16120 KB Output is correct
101 Correct 97 ms 16120 KB Output is correct
102 Correct 102 ms 16120 KB Output is correct
103 Correct 101 ms 16120 KB Output is correct
104 Correct 102 ms 16120 KB Output is correct
105 Correct 103 ms 16120 KB Output is correct