Submission #431292

# Submission time Handle Problem Language Result Execution time Memory
431292 2021-06-17T10:45:41 Z Hazem Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
873 ms 34128 KB
#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>

using namespace std;

const int N = 2e3+10;

vector<int>adj[N],vec,vec1;
vector<vector<int>>adjans,p,vecs;
int n,vis[N],vis1[N];

void dfs(int i){

	if(vis[i])
		return ;
	vis[i] = 1;
	vec.push_back(i);

	for(auto x:adj[i])
		dfs(x);
}

void dfs(int i,int t){

	if(vis1[i])
		return ;

	vis1[i] = 1;
	vec1.push_back(i);
	for(auto x:adj[i])
		if(p[i][x]==1)
			dfs(x,t);
}

bool checkallequal(vector<int>vec1){

	bool ret = 1;
	for(int i=0;i<vec.size();i++)
		for(int j=i+1;j<vec.size();j++){
			bool q = 0;
			for(auto x:vec1)
				q |= p[vec[i]][vec[j]]==x;
			ret &= q;
		}
	return ret;
}

int construct(std::vector<std::vector<int>> P) {

	p = P;
	n = p.size();
	adjans = vector<vector<int>>(n,vector<int>(n,0));

	int mx = 0,mn = 10;set<int>st;
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++){
			if(p[i][j]){
				adj[i].push_back(j);
				adj[j].push_back(i);
			}
			if(i!=j){
				mn = min(mn,p[i][j]);
				mx = max(mx,p[i][j]);
				st.insert(p[i][j]);
			}
		}

	if(mx<=1){
		bool ans = 1;
	for(int i=0;i<n;i++){
		
		dfs(i);bool q = 1;
		for(int j=0;j<vec.size();j++)
			for(int k=0;k<vec.size();k++)
				q &= p[vec[j]][vec[k]]==1;
 
		//q &= !vec.size()||vec.size()>2;
		for(int j=0;j<(int)vec.size()-1;j++)
			adjans[vec[j]][vec[j+1]] = adjans[vec[j+1]][vec[j]] = 1;
 
		//adjans[vec[0]][vec.back()] = adjans[vec.back()][vec[0]] = 1;
		vec.clear();
		ans &= q;
	}

	if(!ans)
		return 0;
 
	build(adjans);
	return 1;

	}

	if((mx==mn)||(!mn&&st.size()==2)){
		bool ans = 1;
	for(int i=0;i<n;i++){
		
		dfs(i);bool q = 1;
		if(vec.size()<=1){
			vec.clear();
			continue;
		}
 
		for(int j=0;j<vec.size();j++)
			for(int k=j+1;k<vec.size();k++){
				q &= p[vec[j]][vec[k]]==2;
				assert(vec[j]!=vec[k]);
			}
			
 
		q &= vec.size()>2;
		for(int j=0;j<(int)vec.size()-1;j++)
			adjans[vec[j]][vec[j+1]] = adjans[vec[j+1]][vec[j]] = 1;
		
		adjans[vec[0]][vec.back()] = adjans[vec.back()][vec[0]] = 1;
		vec.clear();
		ans &= q;
	}
 
	if(!ans)
		return 0;
 
	build(adjans);
	return 1;

	}

	bool ans = 1;
	for(int i=0;i<n;i++){
		
		dfs(i);bool q = 1;
		if(vec.size()<=1){
			vec.clear();
			continue;
		}

		vector<int>vec3;
		for(auto x:vec){
			
			dfs(x,1);
			for(int i=0;i<vec1.size();i++)
				for(int j=i+1;j<vec1.size();j++)
					q &= p[vec1[i]][vec1[j]]==1;
			
			vecs.push_back(vec1);
			for(int j=0;j<(int)vec1.size()-1;j++){
				adjans[vec1[j]][vec1[j+1]] = adjans[vec1[j+1]][vec1[j]] = 1;
				assert(vec1[j]!=vec1[j+1]);
			}
			
			if(vec1.size())
				vec3.push_back(x);
			vec1.clear();
		}
		
		for(int j=0;j<(int)vec3.size()-1;j++){
			adjans[vec3[j]][vec3[j+1]] = adjans[vec3[j+1]][vec3[j]] = 1;
			assert(vec3[j]!=vec3[j+1]);
		}

		q &= vec3.size()!=2;
		if(vec3.size()>1){
			adjans[vec3[0]][vec3.back()] = adjans[vec3.back()][vec3[0]] = 1;
			assert(vec[0]!=vec3.back());
		}

		for(int j=0;j<(int)vecs.size()-1;j++)
			for(int k=j+1;k<vec.size();k++)
			for(auto x:vecs[j])
				for(auto y:vecs[k])
					q &= p[x][y]==2;
		
		ans &= q;
		vecs.clear();
	}

	if(!ans)
		return 0;

	build(adjans);
	return 1;
}

