Submission #776730

# Submission time Handle Problem Language Result Execution time Memory
776730 2023-07-08T08:06:07 Z _martynas Connecting Supertrees (IOI20_supertrees) C++14
46 / 100
165 ms 24140 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_stack,
                 vector<int> &group, vector<vector<int>> &p) {
    visited[u] = in_stack[u] = true;
    group.push_back(u);
    for(int v = 0; v < n; v++) {
        if(p[u][v] == 0) {
            if(in_stack[v]) return true;
        }
        else if(!visited[v]) {
            if(check_contr(v, visited, in_stack, group, p)) return true;
        }
    }
    in_stack[u] = false;
    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();
	// check p[i][j] == p[j][i]
	for(int i = 0; i < n; i++) for(int j = 0; j < n; j++)
        if(p[i][j] != p[j][i]) return 0;
    // 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_stack(n);
    vector<vector<int>> groups;
    for(int i = 0; i < n; i++) {
        if(!visited[i]) {
            vector<int> group;
            if(check_contr(i, visited, in_stack, group, p)) return 0;
            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.
        vector<int> root(g.size(), -1);
        for(int i = 0; i < g.size(); i++) {
            if(root[i] == -1) {
                // new subtree
                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
                        }
                    }
                }
            }
        }
        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:64:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |         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:90:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |         for(int i = 0; i < g.size(); i++) {
      |                        ~~^~~~~~~~~~
supertrees.cpp:95:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |         for(int i = 0; i < root.size(); i++) {
      |                        ~~^~~~~~~~~~~~~
# Verdict Execution time Memory 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 1 ms 296 KB Output is correct
6 Correct 7 ms 1244 KB Output is correct
7 Correct 154 ms 23972 KB Output is correct
# Verdict Execution time Memory 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 1 ms 296 KB Output is correct
6 Correct 7 ms 1244 KB Output is correct
7 Correct 154 ms 23972 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 8 ms 1236 KB Output is correct
13 Correct 154 ms 24052 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 3 ms 696 KB Output is correct
17 Correct 63 ms 10108 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Incorrect 1 ms 212 KB Answer gives possible 1 while actual possible 0
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 1 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 -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 38 ms 6200 KB Output is correct
5 Correct 152 ms 23936 KB Output is correct
6 Correct 162 ms 23928 KB Output is correct
7 Correct 161 ms 24012 KB Output is correct
8 Correct 0 ms 300 KB Output is correct
9 Correct 38 ms 6260 KB Output is correct
10 Correct 155 ms 23928 KB Output is correct
11 Correct 150 ms 24012 KB Output is correct
12 Correct 165 ms 24048 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 304 KB Output is correct
16 Correct 39 ms 6224 KB Output is correct
17 Correct 156 ms 24008 KB Output is correct
18 Correct 152 ms 24012 KB Output is correct
19 Correct 155 ms 23936 KB Output is correct
20 Correct 160 ms 24140 KB Output is correct
# Verdict Execution time Memory 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 1 ms 296 KB Output is correct
6 Correct 7 ms 1244 KB Output is correct
7 Correct 154 ms 23972 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 8 ms 1236 KB Output is correct
13 Correct 154 ms 24052 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 3 ms 696 KB Output is correct
17 Correct 63 ms 10108 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Incorrect 1 ms 212 KB Answer gives possible 1 while actual possible 0
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory 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 1 ms 296 KB Output is correct
6 Correct 7 ms 1244 KB Output is correct
7 Correct 154 ms 23972 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 8 ms 1236 KB Output is correct
13 Correct 154 ms 24052 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 3 ms 696 KB Output is correct
17 Correct 63 ms 10108 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Incorrect 1 ms 212 KB Answer gives possible 1 while actual possible 0
20 Halted 0 ms 0 KB -