답안 #837913

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
837913 2023-08-25T19:57:31 Z mathematik 슈퍼트리 잇기 (IOI20_supertrees) C++17
56 / 100
166 ms 22092 KB
#include "supertrees.h"
#include <bits/stdc++.h>

using namespace std;

struct setunion{
	int N;
	vector<int> p, s;

	setunion(int n) {
		N = n;
		p = vector<int>(N, -1);
		s = vector<int>(N, 1);
	}

	int root(int n) {
		int i = n;
		while (p[i] != -1)
			i = p[i];
		return i;
	}

	void combine(int a, int b) {
		int ra = root(a), rb = root(b);

		if (ra == rb) return;

		if (s[ra] < s[rb]) swap(ra, rb);

		s[ra] += s[rb];
		p[rb] = ra;
	}

	vector<vector<int>> extract() {
		vector<vector<int>> erg(N), rem;
		for (int i = 0; i < N; i++)
		{
			erg[root(i)].push_back(i);
		}
		for (vector<int> e: erg)
			if (e.size())
				rem.push_back(e);
		return rem;
	}
};

int construct(vector<vector<int>> p) {
	int N = p.size();

	int c = 0, o = 0;

	setunion zsm(N);
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
		{
			if (p[i][j] == 3) return 0;
			if (p[i][j]) zsm.combine(i, j);
			if (p[i][j] == 1 || i == j) o++;
			if (p[i][j] || i == j) c++;
		}
	}
	
	vector<vector<int>> graph = vector<vector<int>>(N, vector<int>(N));
	for (vector<int> z: zsm.extract()) {
		c -= z.size() * z.size();

		setunion one(z.size());
		for (int i = 0; i < z.size(); ++i)
		{
			for (int j = 0; j < z.size(); ++j)
			{
				if (p[z[i]][z[j]] == 1)
					one.combine(i, j);
			}
		}
		
		vector<vector<int>> g = one.extract();

		for (int i = 0; i < g.size(); i++)
		{
			graph[z[g[i][0]]][z[g[(i + 1) % g.size()][0]]] = true;
			graph[z[g[(i + 1) % g.size()][0]]][z[g[i][0]]] = true;
		}
		
		for (int i = 0; i < g.size(); i++)
		{
			o -= g[i].size() * g[i].size();
			for (int j = 1; j < g[i].size(); j++)
			{
				graph[z[g[i][0]]][z[g[i][j]]] = true;
				graph[z[g[i][j]]][z[g[i][0]]] = true;
			}

			for (int j: g[i])
				for (int k: g[i])
					if (p[z[j]][z[k]] != 1)
						return 0;	
		}

		for (int i: z)
			for (int j: z)
				if (p[i][j] == 0)
						return 0;	
	}

	for (int i = 0; i < N; i++)
		graph[i][i] = false;

	if (c != 0 || o != 0) return 0;

	build(graph);

	return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:69:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |   for (int i = 0; i < z.size(); ++i)
      |                   ~~^~~~~~~~~~
supertrees.cpp:71:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |    for (int j = 0; j < z.size(); ++j)
      |                    ~~^~~~~~~~~~
supertrees.cpp:80:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |   for (int i = 0; i < g.size(); i++)
      |                   ~~^~~~~~~~~~
supertrees.cpp:86:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |   for (int i = 0; i < g.size(); i++)
      |                   ~~^~~~~~~~~~
supertrees.cpp:89:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |    for (int j = 1; j < g[i].size(); j++)
      |                    ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 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 1 ms 212 KB Output is correct
6 Correct 7 ms 1144 KB Output is correct
7 Correct 156 ms 22016 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 212 KB Output is correct
6 Correct 7 ms 1144 KB Output is correct
7 Correct 156 ms 22016 KB Output is correct
8 Correct 1 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 1 ms 212 KB Output is correct
12 Correct 6 ms 1108 KB Output is correct
13 Correct 160 ms 22028 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 724 KB Output is correct
17 Correct 78 ms 12156 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 37 ms 5768 KB Output is correct
21 Correct 152 ms 21964 KB Output is correct
22 Correct 154 ms 21988 KB Output is correct
23 Correct 159 ms 22012 KB Output is correct
24 Correct 158 ms 22092 KB Output is correct
25 Correct 61 ms 12096 KB Output is correct
26 Correct 66 ms 12160 KB Output is correct
27 Correct 157 ms 22020 KB Output is correct
28 Correct 151 ms 21964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 Incorrect 0 ms 212 KB Answer gives possible 1 while actual possible 0
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 38 ms 5692 KB Output is correct
5 Correct 151 ms 22020 KB Output is correct
6 Correct 148 ms 22072 KB Output is correct
7 Correct 162 ms 21964 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 37 ms 5784 KB Output is correct
10 Correct 159 ms 22040 KB Output is correct
11 Correct 166 ms 22048 KB Output is correct
12 Correct 154 ms 21988 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 244 KB Output is correct
15 Correct 0 ms 244 KB Output is correct
16 Correct 38 ms 5652 KB Output is correct
17 Correct 160 ms 22036 KB Output is correct
18 Correct 150 ms 22020 KB Output is correct
19 Correct 151 ms 22000 KB Output is correct
20 Correct 152 ms 22012 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 212 KB Output is correct
6 Correct 7 ms 1144 KB Output is correct
7 Correct 156 ms 22016 KB Output is correct
8 Correct 1 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 1 ms 212 KB Output is correct
12 Correct 6 ms 1108 KB Output is correct
13 Correct 160 ms 22028 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 724 KB Output is correct
17 Correct 78 ms 12156 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 37 ms 5768 KB Output is correct
21 Correct 152 ms 21964 KB Output is correct
22 Correct 154 ms 21988 KB Output is correct
23 Correct 159 ms 22012 KB Output is correct
24 Correct 158 ms 22092 KB Output is correct
25 Correct 61 ms 12096 KB Output is correct
26 Correct 66 ms 12160 KB Output is correct
27 Correct 157 ms 22020 KB Output is correct
28 Correct 151 ms 21964 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Incorrect 0 ms 212 KB Answer gives possible 1 while actual possible 0
33 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 1 ms 212 KB Output is correct
6 Correct 7 ms 1144 KB Output is correct
7 Correct 156 ms 22016 KB Output is correct
8 Correct 1 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 1 ms 212 KB Output is correct
12 Correct 6 ms 1108 KB Output is correct
13 Correct 160 ms 22028 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 724 KB Output is correct
17 Correct 78 ms 12156 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 37 ms 5768 KB Output is correct
21 Correct 152 ms 21964 KB Output is correct
22 Correct 154 ms 21988 KB Output is correct
23 Correct 159 ms 22012 KB Output is correct
24 Correct 158 ms 22092 KB Output is correct
25 Correct 61 ms 12096 KB Output is correct
26 Correct 66 ms 12160 KB Output is correct
27 Correct 157 ms 22020 KB Output is correct
28 Correct 151 ms 21964 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Incorrect 0 ms 212 KB Answer gives possible 1 while actual possible 0
33 Halted 0 ms 0 KB -