답안 #776758

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
776758 2023-07-08T08:39:15 Z _martynas 슈퍼트리 잇기 (IOI20_supertrees) C++14
100 / 100
191 ms 24052 KB
#include "supertrees.h"
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

int n;

bool check_contr(int u, vector<bool> &visited, vector<bool> &in_group,
                 vector<int> &group, vector<vector<int>> &p) {
    visited[u] = in_group[u] = true;
    group.push_back(u);
    for(int v = 0; v < n; v++) {
        if(p[u][v] == 0) {
            if(in_group[v]) return true;
        }
        else if(!visited[v]) {
            if(check_contr(v, visited, in_group, group, p)) return true;
        }
    }
    return false;
}

int construct(vector<vector<int>> p) {
//	// sample interaction
//	int n = p.size();
//	vector<vector<int>> answer;
//	for (int i = 0; i < n; i++) {
//		vector<int> row;
//		row.resize(n);
//		answer.push_back(row);
//	}
//	build(answer);
//	return 1;
	n = p.size();
    // Impossible when any p_i == 3
    for(auto &v : p) for(int x : v) if(x == 3) return 0;
    // Check contradictions (a reaches b and b reaches c implies a reaches c)
    vector<bool> visited(n), in_group(n);
    vector<vector<int>> groups;
    for(int i = 0; i < n; i++) {
        if(!visited[i]) {
            vector<int> group;
            if(check_contr(i, visited, in_group, group, p)) return 0;
            for(int x : group) in_group[x] = false;
            groups.push_back(group);
        }
    }
    vector<vector<int>> ans(n, vector<int>(n));
    for(auto &g : groups) {
        // Every vertex p values within a group are either 1 or 2.
        // There is at most a single cycle in a group.
        // Vertices part of the cycle should have p == 2
        // for all other member of the groups except the subtree attached
        // After making the cycle a root other members
        // should have p == 1 to other vertices in the same subtree
        // and p == 2 to other vertices.
        //
        // Edge case: only 2 subtrees in a group are impossible
        vector<int> root(g.size(), -1);
        int subtrees = 0;
        for(int i = 0; i < g.size(); i++) {
            if(root[i] == -1) {
                // new subtree
                subtrees++;
                root[i] = g[i];
                for(int j = 0; j < g.size(); j++) {
                    if(i == j) continue;
                    if(p[g[i]][g[j]] != 2) {
                        if(root[j] != -1) {
                            return 0; // contradiction
                        }
                        root[j] = root[i];
                    }
                }
            }
            else {
                // check old subtree
                for(int j = 0; j < g.size(); j++) {
                    if(i == j) continue;
                    if(p[g[i]][g[j]] != 2) {
                        if(root[j] == -1 || root[i] != root[j]) {
                            return 0; // contradiction
                        }
                    }
                }
            }
        }
        if(subtrees == 2) return 0;
        for(int i = 0; i < g.size(); i++) {
            ans[g[i]][root[i]] = ans[root[i]][g[i]] = 1;
        }
        sort(root.begin(), root.end());
        root.erase(unique(root.begin(), root.end()), root.end());
        for(int i = 0; i < root.size(); i++) {
            ans[root[i]][root[(i+1)%root.size()]] = ans[root[(i+1)%root.size()]][root[i]] = 1;
        }
    }
    // ensure b[i][i] = 0
    for(int i = 0; i < n; i++) {
        ans[i][i] = 0;
    }
    build(ans);
    return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:63:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |         for(int i = 0; i < g.size(); i++) {
      |                        ~~^~~~~~~~~~
supertrees.cpp:68:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |                 for(int j = 0; j < g.size(); j++) {
      |                                ~~^~~~~~~~~~
supertrees.cpp:80:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |                 for(int j = 0; j < g.size(); j++) {
      |                                ~~^~~~~~~~~~
supertrees.cpp:91:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |         for(int i = 0; i < g.size(); i++) {
      |                        ~~^~~~~~~~~~
supertrees.cpp:96:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |         for(int i = 0; i < root.size(); i++) {
      |                        ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 296 KB Output is correct
3 Correct 1 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 1236 KB Output is correct
7 Correct 149 ms 24012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 296 KB Output is correct
3 Correct 1 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 1236 KB Output is correct
7 Correct 149 ms 24012 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 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 1184 KB Output is correct
13 Correct 148 ms 23968 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 64 ms 10108 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 37 ms 6240 KB Output is correct
21 Correct 161 ms 23960 KB Output is correct
22 Correct 145 ms 23976 KB Output is correct
23 Correct 151 ms 23920 KB Output is correct
24 Correct 156 ms 24036 KB Output is correct
25 Correct 70 ms 10104 KB Output is correct
26 Correct 56 ms 10112 KB Output is correct
27 Correct 152 ms 24052 KB Output is correct
28 Correct 148 ms 23988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 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 1 ms 212 KB Output is correct
8 Correct 6 ms 1252 KB Output is correct
9 Correct 144 ms 24028 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 6 ms 1176 KB Output is correct
13 Correct 158 ms 24052 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 65 ms 10100 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 300 KB Output is correct
21 Correct 38 ms 6196 KB Output is correct
22 Correct 150 ms 24000 KB Output is correct
23 Correct 150 ms 23932 KB Output is correct
24 Correct 179 ms 24008 KB Output is correct
25 Correct 59 ms 14080 KB Output is correct
26 Correct 87 ms 10096 KB Output is correct
27 Correct 148 ms 23936 KB Output is correct
28 Correct 191 ms 23976 KB Output is correct
29 Correct 59 ms 14100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 58 ms 6220 KB Output is correct
5 Correct 158 ms 23996 KB Output is correct
6 Correct 151 ms 23960 KB Output is correct
7 Correct 160 ms 24008 KB Output is correct
8 Correct 1 ms 352 KB Output is correct
9 Correct 46 ms 6256 KB Output is correct
10 Correct 160 ms 23976 KB Output is correct
11 Correct 147 ms 23988 KB Output is correct
12 Correct 154 ms 23920 KB Output is correct
13 Correct 1 ms 300 KB Output is correct
14 Correct 1 ms 300 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 40 ms 6220 KB Output is correct
17 Correct 153 ms 24012 KB Output is correct
18 Correct 144 ms 24000 KB Output is correct
19 Correct 145 ms 23924 KB Output is correct
20 Correct 145 ms 24012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 296 KB Output is correct
3 Correct 1 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 1236 KB Output is correct
7 Correct 149 ms 24012 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 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 1184 KB Output is correct
13 Correct 148 ms 23968 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 64 ms 10108 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 37 ms 6240 KB Output is correct
21 Correct 161 ms 23960 KB Output is correct
22 Correct 145 ms 23976 KB Output is correct
23 Correct 151 ms 23920 KB Output is correct
24 Correct 156 ms 24036 KB Output is correct
25 Correct 70 ms 10104 KB Output is correct
26 Correct 56 ms 10112 KB Output is correct
27 Correct 152 ms 24052 KB Output is correct
28 Correct 148 ms 23988 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 6 ms 1252 KB Output is correct
37 Correct 144 ms 24028 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 6 ms 1176 KB Output is correct
41 Correct 158 ms 24052 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 3 ms 596 KB Output is correct
45 Correct 65 ms 10100 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 300 KB Output is correct
49 Correct 38 ms 6196 KB Output is correct
50 Correct 150 ms 24000 KB Output is correct
51 Correct 150 ms 23932 KB Output is correct
52 Correct 179 ms 24008 KB Output is correct
53 Correct 59 ms 14080 KB Output is correct
54 Correct 87 ms 10096 KB Output is correct
55 Correct 148 ms 23936 KB Output is correct
56 Correct 191 ms 23976 KB Output is correct
57 Correct 59 ms 14100 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 0 ms 296 KB Output is correct
60 Correct 3 ms 596 KB Output is correct
61 Correct 75 ms 10060 KB Output is correct
62 Correct 1 ms 300 KB Output is correct
63 Correct 0 ms 296 KB Output is correct
64 Correct 0 ms 212 KB Output is correct
65 Correct 44 ms 6204 KB Output is correct
66 Correct 58 ms 10096 KB Output is correct
67 Correct 59 ms 10100 KB Output is correct
68 Correct 57 ms 10096 KB Output is correct
69 Correct 61 ms 14080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 296 KB Output is correct
3 Correct 1 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 1236 KB Output is correct
7 Correct 149 ms 24012 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 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 1184 KB Output is correct
13 Correct 148 ms 23968 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 64 ms 10108 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 37 ms 6240 KB Output is correct
21 Correct 161 ms 23960 KB Output is correct
22 Correct 145 ms 23976 KB Output is correct
23 Correct 151 ms 23920 KB Output is correct
24 Correct 156 ms 24036 KB Output is correct
25 Correct 70 ms 10104 KB Output is correct
26 Correct 56 ms 10112 KB Output is correct
27 Correct 152 ms 24052 KB Output is correct
28 Correct 148 ms 23988 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 6 ms 1252 KB Output is correct
37 Correct 144 ms 24028 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 6 ms 1176 KB Output is correct
41 Correct 158 ms 24052 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 3 ms 596 KB Output is correct
45 Correct 65 ms 10100 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 300 KB Output is correct
49 Correct 38 ms 6196 KB Output is correct
50 Correct 150 ms 24000 KB Output is correct
51 Correct 150 ms 23932 KB Output is correct
52 Correct 179 ms 24008 KB Output is correct
53 Correct 59 ms 14080 KB Output is correct
54 Correct 87 ms 10096 KB Output is correct
55 Correct 148 ms 23936 KB Output is correct
56 Correct 191 ms 23976 KB Output is correct
57 Correct 59 ms 14100 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 300 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 58 ms 6220 KB Output is correct
62 Correct 158 ms 23996 KB Output is correct
63 Correct 151 ms 23960 KB Output is correct
64 Correct 160 ms 24008 KB Output is correct
65 Correct 1 ms 352 KB Output is correct
66 Correct 46 ms 6256 KB Output is correct
67 Correct 160 ms 23976 KB Output is correct
68 Correct 147 ms 23988 KB Output is correct
69 Correct 154 ms 23920 KB Output is correct
70 Correct 1 ms 300 KB Output is correct
71 Correct 1 ms 300 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 40 ms 6220 KB Output is correct
74 Correct 153 ms 24012 KB Output is correct
75 Correct 144 ms 24000 KB Output is correct
76 Correct 145 ms 23924 KB Output is correct
77 Correct 145 ms 24012 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 0 ms 296 KB Output is correct
80 Correct 3 ms 596 KB Output is correct
81 Correct 75 ms 10060 KB Output is correct
82 Correct 1 ms 300 KB Output is correct
83 Correct 0 ms 296 KB Output is correct
84 Correct 0 ms 212 KB Output is correct
85 Correct 44 ms 6204 KB Output is correct
86 Correct 58 ms 10096 KB Output is correct
87 Correct 59 ms 10100 KB Output is correct
88 Correct 57 ms 10096 KB Output is correct
89 Correct 61 ms 14080 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 1 ms 212 KB Output is correct
92 Correct 3 ms 596 KB Output is correct
93 Correct 59 ms 10116 KB Output is correct
94 Correct 1 ms 212 KB Output is correct
95 Correct 0 ms 212 KB Output is correct
96 Correct 1 ms 212 KB Output is correct
97 Correct 14 ms 2676 KB Output is correct
98 Correct 63 ms 10096 KB Output is correct
99 Correct 57 ms 10100 KB Output is correct
100 Correct 62 ms 10096 KB Output is correct
101 Correct 55 ms 10100 KB Output is correct
102 Correct 58 ms 10100 KB Output is correct
103 Correct 56 ms 10100 KB Output is correct
104 Correct 56 ms 10104 KB Output is correct
105 Correct 60 ms 10008 KB Output is correct