답안 #372466

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
372466 2021-02-28T09:33:26 Z peijar 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
290 ms 24172 KB
#include "supertrees.h"
#include <bits/stdc++.h>
#define SZ(v) ((int)(v).size())
using namespace std;

using ll = long long;

struct UnionFind
{
	vector<int> id;
	
	UnionFind() {}
	UnionFind(int N)
	{
		id.assign(N, -1);
	}

	int find(int u)
	{
		if (id[u] < 0) return u;
		return id[u] = find(id[u]);
	}

	bool merge(int u, int v)
	{
		u = find(u), v = find(v);
		if (u == v) return false;
		if (id[u] > id[v]) swap(u, v);
		id[u] += id[v];
		id[v] = u;
		return true;
	}

	vector<vector<int>> getComps()
	{
		vector<vector<int>> ret;
		vector<vector<int>> comps(SZ(id));
		for (int i(0); i < SZ(id); ++i)
			comps[find(i)].push_back(i);
		for (auto v : comps)
			if (SZ(v))
				ret.push_back(v);
		return ret;
	}
};


int construct(vector<vector<int>> p) 
{
	int nbSommets = SZ(p);
	vector<vector<int>> sol(nbSommets, vector<int>(nbSommets));

	UnionFind uf(nbSommets);
	for (int i(0); i < nbSommets; ++i)
		for (int j(i+1); j < nbSommets; ++j)
			if (p[i][j] == 3)
				return 0;
	for (int i(0); i < nbSommets; ++i)
		for (int j(i+1); j < nbSommets; ++j)
			if (p[i][j] == 1)
				uf.merge(i,j);
	for (int i(0); i < nbSommets; ++i)
		for (int j(i+1); j < nbSommets; ++j)
			if (uf.find(i) == uf.find(j) and p[i][j] != 1)
				return 0;
	for (int i(0); i< nbSommets; ++i)
		if (uf.find(i) != i)
			sol[uf.find(i)][i] = sol[i][uf.find(i)] = 1;
	UnionFind uf2(nbSommets);
	for (int i(0); i < nbSommets; ++i)
		for (int j(i+1); j < nbSommets; ++j)
			if (p[i][j])
				uf2.merge(i, j);
	for (int i(0); i < nbSommets; ++i)
		for (int j(i+1); j < nbSommets; ++j)
			if (uf2.find(i) == uf2.find(j) and uf.find(i) != uf.find(j) and p[i][j] != 2)
				return 0; 
	auto comps = uf2.getComps();
	for (auto v : comps)
	{
		vector<int> roots;
		for (auto u : v)
			if (uf.find(u) == u)
				roots.push_back(u);
		if (SZ(roots) == 1) continue;
		bool aTwo(false);
		for (auto u : v)
			for (auto w : v)
				if (p[u][w] == 2)
					aTwo = true;
		if (aTwo and SZ(roots) <= 2)
			return 0;
		roots.push_back(roots.front());
		for (int i(1); i < SZ(roots); ++i)
			sol[roots[i-1]][roots[i]] = sol[roots[i]][roots[i-1]] = true;
	}
	build(sol);
	return 1;
}
# 결과 실행 시간 메모리 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 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 11 ms 1260 KB Output is correct
7 Correct 262 ms 22124 KB Output is correct
# 결과 실행 시간 메모리 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 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 11 ms 1260 KB Output is correct
7 Correct 262 ms 22124 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 11 ms 1260 KB Output is correct
13 Correct 253 ms 22124 KB Output is correct
14 Correct 0 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 5 ms 748 KB Output is correct
17 Correct 134 ms 12140 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 65 ms 5868 KB Output is correct
21 Correct 264 ms 22252 KB Output is correct
22 Correct 252 ms 22124 KB Output is correct
23 Correct 267 ms 22124 KB Output is correct
24 Correct 256 ms 22124 KB Output is correct
25 Correct 111 ms 12140 KB Output is correct
26 Correct 106 ms 12140 KB Output is correct
27 Correct 278 ms 22124 KB Output is correct
28 Correct 257 ms 22124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 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 364 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 512 KB Output is correct
8 Correct 13 ms 1260 KB Output is correct
9 Correct 255 ms 24172 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 255 ms 24044 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 116 ms 14188 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 66 ms 6272 KB Output is correct
22 Correct 253 ms 24044 KB Output is correct
23 Correct 288 ms 24160 KB Output is correct
24 Correct 267 ms 24044 KB Output is correct
25 Correct 109 ms 14188 KB Output is correct
26 Correct 112 ms 14188 KB Output is correct
27 Correct 255 ms 24172 KB Output is correct
28 Correct 264 ms 24044 KB Output is correct
29 Correct 111 ms 14316 KB Output is correct
# 결과 실행 시간 메모리 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 76 ms 5864 KB Output is correct
5 Correct 277 ms 22124 KB Output is correct
6 Correct 254 ms 22124 KB Output is correct
7 Correct 290 ms 22124 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 63 ms 5868 KB Output is correct
10 Correct 250 ms 22124 KB Output is correct
11 Correct 280 ms 22228 KB Output is correct
12 Correct 264 ms 22124 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 73 ms 5848 KB Output is correct
17 Correct 255 ms 22124 KB Output is correct
18 Correct 264 ms 22124 KB Output is correct
19 Correct 257 ms 22124 KB Output is correct
20 Correct 266 ms 22252 KB Output is correct
# 결과 실행 시간 메모리 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 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 11 ms 1260 KB Output is correct
7 Correct 262 ms 22124 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 11 ms 1260 KB Output is correct
13 Correct 253 ms 22124 KB Output is correct
14 Correct 0 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 5 ms 748 KB Output is correct
17 Correct 134 ms 12140 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 65 ms 5868 KB Output is correct
21 Correct 264 ms 22252 KB Output is correct
22 Correct 252 ms 22124 KB Output is correct
23 Correct 267 ms 22124 KB Output is correct
24 Correct 256 ms 22124 KB Output is correct
25 Correct 111 ms 12140 KB Output is correct
26 Correct 106 ms 12140 KB Output is correct
27 Correct 278 ms 22124 KB Output is correct
28 Correct 257 ms 22124 KB Output is correct
29 Correct 1 ms 492 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 364 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 512 KB Output is correct
36 Correct 13 ms 1260 KB Output is correct
37 Correct 255 ms 24172 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 255 ms 24044 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 116 ms 14188 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 66 ms 6272 KB Output is correct
50 Correct 253 ms 24044 KB Output is correct
51 Correct 288 ms 24160 KB Output is correct
52 Correct 267 ms 24044 KB Output is correct
53 Correct 109 ms 14188 KB Output is correct
54 Correct 112 ms 14188 KB Output is correct
55 Correct 255 ms 24172 KB Output is correct
56 Correct 264 ms 24044 KB Output is correct
57 Correct 111 ms 14316 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 124 ms 14188 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 6380 KB Output is correct
66 Correct 117 ms 14176 KB Output is correct
67 Correct 111 ms 14228 KB Output is correct
68 Correct 113 ms 14188 KB Output is correct
69 Correct 109 ms 14188 KB Output is correct
# 결과 실행 시간 메모리 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 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 11 ms 1260 KB Output is correct
7 Correct 262 ms 22124 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 11 ms 1260 KB Output is correct
13 Correct 253 ms 22124 KB Output is correct
14 Correct 0 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 5 ms 748 KB Output is correct
17 Correct 134 ms 12140 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 65 ms 5868 KB Output is correct
21 Correct 264 ms 22252 KB Output is correct
22 Correct 252 ms 22124 KB Output is correct
23 Correct 267 ms 22124 KB Output is correct
24 Correct 256 ms 22124 KB Output is correct
25 Correct 111 ms 12140 KB Output is correct
26 Correct 106 ms 12140 KB Output is correct
27 Correct 278 ms 22124 KB Output is correct
28 Correct 257 ms 22124 KB Output is correct
29 Correct 1 ms 492 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 364 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 512 KB Output is correct
36 Correct 13 ms 1260 KB Output is correct
37 Correct 255 ms 24172 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 255 ms 24044 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 116 ms 14188 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 66 ms 6272 KB Output is correct
50 Correct 253 ms 24044 KB Output is correct
51 Correct 288 ms 24160 KB Output is correct
52 Correct 267 ms 24044 KB Output is correct
53 Correct 109 ms 14188 KB Output is correct
54 Correct 112 ms 14188 KB Output is correct
55 Correct 255 ms 24172 KB Output is correct
56 Correct 264 ms 24044 KB Output is correct
57 Correct 111 ms 14316 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 364 KB Output is correct
61 Correct 76 ms 5864 KB Output is correct
62 Correct 277 ms 22124 KB Output is correct
63 Correct 254 ms 22124 KB Output is correct
64 Correct 290 ms 22124 KB Output is correct
65 Correct 1 ms 364 KB Output is correct
66 Correct 63 ms 5868 KB Output is correct
67 Correct 250 ms 22124 KB Output is correct
68 Correct 280 ms 22228 KB Output is correct
69 Correct 264 ms 22124 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 73 ms 5848 KB Output is correct
74 Correct 255 ms 22124 KB Output is correct
75 Correct 264 ms 22124 KB Output is correct
76 Correct 257 ms 22124 KB Output is correct
77 Correct 266 ms 22252 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 124 ms 14188 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 6380 KB Output is correct
86 Correct 117 ms 14176 KB Output is correct
87 Correct 111 ms 14228 KB Output is correct
88 Correct 113 ms 14188 KB Output is correct
89 Correct 109 ms 14188 KB Output is correct
90 Correct 1 ms 364 KB Output is correct
91 Correct 1 ms 364 KB Output is correct
92 Correct 5 ms 876 KB Output is correct
93 Correct 129 ms 14176 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 27 ms 3820 KB Output is correct
98 Correct 108 ms 14136 KB Output is correct
99 Correct 104 ms 14188 KB Output is correct
100 Correct 108 ms 14176 KB Output is correct
101 Correct 105 ms 14176 KB Output is correct
102 Correct 107 ms 14188 KB Output is correct
103 Correct 104 ms 14188 KB Output is correct
104 Correct 106 ms 14188 KB Output is correct
105 Correct 111 ms 14316 KB Output is correct