Submission #318220

# Submission time Handle Problem Language Result Execution time Memory
318220 2020-10-31T15:45:35 Z quocnguyen1012 Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
291 ms 22628 KB
#ifdef LOCAL
#include <vector>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <string>

static int n;
static std::vector<std::vector<int>> p;
static std::vector<std::vector<int>> b;
static bool called = false;

static void check(bool cond, std::string message) {
	if (!cond) {
		printf("%s\n", message.c_str());
		fclose(stdout);
		exit(0);
	}
}

void build(std::vector<std::vector<int>> _b) {
	check(!called, "build is called more than once");
	called = true;
	check((int)_b.size() == n, "Invalid number of rows in b");
	for (int i = 0; i < n; i++) {
		check((int)_b[i].size() == n, "Invalid number of columns in b");
	}
	b = _b;
}

#endif // LOCAL

#include <bits/stdc++.h>

#ifndef LOCAL
#include "supertrees.h"
#endif // LOCAL

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back

using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

int construct(std::vector<std::vector<int>> p) {
  int N;
  vector<vector<int>> ans;
  vector<int> lab;

  N = p.size();
  ans = vector<vector<int>>(N, vector<int>(N));
  lab = vector<int>(N, -1);

  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (p[i][j] == 3) {
        return 0;
      }
    }
  }

  auto addedge = [&](int u, int v) {
    ans[u][v] = ans[v][u] = 1;
  };

  auto init = [&](void) {
    lab.assign(N, -1);
  };

  function<int(int)>findset = [&](int u) {
    if (lab[u] < 0) return u;
    return lab[u] = findset(lab[u]);
  };

  function<bool(int, int)>same = [&](int u, int v) {
    return findset(u) == findset(v);
  };

  function<void(int, int)>mergeset = [&](int u, int v) {
    u = findset(u); v = findset(v);
    if (u == v) return;
    if (lab[u] > lab[v]) swap(u, v);
    lab[u] += lab[v];
    lab[v] = u;
    return;
  };

  init();
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (p[i][j] == 0 && same(i, j)) {
        return 0;
      }
      if (p[i][j])
        mergeset(i, j);
    }
  }

  init();
  vector<bool> used(N, false), ok(N, true);
  /// used is have cycle go through v

  for (int i = 0; i < N; ++i) {
    for (int j = i + 1; j < N; ++j) {
      if (p[i][j] == 1) ok[i] = false;
    }
  }


  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < i; ++j) {
      if (p[i][j] == 1 && !same(i, j)) {
        addedge(i, j);
        mergeset(i, j);
      }
    }

    if (!ok[i]) continue;
    if (used[i]) continue;

    bool needstart = false;
    for (int j = i + 1; j < N; ++j) {
      if (p[i][j] == 2 && !used[j])
        needstart = true;
    }

    if (!needstart) continue;

    queue<int> q; q.push(i);
    used[i] = true;
    vector<int> cycle;
    while (q.size()) {
      int u = q.front(); q.pop();
      cycle.eb(u);
      for (int v = 0; v < N; ++v) {
        if (p[u][v] == 2 && !used[v] && ok[v]) {
          used[v] = true;
          q.push(v);
        }
      }
    }
    if (cycle.size() < 3)
      return 0;
    int n = cycle.size();
    for (int i = 0; i < n; ++i) {
      addedge(cycle[i], cycle[(i + 1) % n]);
      mergeset(cycle[i], cycle[(i + 1) % n]);
    }
  }
  build(ans);
  return 1;
}

