답안 #303962

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
303962 2020-09-20T20:34:39 Z AlanC 슈퍼트리 잇기 (IOI20_supertrees) C++14
100 / 100
295 ms 26360 KB
#include "supertrees.h"
#include <vector>
#include <cstdio>
#define N 1005
using namespace std;

int n, cnt;

vector<vector<int>> answer, cp, e, cyc;
vector<int> s;
int v[N], in[N];
bool error = false;



void DFS(int x) {
	s.push_back(x);
	v[x] = 1;
	for (int i=0; i<n; i++)
		if (e[x][i] == 1 && !v[i])
			DFS(i);
}

void findTrees() {
	cnt = 0;
	cp.clear();
	for (int i=0; i<n; i++) v[i] = 0;
	for (int i=0; i<n; i++)
		if (!v[i]) {
			s.clear();		
			DFS(i);
			cp.push_back(s);
		}
}



void checkTrees() {
	for (int k=0; k<cp.size(); k++) {
		for (int i=0; i<cp[k].size(); i++)
			in[cp[k][i]] = k;
	}
	
	for (int k=0; k<cp.size(); k++) {
		for (int j=0; j<n; j++) {
			int last = -1;
			for (int i=0; i<cp[k].size(); i++) {
				int x = cp[k][i];
				if (in[x] == in[j]) {
					if (e[x][j] != 1) error = true;
				}
				else {
					if (last == -1) last = e[x][j];
					else if (last != e[x][j]) error = true;
				}
			}
		}
	}
}

void DFS_C(int x) {
	v[x] = 1;
	s.push_back(x);
	for (int i=0; i<cp.size(); i++)
		if (!v[i] && e[cp[x][0]][cp[i][0]] == 2)
			DFS_C(i);
}

void fixCycles() {
	cyc.clear();
	for (int i=0; i<cp.size(); i++) v[i] = 0;
	for (int i=0; i<cp.size(); i++)
		if (!v[i]) {
			s.clear();
			DFS_C(i);
			cyc.push_back(s);
		}
}


void checkCycles() {
	for (int k=0; k<cyc.size(); k++) {
		for (int i=0; i<cyc[k].size(); i++)
			for (int j=0; j<i; j++) {
				int x = cp[cyc[k][i]][0];
				int y = cp[cyc[k][j]][0];
				if (e[x][y] != 2 || e[y][x] != 2) error = true;
			}
	}
}

void check3() {
	for (int i=0; i<n; i++)
	for (int j=0; j<n; j++)
		if (e[i][j] == 3) error = true;
}


int construct(vector<vector<int>> p) {
	n = p.size();
	e = p;
	/*
	vector<vector<int>> answer;
	for (int i = 0; i < n; i++) {
		vector<int> row;
		row.resize(n);
		answer.push_back(row);
	}
	*/
	findTrees();
	checkTrees();
	if (error) return 0;
	fixCycles();
	checkCycles();
	if (error) return 0;
	
	check3();
	if (error) return 0;
	
	for (int i=0; i<n; i++) {
		vector<int> row;
		row.resize(n);
		answer.push_back(row);
	}
	for (int i=0; i<cp.size(); i++) {
		for (int j=0; j<cp[i].size()-1; j++) {
			int x = cp[i][j];
			int y = cp[i][j+1];
			answer[x][y] = answer[y][x] = 1;
		}
	}
	
	for (int i=0; i<cyc.size(); i++) {
		if (cyc[i].size() == 1) continue;
		if (cyc[i].size() == 2) error = true;
		for (int j=0; j<cyc[i].size(); j++) {
			int x = cp[cyc[i][j]][0];
			int y = cp[cyc[i][(j+1) % cyc[i].size()]][0];
			answer[x][y] = answer[y][x] = 1;
		}
	}
	
	if (error) return 0;
	
	build(answer);
	return 1;
}

Compilation message

