제출 #1046409

#제출 시각아이디문제언어결과실행 시간메모리
1046409vaneaConnecting Supertrees (IOI20_supertrees)C++14
11 / 100
96 ms22068 KiB
#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
using ll = long long;
using ld = long double;

const int mxN = 1e3+10;

int p[mxN], root[mxN];

vector<int> adj[mxN];

int get(int x) {
    return p[x] < 0 ? x : p[x] = get(p[x]);
}

void uni(int a, int b) {
    a = get(a); b = get(b);
    if(a == b) return ;
    if(p[a] > p[b]) swap(a, b);
    p[a] += p[b];
    p[b] = a;
}

int construct(vector<vector<int>> P) {
    int n = P.size();
    for(int i = 0; i < n; i++) {
        p[i] = -1;
        root[i] = i;
    }
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            if(i == j) continue;
            if(P[i][j] == 1 && (get(i) != get(j))) {
                uni(i, j);
                adj[i].push_back(j);
                adj[j].push_back(i);
            }
        }
    }
    bool pos = true;
    for(int i = 0; i < n; i++) {
        vector<int> v;
        set<int> s;
        for(int j = 0; j < n; j++) {
            if(i == j) continue;
            if(P[i][j] == 2 && (get(i) != get(j))) s.insert(get(j));
        }
        if(s.empty()) continue;
        if(s.size() == 1) {
            pos = false;
            continue;
        }
        for(auto it : s) v.push_back(it);
        for(int i = 0; i + 1 < v.size(); i++) {
            int a = root[get(v[i])], b = root[get(v[i+1])];
            adj[a].push_back(b);
            adj[b].push_back(a);
        }
        int x = root[get(i)];
        adj[x].push_back(root[v[0]]);
        adj[root[v[0]]].push_back(x);
        adj[root[v.back()]].push_back(x);
        adj[x].push_back(root[v.back()]);
        uni(x, v.back());
        for(int i = 0; i + 1 < v.size(); i++) {
            uni(v[i], x);
        }
    }
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            if(!P[i][j] && (get(i) == get(j))) {
                pos = false;
            }
            else {
                if(get(i) != get(j)) pos = false;

            }
        }
    }
    if(!pos) return 0;
    vector<vector<int>> ans(n, vector<int>(n));
    for(int i = 0; i < n; i++) {
        for(auto it : adj[i]) {
            ans[i][it] = ans[it][i] = 1;
        }
    }
    build(ans);
    return 1;
}
/*
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    vector<vector<int>> a = {{1, 2, 2}, {2, 1, 2}, {2, 2, 1}};
    cout << construct(a);
}*/

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

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:55:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |         for(int i = 0; i + 1 < v.size(); i++) {
      |                        ~~~~~~^~~~~~~~~~
supertrees.cpp:66:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |         for(int i = 0; i + 1 < v.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...