답안 #776739

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
776739 2023-07-08T08:11:53 Z _martynas 슈퍼트리 잇기 (IOI20_supertrees) C++14
0 / 100
1 ms 304 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;
        }
        else if(!in_stack[v]) {
            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:67:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |         for(int i = 0; i < g.size(); i++) {
      |                        ~~^~~~~~~~~~
supertrees.cpp:71:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |                 for(int j = 0; j < g.size(); j++) {
      |                                ~~^~~~~~~~~~
supertrees.cpp:83:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |                 for(int j = 0; j < g.size(); j++) {
      |                                ~~^~~~~~~~~~
supertrees.cpp:93:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |         for(int i = 0; i < g.size(); i++) {
      |                        ~~^~~~~~~~~~
supertrees.cpp:98:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |         for(int i = 0; i < root.size(); i++) {
      |                        ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 1 ms 212 KB Answer gives possible 0 while actual possible 1
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 1 ms 212 KB Answer gives possible 0 while actual possible 1
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 304 KB Output is correct
2 Correct 1 ms 212 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 -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Answer gives possible 0 while actual possible 1
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 1 ms 212 KB Answer gives possible 0 while actual possible 1
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 1 ms 212 KB Answer gives possible 0 while actual possible 1
4 Halted 0 ms 0 KB -