답안 #433576

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
433576 2021-06-20T07:47:49 Z sean617 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
273 ms 25160 KB
#include "supertrees.h"
#include <vector>
#include <cstring>
#define N 1005

using namespace std;
int n, cnt, bu[N], bu2[N], gut[N];
bool v[N], co[N][N], icy[N];
vector<int> gu[N], cy[N], cy2, ans, ch[N];
vector<std::vector<int> > answer;
int f(int p) {
    if (bu[p] == p) return p;
    else return bu[p] = f(bu[p]);
}

int f2(int p) {
	if (bu2[p] == p) return p;
	else return bu2[p] = f2(bu2[p]);
}

void f_co(int p, int q) {
    co[p][q] = co[q][p] = 1;
}

int construct(std::vector<std::vector<int> > p) {
    int i, j, k, t, t1, t2, la, tcnt;
	n = p.size();
	for (i = 0; i < n; i++) {bu[i] = i; bu2[i] = i;}
	for (i = 0; i < n; i++) {
        for (j = i + 1; j < n; j++) {
            if (p[i][j]== 0) continue;
            if (p[i][j] == 3) return 0;
            t1 = f(i);
            t2 = f(j);
            if (t1 != t2) bu[t1] = t2;
        }
	}
	memset(gut, -1, sizeof(gut));
	for (i = 0; i < n; i++) f(i);
	for (i = 0; i < n; i++) {
		tcnt = 0;
		for (j = 0; j < n; j++) {
			if (i == j) continue;
			if (bu[i] != bu[j]) continue;
			if (p[i][j] == 0) return 0;
			if (p[i][j] == 1) {
				t1 = f2(i);
				t2 = f2(j);
				if (t1 != t2) bu2[t1] = t2;
				tcnt++;
			}
		}
		if (tcnt == 0) {
			icy[i] = 1;
			cy[bu[i]].push_back(i);
		}
	}
	for (i = 0; i < n; i++) {
		for (j = i + 1; j < n; j++) {
			if (bu2[i] != bu2[j]) continue;
			if (p[i][j] != 1) return 0;
		}
	}
	for (i = 0; i < n; i++) {
		if (cy[i].empty()) continue;
		for (j = 0; j < cy[i].size(); j++) {
			for (k = j + 1; k < cy[i].size(); k++) {
				t1 = cy[i][j];
				t2 = cy[i][k];
				if (p[t1][t2] != 2) return 0;
			}
		}
//		int cysz = cy[i].size();
//		for (j = 0; j < cysz; j++) {
//			t1 = cy[i][j];
//			t2 = cy[i][(j + 1) % cysz];
//			f_co(t1, t2);
//		}
	}
	for (i = 0; i < n; i++) {
		if (icy[i]) continue;
		for (j = i + 1; j < n; j++) {
			if (bu[i] != bu[j]) continue;
			if (bu2[i] == bu2[j] && p[i][j] != 1 || bu2[i] != bu2[j] && p[i][j] != 2) return 0;
		}
		if (gut[bu2[i]] == -1) {
			cy[bu[i]].push_back(i);
		} else {
			f_co(gut[bu2[i]], i);
		}
		gut[bu2[i]] = i;
	}
	for (i = 0; i < n; i++) {
		if (cy[i].empty() || cy[i].size() == 1) continue;
		if (cy[i].size() == 2) return 0;
		int cysz = cy[i].size();
		for (j = 0; j < cysz; j++) {
			t1 = cy[i][j];
			t2 = cy[i][(j + 1) % cysz];
			f_co(t1, t2);
		}
	}
	/*for (i =0; i < n; i++) {
        t = f(i);
        gu[t].push_back(i);
	}
	for (i = 0; i < n; i++) {
        t = f(i);
        if (gu[t].size() == 1) continue;
        for (j = 0; j < gu[t].size(); j++) {
            if (gu[t][j] == i) continue;
            if (p[i][gu[t][j]] != 2) break;
        }
        if (j == gu[t].size()) {
            v[i] = 1;
            cy[t].push_back(i);
        }
	}

	for (i = 0; i < n; i++) {
        if (v[i]) continue;
        t = f(i);
        if (gu[t].size() == 1) continue;
        for (j = 0; j < gu[t].size(); j++) {
            if (gu[t][j] == i) continue;
            if (p[i][gu[t][j]] == 1 && v[gu[t][j]]) break;
        }
        if (j == gu[t].size()) {
            v[i] = 1;
            cy[t].push_back(i);
            cy2.push_back(i);
        }
	}
	for (i = 0; i < n; i++) {
        int sz = cy[i].size();
        if (sz >= 2) {
            for (j = 0; j < sz; j++) {
                f_co(cy[i][j], cy[i][(j + 1) % sz]);
            }
        }
	}
	for (i = 0; i < cy2.size(); i++) {
        la = t = cy2[i];
        t2 = f(t);
        ch[i].push_back(la);
        for (j = 0; j < gu[t2].size(); j++) {
            if (gu[t2][j] != t && p[t][gu[t2][j]] == 1) {
                f_co(la, gu[t2][j]);
                la = gu[t2][j];
                ch[i].push_back(la);
            }
        }
	}
	for (i = 0; i < cy2.size(); i++) {
        for (j = 0; j < ch[i].size(); j++) {
            for (k = j + 1; k < ch[i].size(); k++) {
                if (p[ch[i][j]][ch[i][k]] != 1) return 0;
            }
        }
	}
*/	for (i = 0; i < n; i++) {
        ans.clear();
        for (j =0; j < n; j++) {
            ans.push_back(co[i][j]);
        }
		answer.push_back(ans);
	}
	build(answer);
	return 1;
}




Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:66:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |   for (j = 0; j < cy[i].size(); j++) {
      |               ~~^~~~~~~~~~~~~~
supertrees.cpp:67:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |    for (k = j + 1; k < cy[i].size(); k++) {
      |                    ~~^~~~~~~~~~~~~~
supertrees.cpp:84:25: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   84 |    if (bu2[i] == bu2[j] && p[i][j] != 1 || bu2[i] != bu2[j] && p[i][j] != 2) return 0;
      |        ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
supertrees.cpp:26:18: warning: unused variable 't' [-Wunused-variable]
   26 |     int i, j, k, t, t1, t2, la, tcnt;
      |                  ^
supertrees.cpp:26:29: warning: unused variable 'la' [-Wunused-variable]
   26 |     int i, j, k, t, t1, t2, la, tcnt;
      |                             ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 11 ms 1436 KB Output is correct
7 Correct 255 ms 23104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 11 ms 1436 KB Output is correct
7 Correct 255 ms 23104 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 10 ms 1244 KB Output is correct
13 Correct 235 ms 22088 KB Output is correct
14 Correct 1 ms 352 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 5 ms 588 KB Output is correct
17 Correct 136 ms 8128 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 63 ms 6336 KB Output is correct
21 Correct 243 ms 23120 KB Output is correct
22 Correct 261 ms 23116 KB Output is correct
23 Correct 260 ms 23104 KB Output is correct
24 Correct 241 ms 22352 KB Output is correct
25 Correct 103 ms 8128 KB Output is correct
26 Correct 99 ms 8136 KB Output is correct
27 Correct 269 ms 23112 KB Output is correct
28 Correct 268 ms 22344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 11 ms 1228 KB Output is correct
9 Correct 247 ms 24128 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 10 ms 1484 KB Output is correct
13 Correct 246 ms 25028 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 360 KB Output is correct
16 Correct 5 ms 732 KB Output is correct
17 Correct 111 ms 10160 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 75 ms 6792 KB Output is correct
22 Correct 246 ms 25032 KB Output is correct
23 Correct 240 ms 25044 KB Output is correct
24 Correct 259 ms 25160 KB Output is correct
25 Correct 105 ms 10312 KB Output is correct
26 Correct 102 ms 10160 KB Output is correct
27 Correct 236 ms 25004 KB Output is correct
28 Correct 272 ms 25124 KB Output is correct
29 Correct 112 ms 10296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 61 ms 6228 KB Output is correct
5 Correct 242 ms 23100 KB Output is correct
6 Correct 241 ms 23024 KB Output is correct
7 Correct 256 ms 23036 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 59 ms 6328 KB Output is correct
10 Correct 238 ms 23112 KB Output is correct
11 Correct 241 ms 23044 KB Output is correct
12 Correct 251 ms 23100 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 65 ms 6232 KB Output is correct
17 Correct 241 ms 23116 KB Output is correct
18 Correct 268 ms 23104 KB Output is correct
19 Correct 257 ms 23104 KB Output is correct
20 Correct 273 ms 22344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 11 ms 1436 KB Output is correct
7 Correct 255 ms 23104 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 10 ms 1244 KB Output is correct
13 Correct 235 ms 22088 KB Output is correct
14 Correct 1 ms 352 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 5 ms 588 KB Output is correct
17 Correct 136 ms 8128 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 63 ms 6336 KB Output is correct
21 Correct 243 ms 23120 KB Output is correct
22 Correct 261 ms 23116 KB Output is correct
23 Correct 260 ms 23104 KB Output is correct
24 Correct 241 ms 22352 KB Output is correct
25 Correct 103 ms 8128 KB Output is correct
26 Correct 99 ms 8136 KB Output is correct
27 Correct 269 ms 23112 KB Output is correct
28 Correct 268 ms 22344 KB Output is correct
29 Correct 0 ms 332 KB Output is correct
30 Correct 0 ms 332 KB Output is correct
31 Correct 0 ms 332 KB Output is correct
32 Correct 0 ms 332 KB Output is correct
33 Correct 0 ms 332 KB Output is correct
34 Correct 0 ms 332 KB Output is correct
35 Correct 0 ms 332 KB Output is correct
36 Correct 11 ms 1228 KB Output is correct
37 Correct 247 ms 24128 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 10 ms 1484 KB Output is correct
41 Correct 246 ms 25028 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 360 KB Output is correct
44 Correct 5 ms 732 KB Output is correct
45 Correct 111 ms 10160 KB Output is correct
46 Correct 0 ms 332 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 0 ms 332 KB Output is correct
49 Correct 75 ms 6792 KB Output is correct
50 Correct 246 ms 25032 KB Output is correct
51 Correct 240 ms 25044 KB Output is correct
52 Correct 259 ms 25160 KB Output is correct
53 Correct 105 ms 10312 KB Output is correct
54 Correct 102 ms 10160 KB Output is correct
55 Correct 236 ms 25004 KB Output is correct
56 Correct 272 ms 25124 KB Output is correct
57 Correct 112 ms 10296 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 5 ms 716 KB Output is correct
61 Correct 111 ms 10156 KB Output is correct
62 Correct 1 ms 332 KB Output is correct
63 Correct 1 ms 332 KB Output is correct
64 Correct 1 ms 332 KB Output is correct
65 Correct 64 ms 6832 KB Output is correct
66 Correct 108 ms 10172 KB Output is correct
67 Correct 103 ms 10156 KB Output is correct
68 Correct 101 ms 10180 KB Output is correct
69 Correct 105 ms 10284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 11 ms 1436 KB Output is correct
7 Correct 255 ms 23104 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 10 ms 1244 KB Output is correct
13 Correct 235 ms 22088 KB Output is correct
14 Correct 1 ms 352 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 5 ms 588 KB Output is correct
17 Correct 136 ms 8128 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 63 ms 6336 KB Output is correct
21 Correct 243 ms 23120 KB Output is correct
22 Correct 261 ms 23116 KB Output is correct
23 Correct 260 ms 23104 KB Output is correct
24 Correct 241 ms 22352 KB Output is correct
25 Correct 103 ms 8128 KB Output is correct
26 Correct 99 ms 8136 KB Output is correct
27 Correct 269 ms 23112 KB Output is correct
28 Correct 268 ms 22344 KB Output is correct
29 Correct 0 ms 332 KB Output is correct
30 Correct 0 ms 332 KB Output is correct
31 Correct 0 ms 332 KB Output is correct
32 Correct 0 ms 332 KB Output is correct
33 Correct 0 ms 332 KB Output is correct
34 Correct 0 ms 332 KB Output is correct
35 Correct 0 ms 332 KB Output is correct
36 Correct 11 ms 1228 KB Output is correct
37 Correct 247 ms 24128 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 10 ms 1484 KB Output is correct
41 Correct 246 ms 25028 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 360 KB Output is correct
44 Correct 5 ms 732 KB Output is correct
45 Correct 111 ms 10160 KB Output is correct
46 Correct 0 ms 332 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 0 ms 332 KB Output is correct
49 Correct 75 ms 6792 KB Output is correct
50 Correct 246 ms 25032 KB Output is correct
51 Correct 240 ms 25044 KB Output is correct
52 Correct 259 ms 25160 KB Output is correct
53 Correct 105 ms 10312 KB Output is correct
54 Correct 102 ms 10160 KB Output is correct
55 Correct 236 ms 25004 KB Output is correct
56 Correct 272 ms 25124 KB Output is correct
57 Correct 112 ms 10296 KB Output is correct
58 Correct 0 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 1 ms 332 KB Output is correct
61 Correct 61 ms 6228 KB Output is correct
62 Correct 242 ms 23100 KB Output is correct
63 Correct 241 ms 23024 KB Output is correct
64 Correct 256 ms 23036 KB Output is correct
65 Correct 1 ms 332 KB Output is correct
66 Correct 59 ms 6328 KB Output is correct
67 Correct 238 ms 23112 KB Output is correct
68 Correct 241 ms 23044 KB Output is correct
69 Correct 251 ms 23100 KB Output is correct
70 Correct 0 ms 332 KB Output is correct
71 Correct 1 ms 332 KB Output is correct
72 Correct 1 ms 332 KB Output is correct
73 Correct 65 ms 6232 KB Output is correct
74 Correct 241 ms 23116 KB Output is correct
75 Correct 268 ms 23104 KB Output is correct
76 Correct 257 ms 23104 KB Output is correct
77 Correct 273 ms 22344 KB Output is correct
78 Correct 1 ms 332 KB Output is correct
79 Correct 1 ms 332 KB Output is correct
80 Correct 5 ms 716 KB Output is correct
81 Correct 111 ms 10156 KB Output is correct
82 Correct 1 ms 332 KB Output is correct
83 Correct 1 ms 332 KB Output is correct
84 Correct 1 ms 332 KB Output is correct
85 Correct 64 ms 6832 KB Output is correct
86 Correct 108 ms 10172 KB Output is correct
87 Correct 103 ms 10156 KB Output is correct
88 Correct 101 ms 10180 KB Output is correct
89 Correct 105 ms 10284 KB Output is correct
90 Correct 1 ms 332 KB Output is correct
91 Correct 1 ms 332 KB Output is correct
92 Correct 5 ms 724 KB Output is correct
93 Correct 105 ms 10164 KB Output is correct
94 Correct 1 ms 332 KB Output is correct
95 Correct 1 ms 332 KB Output is correct
96 Correct 1 ms 332 KB Output is correct
97 Correct 25 ms 2756 KB Output is correct
98 Correct 104 ms 10160 KB Output is correct
99 Correct 100 ms 10160 KB Output is correct
100 Correct 121 ms 10180 KB Output is correct
101 Correct 107 ms 10176 KB Output is correct
102 Correct 102 ms 10156 KB Output is correct
103 Correct 100 ms 10156 KB Output is correct
104 Correct 136 ms 10176 KB Output is correct
105 Correct 103 ms 10160 KB Output is correct