Submission #788289

# Submission time Handle Problem Language Result Execution time Memory
788289 2023-07-20T04:42:23 Z mannshah1211 Connecting Supertrees (IOI20_supertrees) C++17
40 / 100
178 ms 24052 KB
#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;



void build(vector<vector<int>> b);

struct DSU {
	vector<int> link, siz;
	DSU (int n) : link(n), siz(n, 1) {
		iota(link.begin(), link.end(), 0ll);
	}
	
	int get(int x) {
		if (link[x] == x) {
			return x;
		}
		return link[x] = get(link[x]);
	}
	
	bool unite(int a, int b) {
	   a = get(a), b = get(b);
	   if (a == b) {
	   	return false;
	   }
	   
	   if (siz[a] < siz[b]) {
	   	swap(a, b);
	   }
	   link[b] = a;
	   siz[a] += siz[b];
	   return true;
	}
	
	bool same(int a, int b) {
		return (get(a) == get(b));
	}
	
};

int construct(vector<vector<int>> p) {
	vector<vector<int>> b(p.size(), vector<int>(p.size()));
	DSU ds(p.size());
	
	bool one = true, two = true;
	for (int i = 0; i < p.size(); i++) {
		for (int j = i + 1; j < p.size(); j++) {
			if (p[i][j] != 0 && p[i][j] != 2) {
				two = false;
			}
			if (p[i][j] != 0 && p[i][j] != 1) {
				one = false;
			}
		}
	}
	
	if (one) {
		for (int i = 0; i < p.size(); i++) {
		  for (int j = i + 1; j < p.size(); j++) {
			  if (p[i][j] != 0) {
				  ds.unite(i, j);
			  }
		  }
  	}
	
	 for (int i = 0; i < p.size(); i++) {
		 for (int j = i + 1; j < p.size(); j++) {
			 if (p[i][j] == 0 && ds.same(i, j)) {
				return 0;
			 } else if (p[i][j] == 1 && !ds.same(i, j)) {
				return 0;
			 }
		 }
	 }
	
	 vector<vector<int>> conncomps(p.size());
	 for (int i = 0; i < p.size(); i++) {
	  	conncomps[ds.get(i)].push_back(i);
   }
	
	 for (int i = 0; i < conncomps.size(); i++) {
		 for (int j = 0; j < (int) conncomps[i].size() - 1; j++) {
			 b[conncomps[i][j]][conncomps[i][j + 1]] = 1;
		   b[conncomps[i][j + 1]][conncomps[i][j]] = 1;
		 }
	 }
	
	 build(b);
	 return 1;
	 }
	 for (int i = 0; i < p.size(); i++) {
		 for (int j = i + 1; j < p.size(); j++) {
			 if (p[i][j] != 0) {
				 ds.unite(i, j);
			 }
		 }
	 }
	
	 for (int i = 0; i < p.size(); i++) {
		 for (int j = i + 1; j < p.size(); j++) {
		 	 if (p[i][j] == 0 && ds.same(i, j)) {
			   return 0;
			 } else if (p[i][j] == 1 && !ds.same(i, j)) {
				 return 0;
			 }
		 }
	 }
	
	 vector<vector<int>> conncomps(p.size());
	 for (int i = 0; i < p.size(); i++) {
		 conncomps[ds.get(i)].push_back(i);
	 }
	
	 for (int i = 0; i < conncomps.size(); i++) {
		if (conncomps[i].size() < 2) {
			continue;
		}
		if (conncomps[i].size() == 2) {
			return 0;
		}
		for (int j = 0; j < conncomps[i].size(); j++) {
			b[conncomps[i][j]][conncomps[i][(j + 1) % conncomps[i].size()]] = 1;
			b[conncomps[i][(j + 1) % conncomps[i].size()]][conncomps[i][j]] = 1;
		}
	}
	
	build(b);
	return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:47:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |  for (int i = 0; i < p.size(); i++) {
      |                  ~~^~~~~~~~~~
supertrees.cpp:48:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |   for (int j = i + 1; j < p.size(); j++) {
      |                       ~~^~~~~~~~~~
supertrees.cpp:59:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |   for (int i = 0; i < p.size(); i++) {
      |                   ~~^~~~~~~~~~
supertrees.cpp:60:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |     for (int j = i + 1; j < p.size(); j++) {
      |                         ~~^~~~~~~~~~
supertrees.cpp:67:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |   for (int i = 0; i < p.size(); i++) {
      |                   ~~^~~~~~~~~~
supertrees.cpp:68:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |    for (int j = i + 1; j < p.size(); j++) {
      |                        ~~^~~~~~~~~~
supertrees.cpp:78:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |   for (int i = 0; i < p.size(); i++) {
      |                   ~~^~~~~~~~~~
supertrees.cpp:82:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |   for (int i = 0; i < conncomps.size(); i++) {
      |                   ~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:92:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |   for (int i = 0; i < p.size(); i++) {
      |                   ~~^~~~~~~~~~
supertrees.cpp:93:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |    for (int j = i + 1; j < p.size(); j++) {
      |                        ~~^~~~~~~~~~
supertrees.cpp:100:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |   for (int i = 0; i < p.size(); i++) {
      |                   ~~^~~~~~~~~~
supertrees.cpp:101:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |    for (int j = i + 1; j < p.size(); j++) {
      |                        ~~^~~~~~~~~~
supertrees.cpp:111:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |   for (int i = 0; i < p.size(); i++) {
      |                   ~~^~~~~~~~~~
supertrees.cpp:115:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  115 |   for (int i = 0; i < conncomps.size(); i++) {
      |                   ~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:122:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |   for (int j = 0; j < conncomps[i].size(); j++) {
      |                   ~~^~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:46:19: warning: variable 'two' set but not used [-Wunused-but-set-variable]
   46 |  bool one = true, two = true;
      |                   ^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1168 KB Output is correct
7 Correct 155 ms 22032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1168 KB Output is correct
7 Correct 155 ms 22032 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 6 ms 1124 KB Output is correct
13 Correct 149 ms 22032 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 724 KB Output is correct
17 Correct 72 ms 12044 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 38 ms 5732 KB Output is correct
21 Correct 152 ms 22024 KB Output is correct
22 Correct 152 ms 22064 KB Output is correct
23 Correct 158 ms 22004 KB Output is correct
24 Correct 175 ms 22028 KB Output is correct
25 Correct 74 ms 12296 KB Output is correct
26 Correct 65 ms 12076 KB Output is correct
27 Correct 157 ms 22092 KB Output is correct
28 Correct 161 ms 21996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 7 ms 1108 KB Output is correct
9 Correct 152 ms 22008 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1108 KB Output is correct
13 Correct 156 ms 22016 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 724 KB Output is correct
17 Correct 72 ms 12036 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 39 ms 5772 KB Output is correct
22 Correct 155 ms 22220 KB Output is correct
23 Correct 153 ms 22160 KB Output is correct
24 Correct 162 ms 22164 KB Output is correct
25 Correct 68 ms 12312 KB Output is correct
26 Correct 77 ms 12200 KB Output is correct
27 Correct 155 ms 22252 KB Output is correct
28 Correct 178 ms 22184 KB Output is correct
29 Correct 67 ms 12304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 276 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 38 ms 6248 KB Output is correct
5 Correct 157 ms 23272 KB Output is correct
6 Correct 154 ms 23176 KB Output is correct
7 Correct 159 ms 23192 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 40 ms 6244 KB Output is correct
10 Correct 152 ms 23936 KB Output is correct
11 Correct 156 ms 23920 KB Output is correct
12 Correct 160 ms 24052 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Incorrect 1 ms 212 KB Too many ways to get from 4 to 8, should be 1 found no less than 2
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1168 KB Output is correct
7 Correct 155 ms 22032 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 6 ms 1124 KB Output is correct
13 Correct 149 ms 22032 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 724 KB Output is correct
17 Correct 72 ms 12044 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 38 ms 5732 KB Output is correct
21 Correct 152 ms 22024 KB Output is correct
22 Correct 152 ms 22064 KB Output is correct
23 Correct 158 ms 22004 KB Output is correct
24 Correct 175 ms 22028 KB Output is correct
25 Correct 74 ms 12296 KB Output is correct
26 Correct 65 ms 12076 KB Output is correct
27 Correct 157 ms 22092 KB Output is correct
28 Correct 161 ms 21996 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 7 ms 1108 KB Output is correct
37 Correct 152 ms 22008 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 7 ms 1108 KB Output is correct
41 Correct 156 ms 22016 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 3 ms 724 KB Output is correct
45 Correct 72 ms 12036 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 39 ms 5772 KB Output is correct
50 Correct 155 ms 22220 KB Output is correct
51 Correct 153 ms 22160 KB Output is correct
52 Correct 162 ms 22164 KB Output is correct
53 Correct 68 ms 12312 KB Output is correct
54 Correct 77 ms 12200 KB Output is correct
55 Correct 155 ms 22252 KB Output is correct
56 Correct 178 ms 22184 KB Output is correct
57 Correct 67 ms 12304 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 3 ms 852 KB Output is correct
61 Correct 73 ms 14064 KB Output is correct
62 Correct 0 ms 212 KB Output is correct
63 Incorrect 0 ms 212 KB Too many ways to get from 0 to 9, should be 1 found no less than 2
64 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1168 KB Output is correct
7 Correct 155 ms 22032 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 6 ms 1124 KB Output is correct
13 Correct 149 ms 22032 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 724 KB Output is correct
17 Correct 72 ms 12044 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 38 ms 5732 KB Output is correct
21 Correct 152 ms 22024 KB Output is correct
22 Correct 152 ms 22064 KB Output is correct
23 Correct 158 ms 22004 KB Output is correct
24 Correct 175 ms 22028 KB Output is correct
25 Correct 74 ms 12296 KB Output is correct
26 Correct 65 ms 12076 KB Output is correct
27 Correct 157 ms 22092 KB Output is correct
28 Correct 161 ms 21996 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 7 ms 1108 KB Output is correct
37 Correct 152 ms 22008 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 7 ms 1108 KB Output is correct
41 Correct 156 ms 22016 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 3 ms 724 KB Output is correct
45 Correct 72 ms 12036 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 39 ms 5772 KB Output is correct
50 Correct 155 ms 22220 KB Output is correct
51 Correct 153 ms 22160 KB Output is correct
52 Correct 162 ms 22164 KB Output is correct
53 Correct 68 ms 12312 KB Output is correct
54 Correct 77 ms 12200 KB Output is correct
55 Correct 155 ms 22252 KB Output is correct
56 Correct 178 ms 22184 KB Output is correct
57 Correct 67 ms 12304 KB Output is correct
58 Correct 1 ms 276 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 38 ms 6248 KB Output is correct
62 Correct 157 ms 23272 KB Output is correct
63 Correct 154 ms 23176 KB Output is correct
64 Correct 159 ms 23192 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 40 ms 6244 KB Output is correct
67 Correct 152 ms 23936 KB Output is correct
68 Correct 156 ms 23920 KB Output is correct
69 Correct 160 ms 24052 KB Output is correct
70 Correct 0 ms 212 KB Output is correct
71 Incorrect 1 ms 212 KB Too many ways to get from 4 to 8, should be 1 found no less than 2
72 Halted 0 ms 0 KB -