#ifdef LOCAL
int main() {
#ifdef LOCAL
	freopen("A.INP", "r", stdin);
	freopen("A.OUT", "w", stdout);
#endif // LOCAL
	assert(scanf("%d", &n) == 1);

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

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			assert(scanf("%d", &p[i][j]) == 1);
		}
	}
	fclose(stdin);

	int possible = construct(p);

	check(possible == 0 || possible == 1, "Invalid return value of construct");
	if (possible == 1) {
		check(called, "construct returned 1 without calling build");
	} else {
		check(!called, "construct called build but returned 0");
	}

	printf("%d\n", possible);
	if (possible == 1) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (j) {
					printf(" ");
				}
				printf("%d", b[i][j]);
			}
			printf("\n");
		}
	}
	fclose(stdout);
}
#endif // LOCAL
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 12 ms 1260 KB Output is correct
7 Correct 277 ms 22500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 12 ms 1260 KB Output is correct
7 Correct 277 ms 22500 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 11 ms 1260 KB Output is correct
13 Correct 254 ms 22500 KB Output is correct
14 Correct 1 ms 360 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 876 KB Output is correct
17 Correct 111 ms 12640 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 66 ms 6244 KB Output is correct
21 Correct 271 ms 22516 KB Output is correct
22 Correct 286 ms 22500 KB Output is correct
23 Correct 291 ms 22504 KB Output is correct
24 Correct 255 ms 22500 KB Output is correct
25 Correct 125 ms 12644 KB Output is correct
26 Correct 105 ms 12648 KB Output is correct
27 Correct 291 ms 22504 KB Output is correct
28 Correct 263 ms 22500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 11 ms 1256 KB Output is correct
9 Correct 256 ms 22500 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 11 ms 1260 KB Output is correct
13 Correct 271 ms 22500 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 876 KB Output is correct
17 Correct 106 ms 12644 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 68 ms 6244 KB Output is correct
22 Correct 271 ms 22628 KB Output is correct
23 Correct 269 ms 22500 KB Output is correct
24 Correct 286 ms 22628 KB Output is correct
25 Correct 120 ms 12648 KB Output is correct
26 Correct 102 ms 12644 KB Output is correct
27 Correct 268 ms 22500 KB Output is correct
28 Correct 280 ms 22520 KB Output is correct
29 Correct 119 ms 12644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 67 ms 6244 KB Output is correct
5 Correct 268 ms 22500 KB Output is correct
6 Correct 262 ms 22500 KB Output is correct
7 Correct 291 ms 22500 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 67 ms 6240 KB Output is correct
10 Correct 269 ms 22500 KB Output is correct
11 Correct 267 ms 22500 KB Output is correct
12 Correct 280 ms 22500 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 68 ms 6244 KB Output is correct
17 Correct 280 ms 22500 KB Output is correct
18 Correct 270 ms 22500 KB Output is correct
19 Correct 268 ms 22504 KB Output is correct
20 Correct 253 ms 22628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 12 ms 1260 KB Output is correct
7 Correct 277 ms 22500 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 11 ms 1260 KB Output is correct
13 Correct 254 ms 22500 KB Output is correct
14 Correct 1 ms 360 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 876 KB Output is correct
17 Correct 111 ms 12640 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 66 ms 6244 KB Output is correct
21 Correct 271 ms 22516 KB Output is correct
22 Correct 286 ms 22500 KB Output is correct
23 Correct 291 ms 22504 KB Output is correct
24 Correct 255 ms 22500 KB Output is correct
25 Correct 125 ms 12644 KB Output is correct
26 Correct 105 ms 12648 KB Output is correct
27 Correct 291 ms 22504 KB Output is correct
28 Correct 263 ms 22500 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 492 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 11 ms 1256 KB Output is correct
37 Correct 256 ms 22500 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 11 ms 1260 KB Output is correct
41 Correct 271 ms 22500 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 5 ms 876 KB Output is correct
45 Correct 106 ms 12644 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 1 ms 364 KB Output is correct
49 Correct 68 ms 6244 KB Output is correct
50 Correct 271 ms 22628 KB Output is correct
51 Correct 269 ms 22500 KB Output is correct
52 Correct 286 ms 22628 KB Output is correct
53 Correct 120 ms 12648 KB Output is correct
54 Correct 102 ms 12644 KB Output is correct
55 Correct 268 ms 22500 KB Output is correct
56 Correct 280 ms 22520 KB Output is correct
57 Correct 119 ms 12644 KB Output is correct
58 Correct 1 ms 364 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 5 ms 876 KB Output is correct
61 Correct 109 ms 12644 KB Output is correct
62 Correct 1 ms 364 KB Output is correct
63 Correct 1 ms 364 KB Output is correct
64 Correct 1 ms 364 KB Output is correct
65 Correct 67 ms 6244 KB Output is correct
66 Correct 105 ms 12644 KB Output is correct
67 Correct 103 ms 12648 KB Output is correct
68 Correct 110 ms 12644 KB Output is correct
69 Correct 116 ms 12644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 12 ms 1260 KB Output is correct
7 Correct 277 ms 22500 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 11 ms 1260 KB Output is correct
13 Correct 254 ms 22500 KB Output is correct
14 Correct 1 ms 360 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 876 KB Output is correct
17 Correct 111 ms 12640 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 66 ms 6244 KB Output is correct
21 Correct 271 ms 22516 KB Output is correct
22 Correct 286 ms 22500 KB Output is correct
23 Correct 291 ms 22504 KB Output is correct
24 Correct 255 ms 22500 KB Output is correct
25 Correct 125 ms 12644 KB Output is correct
26 Correct 105 ms 12648 KB Output is correct
27 Correct 291 ms 22504 KB Output is correct
28 Correct 263 ms 22500 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 492 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 11 ms 1256 KB Output is correct
37 Correct 256 ms 22500 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 11 ms 1260 KB Output is correct
41 Correct 271 ms 22500 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 5 ms 876 KB Output is correct
45 Correct 106 ms 12644 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 1 ms 364 KB Output is correct
49 Correct 68 ms 6244 KB Output is correct
50 Correct 271 ms 22628 KB Output is correct
51 Correct 269 ms 22500 KB Output is correct
52 Correct 286 ms 22628 KB Output is correct
53 Correct 120 ms 12648 KB Output is correct
54 Correct 102 ms 12644 KB Output is correct
55 Correct 268 ms 22500 KB Output is correct
56 Correct 280 ms 22520 KB Output is correct
57 Correct 119 ms 12644 KB Output is correct
58 Correct 1 ms 364 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 1 ms 512 KB Output is correct
61 Correct 67 ms 6244 KB Output is correct
62 Correct 268 ms 22500 KB Output is correct
63 Correct 262 ms 22500 KB Output is correct
64 Correct 291 ms 22500 KB Output is correct
65 Correct 1 ms 364 KB Output is correct
66 Correct 67 ms 6240 KB Output is correct
67 Correct 269 ms 22500 KB Output is correct
68 Correct 267 ms 22500 KB Output is correct
69 Correct 280 ms 22500 KB Output is correct
70 Correct 1 ms 364 KB Output is correct
71 Correct 1 ms 364 KB Output is correct
72 Correct 1 ms 364 KB Output is correct
73 Correct 68 ms 6244 KB Output is correct
74 Correct 280 ms 22500 KB Output is correct
75 Correct 270 ms 22500 KB Output is correct
76 Correct 268 ms 22504 KB Output is correct
77 Correct 253 ms 22628 KB Output is correct
78 Correct 1 ms 364 KB Output is correct
79 Correct 1 ms 364 KB Output is correct
80 Correct 5 ms 876 KB Output is correct
81 Correct 109 ms 12644 KB Output is correct
82 Correct 1 ms 364 KB Output is correct
83 Correct 1 ms 364 KB Output is correct
84 Correct 1 ms 364 KB Output is correct
85 Correct 67 ms 6244 KB Output is correct
86 Correct 105 ms 12644 KB Output is correct
87 Correct 103 ms 12648 KB Output is correct
88 Correct 110 ms 12644 KB Output is correct
89 Correct 116 ms 12644 KB Output is correct
90 Correct 1 ms 364 KB Output is correct
91 Correct 1 ms 492 KB Output is correct
92 Correct 5 ms 876 KB Output is correct
93 Correct 104 ms 12644 KB Output is correct
94 Correct 1 ms 364 KB Output is correct
95 Correct 1 ms 364 KB Output is correct
96 Correct 1 ms 364 KB Output is correct
97 Correct 25 ms 3692 KB Output is correct
98 Correct 105 ms 12644 KB Output is correct
99 Correct 106 ms 12644 KB Output is correct
100 Correct 100 ms 12644 KB Output is correct
101 Correct 98 ms 12644 KB Output is correct
102 Correct 100 ms 12644 KB Output is correct
103 Correct 100 ms 12644 KB Output is correct
104 Correct 99 ms 12644 KB Output is correct
105 Correct 109 ms 12644 KB Output is correct