Submission #559763

# Submission time Handle Problem Language Result Execution time Memory
559763 2022-05-10T14:20:11 Z AlperenT Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
268 ms 22408 KB
#include "supertrees.h"
#include <bits/stdc++.h>
 
using namespace std;
 
const int N = 1000 + 5;
 
struct DSU{
	int par[N], type[N];
	set<int> nodes[N];
 
	void reset(int n){
		for(int i = 0; i < n; i++) par[i] = i, type[i] = 0, nodes[i].insert(i);
	}
 
	int setfind(int a){
		if(par[a] == a) return a;
		else return par[a] = setfind(par[a]);
	}
 
	void setunion(int a, int b){
		a = setfind(a), b = setfind(b);

		// cout << a << " " << b << "+\n";
 
		if(a != b){
			if(nodes[b].size() > nodes[a].size()) swap(a, b);
			par[b] = par[a];
			type[b] = -1;
			for(auto x : nodes[b]) nodes[a].insert(x);
			nodes[b].clear();
		}
	}
};
 
DSU dsu;
 
int construct(vector<vector<int>> graph) {
	int n = graph.size();

	for(auto i : graph){
		for(auto j : i){
			if(j == 3) return 0;
		}
	}
 
	vector ans(n, vector(n, 0));
 
	dsu.reset(n);
 
	// finding 3 cycles
 
	set<int> curnodes;
 
	for(int i = 0; i < n; i++) curnodes.insert(i);
 
	for(int v = 0; v < n; v++){
		if(curnodes.count(v) && dsu.type[dsu.setfind(v)] == 0){
			curnodes.erase(v);

			vector<int> nodevec;
 
			for(auto u : curnodes){
				if(graph[v][u] == 3){
					bool flag = true;
 
					for(auto w : nodevec){
						if(graph[u][w] != 3) flag = false;
					}
 
					if(flag) nodevec.push_back(u);
				}
			}

			if(nodevec.size() >= 3){
				for(auto u : nodevec) dsu.setunion(v, u);
				dsu.type[dsu.setfind(v)] = 3;
				for(auto u : nodevec) curnodes.erase(u);

				vector<int> tmp;
				for(auto x : dsu.nodes[dsu.setfind(v)]) tmp.push_back(x);
	 
				for(int i = 0; i + 1 < tmp.size(); i++) ans[tmp[i]][tmp[i + 1]] = ans[tmp[i + 1]][tmp[i]] = 1;
				ans[tmp.back()][tmp.front()] = ans[tmp.front()][tmp.back()] = 1;
				ans[0][2] = ans[2][0] = 1;

				nodevec.push_back(v);

				for(auto ww : nodevec){
					vector<int> ones;

					for(int u = 0; u < n; u++){
						if(dsu.setfind(ww) != dsu.setfind(u) && dsu.type[dsu.setfind(u)] == 0){
							if(graph[ww][u] == 1){
								int typeu = dsu.type[dsu.setfind(ww)];
			 
								bool flag = true;
			 
								for(auto x : dsu.nodes[dsu.setfind(ww)]){
									for(auto y : dsu.nodes[dsu.setfind(u)]){
										if(x == ww && graph[x][y] != 1) flag = false;
										else if(x != ww && graph[x][y] != typeu) flag = false; 
									}
								}

								// cout << u << " " << flag << "\n";
			 
								if(flag){
									ones.push_back(u);
									ans[ww][u] = ans[u][ww] = 1;
								}
								else return 0;
							}
						}
					}

					for(auto u : ones){
						// cout << ww << " " << u << "*\n";
						dsu.setunion(ww, u);
					}
				}
			}
			// else curnodes.insert(v);
		}
		else curnodes.erase(v);
	}
 
	// finding 2 cycles
 
	curnodes.clear();
 
	for(int i = 0; i < n; i++) curnodes.insert(i);
 
	for(int v = 0; v < n; v++){
		if(curnodes.count(v) && dsu.type[dsu.setfind(v)] == 0){
			curnodes.erase(v);

			vector<int> nodevec;
 
			for(auto u : curnodes){
				if(graph[v][u] == 2){
					bool flag = true;
 
					for(auto w : nodevec){
						if(graph[u][w] != 2) flag = false;
					}
 
					if(flag) nodevec.push_back(u);
				}
			}

			if(nodevec.size() >= 2){
				for(auto u : nodevec) dsu.setunion(v, u);
				dsu.type[dsu.setfind(v)] = 2;
				for(auto u : nodevec) curnodes.erase(u);

				vector<int> tmp;
				for(auto x : dsu.nodes[dsu.setfind(v)]) tmp.push_back(x);
	 
				for(int i = 0; i + 1 < tmp.size(); i++) ans[tmp[i]][tmp[i + 1]] = ans[tmp[i + 1]][tmp[i]] = 1;
				ans[tmp.back()][tmp.front()] = ans[tmp.front()][tmp.back()] = 1;

				// cout << v << ": ";

				// for(auto u : nodevec) cout << u << " ";

				// cout << "\n";

				nodevec.push_back(v);

				for(auto ww : nodevec){
					vector<int> ones;

					for(int u = 0; u < n; u++){
						if(dsu.setfind(ww) != dsu.setfind(u) && dsu.type[dsu.setfind(u)] == 0){
							if(graph[ww][u] == 1){
								int typeu = dsu.type[dsu.setfind(ww)];
			 
								bool flag = true;
			 
								for(auto x : dsu.nodes[dsu.setfind(ww)]){
									for(auto y : dsu.nodes[dsu.setfind(u)]){
										if(x == ww && graph[x][y] != 1) flag = false;
										else if(x != ww && graph[x][y] != typeu) flag = false; 
									}
								}

								// cout << u << " " << flag << "\n";
			 
								if(flag){
									ones.push_back(u);
									ans[ww][u] = ans[u][ww] = 1;
								}
								else return 0;
							}
						}
					}

					for(auto u : ones){
						// cout << ww << " " << u << "*\n";
						dsu.setunion(ww, u);
					}
				}
			}
			// else curnodes.insert(v);
		}
		else curnodes.erase(v);
	}
 
	// finding 1 cycles
 
	curnodes.clear();
 
	for(int i = 0; i < n; i++) curnodes.insert(i);
 
	for(int v = 0; v < n; v++){
		if(curnodes.count(v) && dsu.type[dsu.setfind(v)] == 0){
			curnodes.erase(v);
			dsu.type[dsu.setfind(v)] = 1;

			vector<int> nodevec;
 
			for(auto u : curnodes){
				if(graph[v][u] == 1){
					bool flag = true;
 
					for(auto w : nodevec){
						if(graph[u][w] != 1) flag = false;
					}
 
					if(flag) nodevec.push_back(u);
				}
			}

			if(nodevec.size() >= 1){
				for(auto u : nodevec) dsu.setunion(v, u);
				dsu.type[dsu.setfind(v)] = 1;
				for(auto u : nodevec) curnodes.erase(u);

				vector<int> tmp;
				for(auto x : dsu.nodes[dsu.setfind(v)]) tmp.push_back(x);
	 
				for(int i = 0; i + 1 < tmp.size(); i++) ans[tmp[i]][tmp[i + 1]] = ans[tmp[i + 1]][tmp[i]] = 1;
			}
			// else curnodes.insert(v);
		}
		else curnodes.erase(v);
	}
 
	// for(int i = 0; i < n; i++){
	// 	cout << i << " " << dsu.type[i] << ": ";
	// 	for(auto v : dsu.nodes[i]) cout << v << " ";
	// 	cout << "\n";
	// }
 
	// creating edges
 
	// connecting 1 cycles to other cycles
 
	for(int v = 0; v < n; v++){
		int cnt = 0;
 
		for(int u = 0; u < n; u++){
			if(dsu.type[dsu.setfind(v)] == 1 && dsu.setfind(v) != dsu.setfind(u)){
				if(graph[v][u] == 1){
					int typeu = dsu.type[dsu.setfind(u)];
 
					bool flag = true;
 
					for(auto x : dsu.nodes[dsu.setfind(v)]){
						for(auto y : dsu.nodes[dsu.setfind(u)]){
							if(y == u && graph[x][y] != 1) flag = false;
							else if(y != u && graph[x][y] != typeu) flag = false; 
						}
					}
 
					if(flag){
						dsu.setunion(v, u);
						ans[v][u] = ans[u][v] = 1;
					}
					else return 0;
 
					cnt++;
				}
			}
		}
 
		if(cnt >= 2) return 0;
	}
 
	for(int v = 0; v < n; v++){
		for(int u = 0; u < n; u++){
			if(dsu.setfind(v) != dsu.setfind(u) && graph[v][u] != 0) return 0;
		}
	}
 
	build(ans);
	return 1;
}

