Submission #305321

# Submission time Handle Problem Language Result Execution time Memory
305321 2020-09-22T23:59:42 Z JustInCase Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
255 ms 22396 KB
#include "supertrees.h"
#include <vector>
#include <iostream>

#define int32_t int
#define int64_t long long

const int32_t MAX_N = 1000;

struct Dsu {
	int32_t parent[MAX_N + 5], siz[MAX_N + 5];	
	
	void init(int32_t n) {
		for(int32_t i = 0; i < n; i++) {
			parent[i] = i;
			siz[i] = 1;
		}
	}

	void unite(int32_t x, int32_t y) {
		if(siz[x] < siz[y]) {
			parent[x] = y;
			siz[y] += siz[x];
		}
		else {
			parent[y] = x;
			siz[x] += siz[y];
		}
	}

	int32_t find_parent(int32_t x) {
		if(parent[x] == x) {
			return x;
		}
		
		parent[x] = find_parent(parent[x]);
		return parent[x];
	}
};

Dsu dsuLoops, dsuBranches;

int32_t construct(std::vector< std::vector< int32_t > > p) {
	int32_t n = p.size();

	dsuLoops.init(n);
	dsuBranches.init(n);

	for(int32_t i = 0; i < n; i++) {
		for(int32_t j = i + 1; j < n; j++) {
			if(p[i][j] == 3) {
				return 0;
			}

			if(p[i][j] == 1) {
				int32_t parI = dsuBranches.find_parent(i);
				int32_t parJ = dsuBranches.find_parent(j);

				if(parI != parJ) {
					dsuBranches.unite(parI, parJ);
				}
			}
		}
	}
	
	std::vector< std::vector< int32_t > > branches(n);
	std::vector< int32_t > roots;
	for(int32_t i = 0; i < n; i++) {
		branches[dsuBranches.find_parent(i)].push_back(i);
	}

	std::vector< std::vector< int32_t > > res(n, std::vector< int32_t >(n, 0)); 
	for(int32_t i = 0; i < n; i++) {
		if(branches[i].size() > 0) {
			for(int32_t a = 0; a < branches[i].size(); a++) {
				for(int32_t b = a + 1; b < branches[i].size(); b++) {
					if(p[branches[i][a]][branches[i][b]] != 1) {
						return 0;
					}
				}
			}

			roots.push_back(i);
			for(int32_t j = 1; j < branches[i].size(); j++) {
				res[branches[i][j - 1]][branches[i][j]] = res[branches[i][j]][branches[i][j - 1]] = 1;
			}
		}
	}

	for(int32_t i = 0; i < roots.size(); i++) {
		for(int32_t j = i + 1; j < roots.size(); j++) {
			if(p[roots[i]][roots[j]] == 2) {
				int32_t parI = dsuLoops.find_parent(roots[i]);
				int32_t parJ = dsuLoops.find_parent(roots[j]);

				if(parI != parJ) {
					dsuLoops.unite(parI, parJ);
				}
			}
		}
	}

	std::vector< std::vector< int32_t > > loops(n);
	for(int32_t i = 0; i < n; i++) {
		int32_t par = dsuLoops.find_parent(i);

		loops[par].push_back(i);
	}
	
	/**
	for(int32_t i = 0; i < n; i++) {
		std::cout << i << ": ";
		for(auto &x : loops[i]) {
			std::cout << x << " ";
		}
		std::cout << '\n';
	}
	*/

	for(int32_t i = 0; i < n; i++) {
		if(loops[i].size() == 2) {
			return 0;
		}
		
		for(int32_t a = 0; a < loops[i].size(); a++) {
			for(int32_t b = a + 1; b < loops[i].size(); b++) {
				if(p[loops[i][a]][loops[i][b]] != 2) {
					return 0;
				}
			}
		}

		if(loops[i].size() > 2) {
			for(int32_t j = 0; j < loops[i].size() - 1; j++) {
				res[loops[i][j]][loops[i][j + 1]] = res[loops[i][j + 1]][loops[i][j]] = 1;
			}
			res[loops[i][0]][loops[i].back()] = res[loops[i].back()][loops[i][0]] = 1;
		}
	}
	
	build(res);
	return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:75:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |    for(int32_t a = 0; a < branches[i].size(); a++) {
      |                       ~~^~~~~~~~~~~~~~~~~~~~
supertrees.cpp:76:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |     for(int32_t b = a + 1; b < branches[i].size(); b++) {
      |                            ~~^~~~~~~~~~~~~~~~~~~~
supertrees.cpp:84:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |    for(int32_t j = 1; j < branches[i].size(); j++) {
      |                       ~~^~~~~~~~~~~~~~~~~~~~
supertrees.cpp:90:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |  for(int32_t i = 0; i < roots.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
supertrees.cpp:91:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |   for(int32_t j = i + 1; j < roots.size(); j++) {
      |                          ~~^~~~~~~~~~~~~~
supertrees.cpp:125:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  125 |   for(int32_t a = 0; a < loops[i].size(); a++) {
      |                      ~~^~~~~~~~~~~~~~~~~
supertrees.cpp:126:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  126 |    for(int32_t b = a + 1; b < loops[i].size(); b++) {
      |                           ~~^~~~~~~~~~~~~~~~~
supertrees.cpp:134:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  134 |    for(int32_t j = 0; j < loops[i].size() - 1; j++) {
      |                       ~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 10 ms 1280 KB Output is correct
7 Correct 244 ms 22264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 10 ms 1280 KB Output is correct
7 Correct 244 ms 22264 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 10 ms 1280 KB Output is correct
13 Correct 246 ms 22240 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 5 ms 768 KB Output is correct
17 Correct 119 ms 12252 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 60 ms 5880 KB Output is correct
21 Correct 244 ms 22136 KB Output is correct
22 Correct 248 ms 22136 KB Output is correct
23 Correct 253 ms 22264 KB Output is correct
24 Correct 242 ms 22396 KB Output is correct
25 Correct 108 ms 12252 KB Output is correct
26 Correct 103 ms 12152 KB Output is correct
27 Correct 254 ms 22264 KB Output is correct
28 Correct 242 ms 22364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 10 ms 1280 KB Output is correct
9 Correct 243 ms 22236 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 10 ms 1280 KB Output is correct
13 Correct 247 ms 22236 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 5 ms 768 KB Output is correct
17 Correct 118 ms 12252 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 60 ms 5880 KB Output is correct
22 Correct 246 ms 22232 KB Output is correct
23 Correct 238 ms 22232 KB Output is correct
24 Correct 252 ms 22264 KB Output is correct
25 Correct 106 ms 12280 KB Output is correct
26 Correct 109 ms 12244 KB Output is correct
27 Correct 239 ms 22136 KB Output is correct
28 Correct 254 ms 22136 KB Output is correct
29 Correct 106 ms 12280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 61 ms 5832 KB Output is correct
5 Correct 240 ms 22296 KB Output is correct
6 Correct 247 ms 22136 KB Output is correct
7 Correct 255 ms 22136 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 62 ms 5880 KB Output is correct
10 Correct 241 ms 22136 KB Output is correct
11 Correct 244 ms 22136 KB Output is correct
12 Correct 251 ms 22136 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 61 ms 5948 KB Output is correct
17 Correct 239 ms 22264 KB Output is correct
18 Correct 240 ms 22136 KB Output is correct
19 Correct 239 ms 22136 KB Output is correct
20 Correct 242 ms 22264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 10 ms 1280 KB Output is correct
7 Correct 244 ms 22264 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 10 ms 1280 KB Output is correct
13 Correct 246 ms 22240 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 5 ms 768 KB Output is correct
17 Correct 119 ms 12252 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 60 ms 5880 KB Output is correct
21 Correct 244 ms 22136 KB Output is correct
22 Correct 248 ms 22136 KB Output is correct
23 Correct 253 ms 22264 KB Output is correct
24 Correct 242 ms 22396 KB Output is correct
25 Correct 108 ms 12252 KB Output is correct
26 Correct 103 ms 12152 KB Output is correct
27 Correct 254 ms 22264 KB Output is correct
28 Correct 242 ms 22364 KB Output is correct
29 Correct 0 ms 384 KB Output is correct
30 Correct 0 ms 384 KB Output is correct
31 Correct 0 ms 384 KB Output is correct
32 Correct 0 ms 384 KB Output is correct
33 Correct 0 ms 384 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 0 ms 384 KB Output is correct
36 Correct 10 ms 1280 KB Output is correct
37 Correct 243 ms 22236 KB Output is correct
38 Correct 0 ms 384 KB Output is correct
39 Correct 0 ms 384 KB Output is correct
40 Correct 10 ms 1280 KB Output is correct
41 Correct 247 ms 22236 KB Output is correct
42 Correct 1 ms 384 KB Output is correct
43 Correct 0 ms 384 KB Output is correct
44 Correct 5 ms 768 KB Output is correct
45 Correct 118 ms 12252 KB Output is correct
46 Correct 0 ms 384 KB Output is correct
47 Correct 0 ms 256 KB Output is correct
48 Correct 1 ms 384 KB Output is correct
49 Correct 60 ms 5880 KB Output is correct
50 Correct 246 ms 22232 KB Output is correct
51 Correct 238 ms 22232 KB Output is correct
52 Correct 252 ms 22264 KB Output is correct
53 Correct 106 ms 12280 KB Output is correct
54 Correct 109 ms 12244 KB Output is correct
55 Correct 239 ms 22136 KB Output is correct
56 Correct 254 ms 22136 KB Output is correct
57 Correct 106 ms 12280 KB Output is correct
58 Correct 1 ms 384 KB Output is correct
59 Correct 1 ms 384 KB Output is correct
60 Correct 5 ms 768 KB Output is correct
61 Correct 114 ms 12152 KB Output is correct
62 Correct 1 ms 384 KB Output is correct
63 Correct 0 ms 384 KB Output is correct
64 Correct 0 ms 384 KB Output is correct
65 Correct 61 ms 6016 KB Output is correct
66 Correct 113 ms 12280 KB Output is correct
67 Correct 107 ms 12152 KB Output is correct
68 Correct 105 ms 12280 KB Output is correct
69 Correct 109 ms 12280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 10 ms 1280 KB Output is correct
7 Correct 244 ms 22264 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 10 ms 1280 KB Output is correct
13 Correct 246 ms 22240 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 5 ms 768 KB Output is correct
17 Correct 119 ms 12252 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 60 ms 5880 KB Output is correct
21 Correct 244 ms 22136 KB Output is correct
22 Correct 248 ms 22136 KB Output is correct
23 Correct 253 ms 22264 KB Output is correct
24 Correct 242 ms 22396 KB Output is correct
25 Correct 108 ms 12252 KB Output is correct
26 Correct 103 ms 12152 KB Output is correct
27 Correct 254 ms 22264 KB Output is correct
28 Correct 242 ms 22364 KB Output is correct
29 Correct 0 ms 384 KB Output is correct
30 Correct 0 ms 384 KB Output is correct
31 Correct 0 ms 384 KB Output is correct
32 Correct 0 ms 384 KB Output is correct
33 Correct 0 ms 384 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 0 ms 384 KB Output is correct
36 Correct 10 ms 1280 KB Output is correct
37 Correct 243 ms 22236 KB Output is correct
38 Correct 0 ms 384 KB Output is correct
39 Correct 0 ms 384 KB Output is correct
40 Correct 10 ms 1280 KB Output is correct
41 Correct 247 ms 22236 KB Output is correct
42 Correct 1 ms 384 KB Output is correct
43 Correct 0 ms 384 KB Output is correct
44 Correct 5 ms 768 KB Output is correct
45 Correct 118 ms 12252 KB Output is correct
46 Correct 0 ms 384 KB Output is correct
47 Correct 0 ms 256 KB Output is correct
48 Correct 1 ms 384 KB Output is correct
49 Correct 60 ms 5880 KB Output is correct
50 Correct 246 ms 22232 KB Output is correct
51 Correct 238 ms 22232 KB Output is correct
52 Correct 252 ms 22264 KB Output is correct
53 Correct 106 ms 12280 KB Output is correct
54 Correct 109 ms 12244 KB Output is correct
55 Correct 239 ms 22136 KB Output is correct
56 Correct 254 ms 22136 KB Output is correct
57 Correct 106 ms 12280 KB Output is correct
58 Correct 0 ms 384 KB Output is correct
59 Correct 0 ms 384 KB Output is correct
60 Correct 0 ms 384 KB Output is correct
61 Correct 61 ms 5832 KB Output is correct
62 Correct 240 ms 22296 KB Output is correct
63 Correct 247 ms 22136 KB Output is correct
64 Correct 255 ms 22136 KB Output is correct
65 Correct 1 ms 512 KB Output is correct
66 Correct 62 ms 5880 KB Output is correct
67 Correct 241 ms 22136 KB Output is correct
68 Correct 244 ms 22136 KB Output is correct
69 Correct 251 ms 22136 KB Output is correct
70 Correct 1 ms 384 KB Output is correct
71 Correct 0 ms 384 KB Output is correct
72 Correct 1 ms 384 KB Output is correct
73 Correct 61 ms 5948 KB Output is correct
74 Correct 239 ms 22264 KB Output is correct
75 Correct 240 ms 22136 KB Output is correct
76 Correct 239 ms 22136 KB Output is correct
77 Correct 242 ms 22264 KB Output is correct
78 Correct 1 ms 384 KB Output is correct
79 Correct 1 ms 384 KB Output is correct
80 Correct 5 ms 768 KB Output is correct
81 Correct 114 ms 12152 KB Output is correct
82 Correct 1 ms 384 KB Output is correct
83 Correct 0 ms 384 KB Output is correct
84 Correct 0 ms 384 KB Output is correct
85 Correct 61 ms 6016 KB Output is correct
86 Correct 113 ms 12280 KB Output is correct
87 Correct 107 ms 12152 KB Output is correct
88 Correct 105 ms 12280 KB Output is correct
89 Correct 109 ms 12280 KB Output is correct
90 Correct 0 ms 256 KB Output is correct
91 Correct 0 ms 384 KB Output is correct
92 Correct 5 ms 640 KB Output is correct
93 Correct 110 ms 8156 KB Output is correct
94 Correct 1 ms 384 KB Output is correct
95 Correct 0 ms 256 KB Output is correct
96 Correct 0 ms 384 KB Output is correct
97 Correct 26 ms 2304 KB Output is correct
98 Correct 103 ms 8184 KB Output is correct
99 Correct 106 ms 8284 KB Output is correct
100 Correct 105 ms 8344 KB Output is correct
101 Correct 104 ms 8184 KB Output is correct
102 Correct 106 ms 8184 KB Output is correct
103 Correct 105 ms 8184 KB Output is correct
104 Correct 107 ms 8156 KB Output is correct
105 Correct 108 ms 8184 KB Output is correct