Submission #812773

#TimeUsernameProblemLanguageResultExecution timeMemory
812773KemalKConnecting Supertrees (IOI20_supertrees)C++17
40 / 100
174 ms22076 KiB
#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int pr[N];
int sz[N];
int get(int x){
	if (pr[x] == x){
		return x;
	}
	return (pr[x] = get(pr[x]));
}
void unit(int a, int b){
	a = get(a);
	b = get(b);
	if (a != b){
		if (sz[a] < sz[b]){
			swap(a, b);
		}
		pr[b] = a;
		sz[a] += sz[b];
	}
}
int get2(int x, vector<int> &pr2){
	if (pr2[x] == x){
		return x;
	}
	return (pr2[x] = get(pr2[x]));
}
void unit2(int a, int b, vector <int> &pr2, vector <int> &sz2){
	a = get2(a, pr2);
	b = get2(b, pr2);
	if (a != b){
		if (sz2[a] < sz2[b]){
			swap(a, b);
		}
		pr2[b] = a;
		sz2[a] += sz2[b];
	}
}
int construct(std::vector<std::vector<int>> p) {
	int n = p.size();
	for (int i = 0; i < n; i++){
		pr[i] = i; sz[i] = 1;
	}
	int check = 0;
	for (int i = 0; i < n; i++){
		for (int j = 0; j < n; j++){
			if (p[i][j]){
				unit(i, j);
				// check = max(check, p[i][j]);
			}
		}
	}
	for (int i = 0; i < n; i++){
		for (int j = 0; j < n; j++){
			if (p[i][j] == 0 and get(i) == get(j)){ //impossible
				return 0;
			}
		}
	}
	vector <vector<int>> answer(n, vector<int>(n));
	// if(!check){
	// 	build(answer);
	// 	return 1;	
	// }
	for (int i = 0; i < n; i++){
		vector <int> branch;
		for (int j = 0; j < n; j++){
			if (get(j) == i){
				branch.push_back(j);
			}
		}
		vector <int> pr2(branch.size()), sz2(branch.size(), 1);
		for (int j = 0; j < branch.size(); j++){
			pr2[j] = j;
		}
		for (int j = 0; j < branch.size(); j++){
			for (int k = 0; k < branch.size(); k++){
				if (p[branch[j]][branch[k]] == 1){
					unit2(j, k, pr2, sz2);
				}
			}
		}
		for (int j = 0; j < branch.size(); j++){
			for (int k = 0; k < branch.size(); k++){
				if (p[branch[j]][branch[k]] == 2 and get2(j, pr2) == get2(k, pr2)){
					return 0;
				}
			}
		}
		vector <vector <int>> forest(branch.size());
		for (int i = 0; i < branch.size(); i++){
			forest[get2(i, pr2)].push_back(branch[i]);
		}
		vector <int> q;
		for (int j = 0; j < forest.size(); j++){
			if (forest[j].size()){
				q.push_back(forest[j][0]);
			}
			for (int k = 0; k + 1 < forest[j].size(); k++){
				answer[forest[j][k]][forest[j][k + 1]] = 1;
				answer[forest[j][k + 1]][forest[j][k]] = 1;
			}
		}
		if (q.size() == 2){
			return 0;
		}
		if (q.size() > 2){
			for (int j = 0; j < q.size(); j++){
				answer[q[j]][q[(j + 1)%q.size()]] = 1;
				answer[q[(j + 1)%q.size()]][q[j]] = 1;
			}
		}
		// for (int j = 0; j + 1 < branch.size(); j++){
		// 	answer[branch[j]][branch[j + 1]] = 1;
		// 	answer[branch[j + 1]][branch[j]] = 1;
		// }
		// if (check == 2){
		// 	if(branch.size() == 2){
		// 		return 0;
		// 	}
		// 	if (branch.size() > 2){
		// 		answer[branch[0]][branch.back()] = 1;
		// 		answer[branch.back()][branch[0]] = 1;
		// 	}
		// }
	}
	build(answer);
	return 1;
}

Compilation message (stderr)

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:76:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |   for (int j = 0; j < branch.size(); j++){
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:79:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |   for (int j = 0; j < branch.size(); j++){
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:80:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |    for (int k = 0; k < branch.size(); k++){
      |                    ~~^~~~~~~~~~~~~~~
supertrees.cpp:86:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |   for (int j = 0; j < branch.size(); j++){
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:87:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |    for (int k = 0; k < branch.size(); k++){
      |                    ~~^~~~~~~~~~~~~~~
supertrees.cpp:94:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |   for (int i = 0; i < branch.size(); i++){
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:98:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |   for (int j = 0; j < forest.size(); j++){
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:102:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |    for (int k = 0; k + 1 < forest[j].size(); k++){
      |                    ~~~~~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:111:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |    for (int j = 0; j < q.size(); j++){
      |                    ~~^~~~~~~~~~
supertrees.cpp:47:6: warning: unused variable 'check' [-Wunused-variable]
   47 |  int check = 0;
      |      ^~~~~
#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...