제출 #776753

#제출 시각아이디문제언어결과실행 시간메모리
776753_martynas슈퍼트리 잇기 (IOI20_supertrees)C++14
56 / 100
163 ms24088 KiB
#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.
        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;
}

컴파일 시 표준 에러 (stderr) 메시지

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:60:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |         for(int i = 0; i < g.size(); i++) {
      |                        ~~^~~~~~~~~~
supertrees.cpp:64:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |                 for(int j = 0; j < g.size(); j++) {
      |                                ~~^~~~~~~~~~
supertrees.cpp:76:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |                 for(int j = 0; j < g.size(); j++) {
      |                                ~~^~~~~~~~~~
supertrees.cpp:86:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |         for(int i = 0; i < g.size(); i++) {
      |                        ~~^~~~~~~~~~
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 < root.size(); i++) {
      |                        ~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...