supertrees.cpp: In function 'void checkTrees()':
supertrees.cpp:39:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |  for (int k=0; k<cp.size(); k++) {
      |                ~^~~~~~~~~~
supertrees.cpp:40:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |   for (int i=0; i<cp[k].size(); i++)
      |                 ~^~~~~~~~~~~~~
supertrees.cpp:44:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |  for (int k=0; k<cp.size(); k++) {
      |                ~^~~~~~~~~~
supertrees.cpp:47:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |    for (int i=0; i<cp[k].size(); i++) {
      |                  ~^~~~~~~~~~~~~
supertrees.cpp: In function 'void DFS_C(int)':
supertrees.cpp:64:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |  for (int i=0; i<cp.size(); i++)
      |                ~^~~~~~~~~~
supertrees.cpp: In function 'void fixCycles()':
supertrees.cpp:71:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |  for (int i=0; i<cp.size(); i++) v[i] = 0;
      |                ~^~~~~~~~~~
supertrees.cpp:72:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |  for (int i=0; i<cp.size(); i++)
      |                ~^~~~~~~~~~
supertrees.cpp: In function 'void checkCycles()':
supertrees.cpp:82:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |  for (int k=0; k<cyc.size(); k++) {
      |                ~^~~~~~~~~~~
supertrees.cpp:83:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |   for (int i=0; i<cyc[k].size(); i++)
      |                 ~^~~~~~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:125:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  125 |  for (int i=0; i<cp.size(); i++) {
      |                ~^~~~~~~~~~
supertrees.cpp:126:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  126 |   for (int j=0; j<cp[i].size()-1; j++) {
      |                 ~^~~~~~~~~~~~~~~
supertrees.cpp:133:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |  for (int i=0; i<cyc.size(); i++) {
      |                ~^~~~~~~~~~~
supertrees.cpp:136:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  136 |   for (int j=0; j<cyc[i].size(); j++) {
      |                 ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 10 ms 1408 KB Output is correct
7 Correct 280 ms 26192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 10 ms 1408 KB Output is correct
7 Correct 280 ms 26192 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 1 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 10 ms 1408 KB Output is correct
13 Correct 257 ms 26360 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 7 ms 768 KB Output is correct
17 Correct 121 ms 12152 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 63 ms 6776 KB Output is correct
21 Correct 258 ms 26192 KB Output is correct
22 Correct 256 ms 26104 KB Output is correct
23 Correct 262 ms 26104 KB Output is correct
24 Correct 253 ms 26104 KB Output is correct
25 Correct 110 ms 12152 KB Output is correct
26 Correct 104 ms 12152 KB Output is correct
27 Correct 295 ms 26104 KB Output is correct
28 Correct 253 ms 26104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 10 ms 1408 KB Output is correct
9 Correct 253 ms 26104 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 11 ms 1536 KB Output is correct
13 Correct 261 ms 26104 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 7 ms 768 KB Output is correct
17 Correct 146 ms 12280 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 0 ms 256 KB Output is correct
21 Correct 63 ms 6776 KB Output is correct
22 Correct 278 ms 26104 KB Output is correct
23 Correct 255 ms 26104 KB Output is correct
24 Correct 274 ms 26232 KB Output is correct
25 Correct 115 ms 16248 KB Output is correct
26 Correct 115 ms 12152 KB Output is correct
27 Correct 255 ms 26104 KB Output is correct
28 Correct 278 ms 26200 KB Output is correct
29 Correct 111 ms 16248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 64 ms 6776 KB Output is correct
5 Correct 261 ms 26068 KB Output is correct
6 Correct 254 ms 26104 KB Output is correct
7 Correct 276 ms 26104 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 65 ms 6776 KB Output is correct
10 Correct 270 ms 26104 KB Output is correct
11 Correct 263 ms 26068 KB Output is correct
12 Correct 266 ms 26160 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 65 ms 6824 KB Output is correct
17 Correct 256 ms 26104 KB Output is correct
18 Correct 279 ms 26104 KB Output is correct
19 Correct 256 ms 26104 KB Output is correct
20 Correct 273 ms 26232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 10 ms 1408 KB Output is correct
7 Correct 280 ms 26192 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 1 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 10 ms 1408 KB Output is correct
13 Correct 257 ms 26360 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 7 ms 768 KB Output is correct
17 Correct 121 ms 12152 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 63 ms 6776 KB Output is correct
21 Correct 258 ms 26192 KB Output is correct
22 Correct 256 ms 26104 KB Output is correct
23 Correct 262 ms 26104 KB Output is correct
24 Correct 253 ms 26104 KB Output is correct
25 Correct 110 ms 12152 KB Output is correct
26 Correct 104 ms 12152 KB Output is correct
27 Correct 295 ms 26104 KB Output is correct
28 Correct 253 ms 26104 KB Output is correct
29 Correct 0 ms 256 KB Output is correct
30 Correct 0 ms 256 KB Output is correct
31 Correct 0 ms 256 KB Output is correct
32 Correct 0 ms 256 KB Output is correct
33 Correct 0 ms 256 KB Output is correct
34 Correct 1 ms 256 KB Output is correct
35 Correct 0 ms 256 KB Output is correct
36 Correct 10 ms 1408 KB Output is correct
37 Correct 253 ms 26104 KB Output is correct
38 Correct 0 ms 256 KB Output is correct
39 Correct 0 ms 256 KB Output is correct
40 Correct 11 ms 1536 KB Output is correct
41 Correct 261 ms 26104 KB Output is correct
42 Correct 0 ms 256 KB Output is correct
43 Correct 0 ms 256 KB Output is correct
44 Correct 7 ms 768 KB Output is correct
45 Correct 146 ms 12280 KB Output is correct
46 Correct 1 ms 256 KB Output is correct
47 Correct 0 ms 256 KB Output is correct
48 Correct 0 ms 256 KB Output is correct
49 Correct 63 ms 6776 KB Output is correct
50 Correct 278 ms 26104 KB Output is correct
51 Correct 255 ms 26104 KB Output is correct
52 Correct 274 ms 26232 KB Output is correct
53 Correct 115 ms 16248 KB Output is correct
54 Correct 115 ms 12152 KB Output is correct
55 Correct 255 ms 26104 KB Output is correct
56 Correct 278 ms 26200 KB Output is correct
57 Correct 111 ms 16248 KB Output is correct
58 Correct 0 ms 256 KB Output is correct
59 Correct 0 ms 256 KB Output is correct
60 Correct 5 ms 768 KB Output is correct
61 Correct 124 ms 12152 KB Output is correct
62 Correct 0 ms 256 KB Output is correct
63 Correct 0 ms 256 KB Output is correct
64 Correct 0 ms 256 KB Output is correct
65 Correct 65 ms 6776 KB Output is correct
66 Correct 116 ms 12280 KB Output is correct
67 Correct 105 ms 12152 KB Output is correct
68 Correct 123 ms 12152 KB Output is correct
69 Correct 133 ms 16248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 10 ms 1408 KB Output is correct
7 Correct 280 ms 26192 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 1 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 10 ms 1408 KB Output is correct
13 Correct 257 ms 26360 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 7 ms 768 KB Output is correct
17 Correct 121 ms 12152 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 63 ms 6776 KB Output is correct
21 Correct 258 ms 26192 KB Output is correct
22 Correct 256 ms 26104 KB Output is correct
23 Correct 262 ms 26104 KB Output is correct
24 Correct 253 ms 26104 KB Output is correct
25 Correct 110 ms 12152 KB Output is correct
26 Correct 104 ms 12152 KB Output is correct
27 Correct 295 ms 26104 KB Output is correct
28 Correct 253 ms 26104 KB Output is correct
29 Correct 0 ms 256 KB Output is correct
30 Correct 0 ms 256 KB Output is correct
31 Correct 0 ms 256 KB Output is correct
32 Correct 0 ms 256 KB Output is correct
33 Correct 0 ms 256 KB Output is correct
34 Correct 1 ms 256 KB Output is correct
35 Correct 0 ms 256 KB Output is correct
36 Correct 10 ms 1408 KB Output is correct
37 Correct 253 ms 26104 KB Output is correct
38 Correct 0 ms 256 KB Output is correct
39 Correct 0 ms 256 KB Output is correct
40 Correct 11 ms 1536 KB Output is correct
41 Correct 261 ms 26104 KB Output is correct
42 Correct 0 ms 256 KB Output is correct
43 Correct 0 ms 256 KB Output is correct
44 Correct 7 ms 768 KB Output is correct
45 Correct 146 ms 12280 KB Output is correct
46 Correct 1 ms 256 KB Output is correct
47 Correct 0 ms 256 KB Output is correct
48 Correct 0 ms 256 KB Output is correct
49 Correct 63 ms 6776 KB Output is correct
50 Correct 278 ms 26104 KB Output is correct
51 Correct 255 ms 26104 KB Output is correct
52 Correct 274 ms 26232 KB Output is correct
53 Correct 115 ms 16248 KB Output is correct
54 Correct 115 ms 12152 KB Output is correct
55 Correct 255 ms 26104 KB Output is correct
56 Correct 278 ms 26200 KB Output is correct
57 Correct 111 ms 16248 KB Output is correct
58 Correct 0 ms 256 KB Output is correct
59 Correct 0 ms 256 KB Output is correct
60 Correct 1 ms 256 KB Output is correct
61 Correct 64 ms 6776 KB Output is correct
62 Correct 261 ms 26068 KB Output is correct
63 Correct 254 ms 26104 KB Output is correct
64 Correct 276 ms 26104 KB Output is correct
65 Correct 0 ms 256 KB Output is correct
66 Correct 65 ms 6776 KB Output is correct
67 Correct 270 ms 26104 KB Output is correct
68 Correct 263 ms 26068 KB Output is correct
69 Correct 266 ms 26160 KB Output is correct
70 Correct 0 ms 256 KB Output is correct
71 Correct 0 ms 256 KB Output is correct
72 Correct 0 ms 256 KB Output is correct
73 Correct 65 ms 6824 KB Output is correct
74 Correct 256 ms 26104 KB Output is correct
75 Correct 279 ms 26104 KB Output is correct
76 Correct 256 ms 26104 KB Output is correct
77 Correct 273 ms 26232 KB Output is correct
78 Correct 0 ms 256 KB Output is correct
79 Correct 0 ms 256 KB Output is correct
80 Correct 5 ms 768 KB Output is correct
81 Correct 124 ms 12152 KB Output is correct
82 Correct 0 ms 256 KB Output is correct
83 Correct 0 ms 256 KB Output is correct
84 Correct 0 ms 256 KB Output is correct
85 Correct 65 ms 6776 KB Output is correct
86 Correct 116 ms 12280 KB Output is correct
87 Correct 105 ms 12152 KB Output is correct
88 Correct 123 ms 12152 KB Output is correct
89 Correct 133 ms 16248 KB Output is correct
90 Correct 0 ms 256 KB Output is correct
91 Correct 1 ms 256 KB Output is correct
92 Correct 5 ms 768 KB Output is correct
93 Correct 118 ms 12152 KB Output is correct
94 Correct 1 ms 256 KB Output is correct
95 Correct 0 ms 256 KB Output is correct
96 Correct 0 ms 256 KB Output is correct
97 Correct 27 ms 3320 KB Output is correct
98 Correct 113 ms 12236 KB Output is correct
99 Correct 106 ms 12152 KB Output is correct
100 Correct 108 ms 12280 KB Output is correct
101 Correct 112 ms 12280 KB Output is correct
102 Correct 118 ms 12408 KB Output is correct
103 Correct 121 ms 12280 KB Output is correct
104 Correct 125 ms 12152 KB Output is correct
105 Correct 126 ms 12280 KB Output is correct