Submission #829694

# Submission time Handle Problem Language Result Execution time Memory
829694 2023-08-18T14:10:35 Z faruk Connecting Supertrees (IOI20_supertrees) C++17
65 / 100
160 ms 27952 KB
#include "supertrees.h"
#include <iostream>
#include <vector>
#include <algorithm>
#define mp make_pair
#define all(a) a.begin(), a.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

vector<vector<int> > out;

int n;
bool check_components(vector<vector<int> > p) {
	vector<int> comp(n, -1);
	for (int i = 0; i < n; i++) {
		if (p[i][i] != 1)
			return false;
		if (comp[i] == -1) {
			for (int j = 0; j < n; j++) {
				if (p[i][j] != 0 and comp[j] != -1)
					return false;
				if (p[i][j] != 0)
					comp[j] = i;
			}
		}
		else {
			for (int j = 0; j < n; j++)
				if (p[i][j] != 0 and comp[j] != comp[i])
					return false;
		}
	}
	return true;
}

void make_line(vector<int> a) {
	if (a.size() == 1)
		return;
	for (int i = 0; i < a.size() - 1; i++)
	{
		int f = a[i], s = a[i + 1];
		out[f][s] = 1, out[s][f] = 1;
	}
}
vector<vector<int> > p;
vector<bool> vis;
vector<int> ch;
void dfs(int c) {
	ch.push_back(c);
	vis[c] = true;
	for (int i = 0; i < n; i++) {
		if (p[i][c] == 1 and !vis[i])
			dfs(i);
	} 
}

int construct(std::vector<std::vector<int>> p) {
	n = p.size();
	::p = p;
	std::vector<std::vector<int>> answer;
	if (check_components(p) == false)
		return 0;
	vector<bool> visited(n);
	out = vector<vector<int> >(n, vector<int>(n, 0));
	for (int v = 0; v < n; v++) {
		if (visited[v])
			continue;
		vector<int> mine;
		for (int j = 0; j < n; j++)
			if (p[v][j] != 0)
				mine.push_back(j), visited[j] = true;
		if (mine.size() == 1)
			continue;

		vector<int> everyone_else = mine;
		vector<vector<int> > chains;
		vis = vector<bool>(n);
		for (int a : everyone_else) {
			// now we decompose into sub-chains
			ch.clear();
			if (!vis[a]) {
				dfs(a);
				chains.push_back(ch);
			}
		}

		// now check if chains are good
		for (int i = 0; i < chains.size(); i++) {
			for (int j = i + 1; j < chains.size(); j++) {
				for (int a : chains[i])
					for (int b : chains[j])
						if (p[a][b] != 2)
							return 0;
			}
		}

		for (vector<int> chai : chains)
			make_line(chai);
		// connecting chains and cyc
		if (chains.size() > 1) {
			vector<int> fin;
			for (int i = 0; i < chains.size(); i++)
				fin.push_back(chains[i][0]); 
			fin.push_back(chains[0][0]);
			if (chains.size() == 2)
				return 0;
			make_line(fin);
		}
	}
	build(out);
	return 1;
}

Compilation message

supertrees.cpp: In function 'void make_line(std::vector<int>)':
supertrees.cpp:41:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |  for (int i = 0; i < a.size() - 1; i++)
      |                  ~~^~~~~~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:90:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |   for (int i = 0; i < chains.size(); i++) {
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:91:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |    for (int j = i + 1; j < chains.size(); j++) {
      |                        ~~^~~~~~~~~~~~~~~
supertrees.cpp:104:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  104 |    for (int i = 0; i < chains.size(); i++)
      |                    ~~^~~~~~~~~~~~~~~
# 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 6 ms 1364 KB Output is correct
7 Correct 158 ms 25964 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 6 ms 1364 KB Output is correct
7 Correct 158 ms 25964 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 1340 KB Output is correct
13 Correct 145 ms 25940 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 852 KB Output is correct
17 Correct 65 ms 16004 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Incorrect 0 ms 212 KB Answer gives possible 1 while actual possible 0
20 Halted 0 ms 0 KB -
# 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 0 ms 212 KB Output is correct
8 Correct 6 ms 1284 KB Output is correct
9 Correct 147 ms 25916 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 1364 KB Output is correct
13 Correct 145 ms 25996 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 852 KB Output is correct
17 Correct 63 ms 16020 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 300 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 37 ms 7140 KB Output is correct
22 Correct 152 ms 27880 KB Output is correct
23 Correct 148 ms 27916 KB Output is correct
24 Correct 152 ms 27952 KB Output is correct
25 Correct 64 ms 18048 KB Output is correct
26 Correct 61 ms 17996 KB Output is correct
27 Correct 145 ms 27872 KB Output is correct
28 Correct 155 ms 27908 KB Output is correct
29 Correct 65 ms 18056 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 38 ms 6944 KB Output is correct
5 Correct 149 ms 25936 KB Output is correct
6 Correct 148 ms 25948 KB Output is correct
7 Correct 160 ms 26004 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 38 ms 6760 KB Output is correct
10 Correct 151 ms 25948 KB Output is correct
11 Correct 156 ms 25916 KB Output is correct
12 Correct 151 ms 26008 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 37 ms 6728 KB Output is correct
17 Correct 151 ms 27864 KB Output is correct
18 Correct 150 ms 27832 KB Output is correct
19 Correct 154 ms 27876 KB Output is correct
20 Correct 147 ms 27888 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 6 ms 1364 KB Output is correct
7 Correct 158 ms 25964 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 1340 KB Output is correct
13 Correct 145 ms 25940 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 852 KB Output is correct
17 Correct 65 ms 16004 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Incorrect 0 ms 212 KB Answer gives possible 1 while actual possible 0
20 Halted 0 ms 0 KB -
# 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 6 ms 1364 KB Output is correct
7 Correct 158 ms 25964 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 1340 KB Output is correct
13 Correct 145 ms 25940 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 852 KB Output is correct
17 Correct 65 ms 16004 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Incorrect 0 ms 212 KB Answer gives possible 1 while actual possible 0
20 Halted 0 ms 0 KB -