// int main(){
// 	construct({{1, 2, 1, 2, 2, 2, 1}, {2, 1, 2, 1, 2, 2, 2}, {1, 2, 1, 2, 2, 2, 1}, {2, 1, 2, 1, 2, 2, 2}, {2, 2, 2, 2, 1, 1, 2}, {2, 2, 2, 2, 1, 1, 2}, {1, 2, 1, 2, 2, 2, 1}});
// }

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:83:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |     for(int i = 0; i + 1 < tmp.size(); i++) ans[tmp[i]][tmp[i + 1]] = ans[tmp[i + 1]][tmp[i]] = 1;
      |                    ~~~~~~^~~~~~~~~~~~
supertrees.cpp:160:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  160 |     for(int i = 0; i + 1 < tmp.size(); i++) ans[tmp[i]][tmp[i + 1]] = ans[tmp[i + 1]][tmp[i]] = 1;
      |                    ~~~~~~^~~~~~~~~~~~
supertrees.cpp:243:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  243 |     for(int i = 0; i + 1 < tmp.size(); i++) ans[tmp[i]][tmp[i + 1]] = ans[tmp[i + 1]][tmp[i]] = 1;
      |                    ~~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 8 ms 1236 KB Output is correct
7 Correct 211 ms 22288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 8 ms 1236 KB Output is correct
7 Correct 211 ms 22288 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 9 ms 1236 KB Output is correct
13 Correct 217 ms 22296 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 4 ms 852 KB Output is correct
17 Correct 95 ms 12544 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 49 ms 5888 KB Output is correct
21 Correct 219 ms 22288 KB Output is correct
22 Correct 227 ms 22272 KB Output is correct
23 Correct 268 ms 22312 KB Output is correct
24 Correct 186 ms 22228 KB Output is correct
25 Correct 112 ms 12492 KB Output is correct
26 Correct 95 ms 12604 KB Output is correct
27 Correct 208 ms 22216 KB Output is correct
28 Correct 209 ms 22204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 352 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 352 KB Output is correct
8 Correct 9 ms 1256 KB Output is correct
9 Correct 224 ms 22188 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 8 ms 1192 KB Output is correct
13 Correct 209 ms 22256 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 5 ms 812 KB Output is correct
17 Correct 125 ms 12428 KB Output is correct
18 Correct 1 ms 352 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 48 ms 5900 KB Output is correct
22 Correct 239 ms 22248 KB Output is correct
23 Correct 217 ms 22252 KB Output is correct
24 Correct 234 ms 22268 KB Output is correct
25 Correct 127 ms 12476 KB Output is correct
26 Correct 109 ms 12544 KB Output is correct
27 Correct 209 ms 22256 KB Output is correct
28 Correct 246 ms 22364 KB Output is correct
29 Correct 92 ms 12560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 61 ms 5908 KB Output is correct
5 Correct 205 ms 22220 KB Output is correct
6 Correct 187 ms 22276 KB Output is correct
7 Correct 214 ms 22300 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 54 ms 5872 KB Output is correct
10 Correct 252 ms 22284 KB Output is correct
11 Correct 190 ms 22260 KB Output is correct
12 Correct 212 ms 22216 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 352 KB Output is correct
16 Correct 57 ms 5840 KB Output is correct
17 Correct 212 ms 22276 KB Output is correct
18 Correct 195 ms 22336 KB Output is correct
19 Correct 231 ms 22408 KB Output is correct
20 Correct 205 ms 22308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 8 ms 1236 KB Output is correct
7 Correct 211 ms 22288 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 9 ms 1236 KB Output is correct
13 Correct 217 ms 22296 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 4 ms 852 KB Output is correct
17 Correct 95 ms 12544 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 49 ms 5888 KB Output is correct
21 Correct 219 ms 22288 KB Output is correct
22 Correct 227 ms 22272 KB Output is correct
23 Correct 268 ms 22312 KB Output is correct
24 Correct 186 ms 22228 KB Output is correct
25 Correct 112 ms 12492 KB Output is correct
26 Correct 95 ms 12604 KB Output is correct
27 Correct 208 ms 22216 KB Output is correct
28 Correct 209 ms 22204 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 352 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 352 KB Output is correct
36 Correct 9 ms 1256 KB Output is correct
37 Correct 224 ms 22188 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 8 ms 1192 KB Output is correct
41 Correct 209 ms 22256 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 5 ms 812 KB Output is correct
45 Correct 125 ms 12428 KB Output is correct
46 Correct 1 ms 352 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 48 ms 5900 KB Output is correct
50 Correct 239 ms 22248 KB Output is correct
51 Correct 217 ms 22252 KB Output is correct
52 Correct 234 ms 22268 KB Output is correct
53 Correct 127 ms 12476 KB Output is correct
54 Correct 109 ms 12544 KB Output is correct
55 Correct 209 ms 22256 KB Output is correct
56 Correct 246 ms 22364 KB Output is correct
57 Correct 92 ms 12560 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 5 ms 852 KB Output is correct
61 Correct 97 ms 12348 KB Output is correct
62 Correct 1 ms 340 KB Output is correct
63 Correct 1 ms 340 KB Output is correct
64 Correct 1 ms 340 KB Output is correct
65 Correct 51 ms 5844 KB Output is correct
66 Correct 92 ms 12396 KB Output is correct
67 Correct 108 ms 12420 KB Output is correct
68 Correct 87 ms 12504 KB Output is correct
69 Correct 99 ms 12568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 8 ms 1236 KB Output is correct
7 Correct 211 ms 22288 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 9 ms 1236 KB Output is correct
13 Correct 217 ms 22296 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 4 ms 852 KB Output is correct
17 Correct 95 ms 12544 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 49 ms 5888 KB Output is correct
21 Correct 219 ms 22288 KB Output is correct
22 Correct 227 ms 22272 KB Output is correct
23 Correct 268 ms 22312 KB Output is correct
24 Correct 186 ms 22228 KB Output is correct
25 Correct 112 ms 12492 KB Output is correct
26 Correct 95 ms 12604 KB Output is correct
27 Correct 208 ms 22216 KB Output is correct
28 Correct 209 ms 22204 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 352 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 352 KB Output is correct
36 Correct 9 ms 1256 KB Output is correct
37 Correct 224 ms 22188 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 8 ms 1192 KB Output is correct
41 Correct 209 ms 22256 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 5 ms 812 KB Output is correct
45 Correct 125 ms 12428 KB Output is correct
46 Correct 1 ms 352 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 48 ms 5900 KB Output is correct
50 Correct 239 ms 22248 KB Output is correct
51 Correct 217 ms 22252 KB Output is correct
52 Correct 234 ms 22268 KB Output is correct
53 Correct 127 ms 12476 KB Output is correct
54 Correct 109 ms 12544 KB Output is correct
55 Correct 209 ms 22256 KB Output is correct
56 Correct 246 ms 22364 KB Output is correct
57 Correct 92 ms 12560 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 61 ms 5908 KB Output is correct
62 Correct 205 ms 22220 KB Output is correct
63 Correct 187 ms 22276 KB Output is correct
64 Correct 214 ms 22300 KB Output is correct
65 Correct 1 ms 340 KB Output is correct
66 Correct 54 ms 5872 KB Output is correct
67 Correct 252 ms 22284 KB Output is correct
68 Correct 190 ms 22260 KB Output is correct
69 Correct 212 ms 22216 KB Output is correct
70 Correct 1 ms 340 KB Output is correct
71 Correct 1 ms 348 KB Output is correct
72 Correct 1 ms 352 KB Output is correct
73 Correct 57 ms 5840 KB Output is correct
74 Correct 212 ms 22276 KB Output is correct
75 Correct 195 ms 22336 KB Output is correct
76 Correct 231 ms 22408 KB Output is correct
77 Correct 205 ms 22308 KB Output is correct
78 Correct 1 ms 340 KB Output is correct
79 Correct 1 ms 340 KB Output is correct
80 Correct 5 ms 852 KB Output is correct
81 Correct 97 ms 12348 KB Output is correct
82 Correct 1 ms 340 KB Output is correct
83 Correct 1 ms 340 KB Output is correct
84 Correct 1 ms 340 KB Output is correct
85 Correct 51 ms 5844 KB Output is correct
86 Correct 92 ms 12396 KB Output is correct
87 Correct 108 ms 12420 KB Output is correct
88 Correct 87 ms 12504 KB Output is correct
89 Correct 99 ms 12568 KB Output is correct
90 Correct 1 ms 340 KB Output is correct
91 Correct 1 ms 340 KB Output is correct
92 Correct 5 ms 596 KB Output is correct
93 Correct 90 ms 8416 KB Output is correct
94 Correct 1 ms 340 KB Output is correct
95 Correct 1 ms 340 KB Output is correct
96 Correct 1 ms 340 KB Output is correct
97 Correct 19 ms 2360 KB Output is correct
98 Correct 89 ms 8396 KB Output is correct
99 Correct 84 ms 8316 KB Output is correct
100 Correct 86 ms 8420 KB Output is correct
101 Correct 75 ms 8440 KB Output is correct
102 Correct 77 ms 8532 KB Output is correct
103 Correct 76 ms 10152 KB Output is correct
104 Correct 77 ms 10160 KB Output is correct
105 Correct 87 ms 10156 KB Output is correct