Submission #783593

#TimeUsernameProblemLanguageResultExecution timeMemory
783593mindiyakConnecting Supertrees (IOI20_supertrees)C++14
21 / 100
158 ms22064 KiB
#include "supertrees.h"
#include <vector>
#include <iostream>

#define pb push_back

using namespace std;
vector<int> head;
vector<int> leaf;

int find_head(int val){
	if(head[val] == val)
	    return val;
	int temp = find_head(head[val]);
	head[val] = temp;
	return temp;
}

int find_leaf(int val){
	if(leaf[val] == val)
	    return val;
	int temp = find_leaf(leaf[val]);
	leaf[val] = temp;
	return temp;
}

int construct(vector<vector<int>> p) {
    // cout << "started" << endl;
	int n = p.size();

	vector<vector<int>> ans;
	vector<int> temp(n,0);
	for (int i = 0; i < n; i++) {
		ans.pb(temp);
		head.pb(i);
		leaf.pb(i);
	}

	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(p[i][j] >= 1){
				int c = min(i,j),d = max(i,j);

				int h_c = find_head(c);
				int h_d = find_head(d);
				int l_c = find_leaf(c);
				int l_d = find_leaf(d);

				if(h_c != h_d){
				    ans[h_c][h_d] = 1;
				    ans[h_d][h_c] = 1;
				}
				head[h_d] = h_c;
				leaf[h_c] = l_d;
			} 
		}
	}

	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			int c = min(i,j),d = max(i,j);
			int h_c = find_head(c);
			int h_d = find_head(d);

			if(p[i][j] == 2){
				int l_c = find_leaf(c);
				int l_d = find_leaf(d);
				ans[h_c][l_d] = 1;
				ans[l_d][h_c] = 1;
				if(h_c != h_d or l_c != l_d){
					return 0;
				}
			}
			
			if(p[i][j] == 0 and h_c == h_d){
				return 0;
			}
			if(p[i][j] == 1 and h_c != h_d){
				return 0;
			}
		}
	}


	build(ans);
// 	cout << "finish" << endl;
	return 1;
}

Compilation message (stderr)

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:46:9: warning: unused variable 'l_c' [-Wunused-variable]
   46 |     int l_c = find_leaf(c);
      |         ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...