답안 #1079265

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1079265 2024-08-28T12:28:21 Z Jawad_Akbar_JJ 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
180 ms 28004 KB
#include <iostream>
#include <vector>
#include "supertrees.h"

using namespace std;
const int NN = 1005;
vector<int> nei[NN];
int seen[NN], dfsd[NN];
int d[NN][NN], lab[NN];

void dfs(int u, int r){
	dfsd[u] = 1;
	d[r][u]++;

	for (int i : nei[u])
		if (!dfsd[i])
			dfs(i, r);
	dfsd[u] = 0;
}

int construct(vector<vector<int>> p){
	int n = p.size();
	vector<vector<int>> ans(n, vector<int> (n, 0)), comp, comp2;
	vector<int> out;

	int z = 0, o = 0, t = 0, th = 0; 
	for (int i=0;i<n;i++)
		for (int j=0;j<n;j++){
			z += p[i][j] == 0;
			o += p[i][j] == 1;
			t += p[i][j] == 2;
			th += p[i][j] == 3;
		}

	if (o and z + t + th == 0){
		for (int i=1;i<n;i++)
			ans[i-1][i] = ans[i][i-1] = 1;
		build(ans);
		return 1;
	}

	if (t + th == 0){

		for (int i=0;i<n;i++){
			int mx = 0, ind = -1;
			for (int k=0;k<comp.size();k++){
				int cnt = 0;
				for (int j : comp[k])
					cnt += p[i][j];
				if (cnt == 0)
					continue;
				if (mx > 0 or cnt != comp[k].size())
					return 0;
				ind = k;
				mx = cnt;
			}
			if (ind == -1)
				comp.push_back({i});
			else
				comp[ind].push_back(i);
		}

		for (auto v : comp)
			for (int j=1;j<v.size();j++)
				ans[v[j-1]][v[j]] = ans[v[j]][v[j-1]] = 1;
		build(ans);
		return 1;
	}

	if (o == n and th == 0){
		for (int i=0;i<n;i++){
			int mx = 0, ind = -1;
			for (int k=0;k<comp.size();k++){
				int cnt = 0;
				for (int j : comp[k])
					cnt += p[i][j] == 2;
				if (cnt == 0)
					continue;
				if (mx > 0 or cnt != comp[k].size())
					return 0;
				ind = k;
				mx = cnt;
			}
			if (ind == -1)
				comp.push_back({i});
			else
				comp[ind].push_back(i);
		}

		for (auto v : comp){
			for (int j=1;j<v.size();j++)
				ans[v[j-1]][v[j]] = ans[v[j]][v[j-1]] = 1;
			if (v.size() > 1)
				ans[v.back()][v[0]] = ans[v[0]][v.back()] = 1;
			if (v.size() == 2)
				return 0;
		}
		build(ans);
		return 1;
	}
	if (th)
		return 0;

	for (int i=0;i<n;i++){
		lab[i] = i;
		int mx = 0, ind = -1;
		for (int k=0;k<comp.size();k++){
			int cnt = 0;
			for (int j : comp[k])
				cnt += p[i][j] == 1;
			if (cnt == 0)
				continue;
			if (cnt == comp[k].size())
				ind = k;
		}
		if (ind == -1)
			comp.push_back({i});
		else
			comp[ind].push_back(i);
	}
	for (auto v : comp)
		for (int j=1;j<v.size();j++)
			ans[v[j]][v[j-1]] = ans[v[j-1]][v[j]] = 1, lab[v[j]] = v[0];

	comp.clear();

	for (int i=0;i<n;i++){
		if (seen[lab[i]])
			continue;
		int ind = -1;
		for (int k=0;k<comp.size();k++){
			int cnt = 0;
			for (int j : comp[k])
				cnt += p[i][j] == 2;
			if (cnt == 0)
				continue;
			if (cnt == comp[k].size())
				ind = k;
		}
		seen[lab[i]] = 1;
		if (ind == -1)
			comp.push_back({i});
		else
			comp[ind].push_back(i);
	}

	for (auto v : comp){
		if (v.size() == 2)
			return 0;
		for (int i=1;i<v.size();i++)
			ans[v[i]][v[i-1]] = ans[v[i-1]][v[i]] = 1;
		ans[v[0]][v.back()] = ans[v.back()][v[0]] = 1;
	}

	for (int i=0;i<n;i++)
		for (int j=0;j<n;j++){
			if (i == j)
				ans[i][j] = 0;
			if (ans[i][j])
				nei[i].push_back(j);
		}

	for (int i=0;i<n;i++)
		dfs(i, i);
	for (int i=0;i<n;i++)
		for (int j=0;j<n;j++)
			if (d[i][j] != p[i][j])
				return 0;
	build(ans);
	return 1;

}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:46:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |    for (int k=0;k<comp.size();k++){
      |                 ~^~~~~~~~~~~~
supertrees.cpp:52:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |     if (mx > 0 or cnt != comp[k].size())
      |                   ~~~~^~~~~~~~~~~~~~~~~
supertrees.cpp:64:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |    for (int j=1;j<v.size();j++)
      |                 ~^~~~~~~~~
supertrees.cpp:73:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |    for (int k=0;k<comp.size();k++){
      |                 ~^~~~~~~~~~~~
supertrees.cpp:79:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |     if (mx > 0 or cnt != comp[k].size())
      |                   ~~~~^~~~~~~~~~~~~~~~~
supertrees.cpp:91:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |    for (int j=1;j<v.size();j++)
      |                 ~^~~~~~~~~
supertrees.cpp:107:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |   for (int k=0;k<comp.size();k++){
      |                ~^~~~~~~~~~~~
supertrees.cpp:113:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |    if (cnt == comp[k].size())
      |        ~~~~^~~~~~~~~~~~~~~~~
supertrees.cpp:106:7: warning: unused variable 'mx' [-Wunused-variable]
  106 |   int mx = 0, ind = -1;
      |       ^~
supertrees.cpp:122:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |   for (int j=1;j<v.size();j++)
      |                ~^~~~~~~~~
supertrees.cpp:131:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |   for (int k=0;k<comp.size();k++){
      |                ~^~~~~~~~~~~~
supertrees.cpp:137:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  137 |    if (cnt == comp[k].size())
      |        ~~~~^~~~~~~~~~~~~~~~~
supertrees.cpp:150:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  150 |   for (int i=1;i<v.size();i++)
      |                ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 1372 KB Output is correct
7 Correct 131 ms 23772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 1372 KB Output is correct
7 Correct 131 ms 23772 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 9 ms 1372 KB Output is correct
13 Correct 134 ms 24028 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 860 KB Output is correct
17 Correct 63 ms 13884 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 37 ms 6224 KB Output is correct
21 Correct 135 ms 23964 KB Output is correct
22 Correct 134 ms 23888 KB Output is correct
23 Correct 180 ms 23948 KB Output is correct
24 Correct 140 ms 23812 KB Output is correct
25 Correct 69 ms 13968 KB Output is correct
26 Correct 60 ms 14160 KB Output is correct
27 Correct 133 ms 23948 KB Output is correct
28 Correct 130 ms 23908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 5 ms 1372 KB Output is correct
9 Correct 121 ms 23892 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 5 ms 1372 KB Output is correct
13 Correct 123 ms 23912 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 6 ms 856 KB Output is correct
17 Correct 58 ms 14084 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 30 ms 6424 KB Output is correct
22 Correct 130 ms 23828 KB Output is correct
23 Correct 126 ms 23820 KB Output is correct
24 Correct 127 ms 23964 KB Output is correct
25 Correct 59 ms 13968 KB Output is correct
26 Correct 60 ms 13860 KB Output is correct
27 Correct 124 ms 23832 KB Output is correct
28 Correct 132 ms 23852 KB Output is correct
29 Correct 56 ms 14104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 31 ms 6224 KB Output is correct
5 Correct 126 ms 23892 KB Output is correct
6 Correct 124 ms 23516 KB Output is correct
7 Correct 126 ms 23380 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 32 ms 6600 KB Output is correct
10 Correct 121 ms 23380 KB Output is correct
11 Correct 128 ms 23388 KB Output is correct
12 Correct 134 ms 23376 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 33 ms 8240 KB Output is correct
17 Correct 116 ms 24088 KB Output is correct
18 Correct 121 ms 28004 KB Output is correct
19 Correct 126 ms 27984 KB Output is correct
20 Correct 127 ms 27984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 1372 KB Output is correct
7 Correct 131 ms 23772 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 9 ms 1372 KB Output is correct
13 Correct 134 ms 24028 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 860 KB Output is correct
17 Correct 63 ms 13884 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 37 ms 6224 KB Output is correct
21 Correct 135 ms 23964 KB Output is correct
22 Correct 134 ms 23888 KB Output is correct
23 Correct 180 ms 23948 KB Output is correct
24 Correct 140 ms 23812 KB Output is correct
25 Correct 69 ms 13968 KB Output is correct
26 Correct 60 ms 14160 KB Output is correct
27 Correct 133 ms 23948 KB Output is correct
28 Correct 130 ms 23908 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 604 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 5 ms 1372 KB Output is correct
37 Correct 121 ms 23892 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 5 ms 1372 KB Output is correct
41 Correct 123 ms 23912 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 6 ms 856 KB Output is correct
45 Correct 58 ms 14084 KB Output is correct
46 Correct 1 ms 344 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 30 ms 6424 KB Output is correct
50 Correct 130 ms 23828 KB Output is correct
51 Correct 126 ms 23820 KB Output is correct
52 Correct 127 ms 23964 KB Output is correct
53 Correct 59 ms 13968 KB Output is correct
54 Correct 60 ms 13860 KB Output is correct
55 Correct 124 ms 23832 KB Output is correct
56 Correct 132 ms 23852 KB Output is correct
57 Correct 56 ms 14104 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 3 ms 860 KB Output is correct
61 Correct 60 ms 13508 KB Output is correct
62 Correct 1 ms 348 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Correct 31 ms 8276 KB Output is correct
66 Correct 81 ms 18068 KB Output is correct
67 Correct 77 ms 18216 KB Output is correct
68 Correct 62 ms 18092 KB Output is correct
69 Correct 57 ms 14244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 1372 KB Output is correct
7 Correct 131 ms 23772 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 9 ms 1372 KB Output is correct
13 Correct 134 ms 24028 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 860 KB Output is correct
17 Correct 63 ms 13884 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 37 ms 6224 KB Output is correct
21 Correct 135 ms 23964 KB Output is correct
22 Correct 134 ms 23888 KB Output is correct
23 Correct 180 ms 23948 KB Output is correct
24 Correct 140 ms 23812 KB Output is correct
25 Correct 69 ms 13968 KB Output is correct
26 Correct 60 ms 14160 KB Output is correct
27 Correct 133 ms 23948 KB Output is correct
28 Correct 130 ms 23908 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 604 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 5 ms 1372 KB Output is correct
37 Correct 121 ms 23892 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 5 ms 1372 KB Output is correct
41 Correct 123 ms 23912 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 6 ms 856 KB Output is correct
45 Correct 58 ms 14084 KB Output is correct
46 Correct 1 ms 344 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 30 ms 6424 KB Output is correct
50 Correct 130 ms 23828 KB Output is correct
51 Correct 126 ms 23820 KB Output is correct
52 Correct 127 ms 23964 KB Output is correct
53 Correct 59 ms 13968 KB Output is correct
54 Correct 60 ms 13860 KB Output is correct
55 Correct 124 ms 23832 KB Output is correct
56 Correct 132 ms 23852 KB Output is correct
57 Correct 56 ms 14104 KB Output is correct
58 Correct 1 ms 344 KB Output is correct
59 Correct 0 ms 604 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 31 ms 6224 KB Output is correct
62 Correct 126 ms 23892 KB Output is correct
63 Correct 124 ms 23516 KB Output is correct
64 Correct 126 ms 23380 KB Output is correct
65 Correct 0 ms 344 KB Output is correct
66 Correct 32 ms 6600 KB Output is correct
67 Correct 121 ms 23380 KB Output is correct
68 Correct 128 ms 23388 KB Output is correct
69 Correct 134 ms 23376 KB Output is correct
70 Correct 0 ms 348 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 33 ms 8240 KB Output is correct
74 Correct 116 ms 24088 KB Output is correct
75 Correct 121 ms 28004 KB Output is correct
76 Correct 126 ms 27984 KB Output is correct
77 Correct 127 ms 27984 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 3 ms 860 KB Output is correct
81 Correct 60 ms 13508 KB Output is correct
82 Correct 1 ms 348 KB Output is correct
83 Correct 0 ms 348 KB Output is correct
84 Correct 0 ms 348 KB Output is correct
85 Correct 31 ms 8276 KB Output is correct
86 Correct 81 ms 18068 KB Output is correct
87 Correct 77 ms 18216 KB Output is correct
88 Correct 62 ms 18092 KB Output is correct
89 Correct 57 ms 14244 KB Output is correct
90 Correct 0 ms 348 KB Output is correct
91 Correct 1 ms 464 KB Output is correct
92 Correct 3 ms 860 KB Output is correct
93 Correct 56 ms 14108 KB Output is correct
94 Correct 0 ms 348 KB Output is correct
95 Correct 0 ms 348 KB Output is correct
96 Correct 0 ms 348 KB Output is correct
97 Correct 15 ms 3932 KB Output is correct
98 Correct 74 ms 14240 KB Output is correct
99 Correct 64 ms 14104 KB Output is correct
100 Correct 59 ms 14104 KB Output is correct
101 Correct 56 ms 14116 KB Output is correct
102 Correct 55 ms 14164 KB Output is correct
103 Correct 62 ms 14104 KB Output is correct
104 Correct 52 ms 14172 KB Output is correct
105 Correct 67 ms 14160 KB Output is correct