Compilation message

supertrees.cpp: In function 'bool checkallequal(std::vector<int>)':
supertrees.cpp:39:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |  for(int i=0;i<vec.size();i++)
      |              ~^~~~~~~~~~~
supertrees.cpp:40:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |   for(int j=i+1;j<vec.size();j++){
      |                 ~^~~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:74:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |   for(int j=0;j<vec.size();j++)
      |               ~^~~~~~~~~~~
supertrees.cpp:75:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |    for(int k=0;k<vec.size();k++)
      |                ~^~~~~~~~~~~
supertrees.cpp:105:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |   for(int j=0;j<vec.size();j++)
      |               ~^~~~~~~~~~~
supertrees.cpp:106:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |    for(int k=j+1;k<vec.size();k++){
      |                  ~^~~~~~~~~~~
supertrees.cpp:142:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  142 |    for(int i=0;i<vec1.size();i++)
      |                ~^~~~~~~~~~~~
supertrees.cpp:143:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  143 |     for(int j=i+1;j<vec1.size();j++)
      |                   ~^~~~~~~~~~~~
supertrees.cpp:169:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  169 |    for(int k=j+1;k<vec.size();k++)
      |                  ~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 14 ms 1828 KB Output is correct
7 Correct 297 ms 34088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 14 ms 1828 KB Output is correct
7 Correct 297 ms 34088 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 14 ms 1340 KB Output is correct
13 Correct 252 ms 26012 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 8 ms 1228 KB Output is correct
17 Correct 161 ms 21944 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 376 KB Output is correct
20 Correct 79 ms 6836 KB Output is correct
21 Correct 307 ms 27052 KB Output is correct
22 Correct 289 ms 26024 KB Output is correct
23 Correct 317 ms 30148 KB Output is correct
24 Correct 302 ms 26028 KB Output is correct
25 Correct 108 ms 17192 KB Output is correct
26 Correct 130 ms 16180 KB Output is correct
27 Correct 271 ms 32168 KB Output is correct
28 Correct 273 ms 26056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 10 ms 1400 KB Output is correct
9 Correct 267 ms 26012 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 11 ms 1784 KB Output is correct
13 Correct 284 ms 34128 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 6 ms 1228 KB Output is correct
17 Correct 136 ms 21936 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 77 ms 6828 KB Output is correct
22 Correct 243 ms 27072 KB Output is correct
23 Correct 296 ms 26084 KB Output is correct
24 Correct 310 ms 32184 KB Output is correct
25 Correct 105 ms 16188 KB Output is correct
26 Correct 114 ms 17200 KB Output is correct
27 Correct 256 ms 26020 KB Output is correct
28 Correct 289 ms 30144 KB Output is correct
29 Correct 134 ms 16176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 61 ms 6832 KB Output is correct
5 Correct 273 ms 27112 KB Output is correct
6 Correct 296 ms 25988 KB Output is correct
7 Correct 320 ms 30148 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 77 ms 6956 KB Output is correct
10 Correct 244 ms 27224 KB Output is correct
11 Correct 243 ms 26144 KB Output is correct
12 Correct 310 ms 32228 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 129 ms 6868 KB Output is correct
17 Correct 257 ms 27088 KB Output is correct
18 Correct 873 ms 27136 KB Output is correct
19 Correct 786 ms 26412 KB Output is correct
20 Correct 427 ms 26052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 14 ms 1828 KB Output is correct
7 Correct 297 ms 34088 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 14 ms 1340 KB Output is correct
13 Correct 252 ms 26012 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 8 ms 1228 KB Output is correct
17 Correct 161 ms 21944 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 376 KB Output is correct
20 Correct 79 ms 6836 KB Output is correct
21 Correct 307 ms 27052 KB Output is correct
22 Correct 289 ms 26024 KB Output is correct
23 Correct 317 ms 30148 KB Output is correct
24 Correct 302 ms 26028 KB Output is correct
25 Correct 108 ms 17192 KB Output is correct
26 Correct 130 ms 16180 KB Output is correct
27 Correct 271 ms 32168 KB Output is correct
28 Correct 273 ms 26056 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 10 ms 1400 KB Output is correct
37 Correct 267 ms 26012 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 11 ms 1784 KB Output is correct
41 Correct 284 ms 34128 KB Output is correct
42 Correct 0 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 6 ms 1228 KB Output is correct
45 Correct 136 ms 21936 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 77 ms 6828 KB Output is correct
50 Correct 243 ms 27072 KB Output is correct
51 Correct 296 ms 26084 KB Output is correct
52 Correct 310 ms 32184 KB Output is correct
53 Correct 105 ms 16188 KB Output is correct
54 Correct 114 ms 17200 KB Output is correct
55 Correct 256 ms 26020 KB Output is correct
56 Correct 289 ms 30144 KB Output is correct
57 Correct 134 ms 16176 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 12 ms 1400 KB Output is correct
61 Correct 695 ms 25008 KB Output is correct
62 Correct 1 ms 332 KB Output is correct
63 Correct 1 ms 332 KB Output is correct
64 Correct 1 ms 332 KB Output is correct
65 Correct 119 ms 6892 KB Output is correct
66 Correct 606 ms 17480 KB Output is correct
67 Correct 617 ms 17204 KB Output is correct
68 Correct 629 ms 16684 KB Output is correct
69 Correct 316 ms 16184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 14 ms 1828 KB Output is correct
7 Correct 297 ms 34088 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 14 ms 1340 KB Output is correct
13 Correct 252 ms 26012 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 8 ms 1228 KB Output is correct
17 Correct 161 ms 21944 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 376 KB Output is correct
20 Correct 79 ms 6836 KB Output is correct
21 Correct 307 ms 27052 KB Output is correct
22 Correct 289 ms 26024 KB Output is correct
23 Correct 317 ms 30148 KB Output is correct
24 Correct 302 ms 26028 KB Output is correct
25 Correct 108 ms 17192 KB Output is correct
26 Correct 130 ms 16180 KB Output is correct
27 Correct 271 ms 32168 KB Output is correct
28 Correct 273 ms 26056 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 10 ms 1400 KB Output is correct
37 Correct 267 ms 26012 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 11 ms 1784 KB Output is correct
41 Correct 284 ms 34128 KB Output is correct
42 Correct 0 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 6 ms 1228 KB Output is correct
45 Correct 136 ms 21936 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 77 ms 6828 KB Output is correct
50 Correct 243 ms 27072 KB Output is correct
51 Correct 296 ms 26084 KB Output is correct
52 Correct 310 ms 32184 KB Output is correct
53 Correct 105 ms 16188 KB Output is correct
54 Correct 114 ms 17200 KB Output is correct
55 Correct 256 ms 26020 KB Output is correct
56 Correct 289 ms 30144 KB Output is correct
57 Correct 134 ms 16176 KB Output is correct
58 Correct 0 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 1 ms 332 KB Output is correct
61 Correct 61 ms 6832 KB Output is correct
62 Correct 273 ms 27112 KB Output is correct
63 Correct 296 ms 25988 KB Output is correct
64 Correct 320 ms 30148 KB Output is correct
65 Correct 1 ms 332 KB Output is correct
66 Correct 77 ms 6956 KB Output is correct
67 Correct 244 ms 27224 KB Output is correct
68 Correct 243 ms 26144 KB Output is correct
69 Correct 310 ms 32228 KB Output is correct
70 Correct 1 ms 332 KB Output is correct
71 Correct 1 ms 332 KB Output is correct
72 Correct 1 ms 332 KB Output is correct
73 Correct 129 ms 6868 KB Output is correct
74 Correct 257 ms 27088 KB Output is correct
75 Correct 873 ms 27136 KB Output is correct
76 Correct 786 ms 26412 KB Output is correct
77 Correct 427 ms 26052 KB Output is correct
78 Correct 1 ms 332 KB Output is correct
79 Correct 1 ms 332 KB Output is correct
80 Correct 12 ms 1400 KB Output is correct
81 Correct 695 ms 25008 KB Output is correct
82 Correct 1 ms 332 KB Output is correct
83 Correct 1 ms 332 KB Output is correct
84 Correct 1 ms 332 KB Output is correct
85 Correct 119 ms 6892 KB Output is correct
86 Correct 606 ms 17480 KB Output is correct
87 Correct 617 ms 17204 KB Output is correct
88 Correct 629 ms 16684 KB Output is correct
89 Correct 316 ms 16184 KB Output is correct
90 Correct 1 ms 332 KB Output is correct
91 Correct 1 ms 332 KB Output is correct
92 Correct 12 ms 1484 KB Output is correct
93 Correct 665 ms 24920 KB Output is correct
94 Correct 1 ms 332 KB Output is correct
95 Correct 1 ms 332 KB Output is correct
96 Correct 1 ms 332 KB Output is correct
97 Correct 89 ms 4400 KB Output is correct
98 Correct 740 ms 17280 KB Output is correct
99 Correct 703 ms 17280 KB Output is correct
100 Correct 670 ms 16652 KB Output is correct
101 Correct 284 ms 16232 KB Output is correct
102 Correct 125 ms 24256 KB Output is correct
103 Correct 672 ms 24372 KB Output is correct
104 Correct 775 ms 24392 KB Output is correct
105 Correct 127 ms 21948 KB Output is correct