제출 #300780

#제출 시각아이디문제언어결과실행 시간메모리
300780Sorting슈퍼트리 잇기 (IOI20_supertrees)C++17
100 / 100
275 ms22136 KiB
#include "supertrees.h"
#include <bits/stdc++.h>

using namespace std;

const int k_N = 1000 + 3;

struct DSU{
    int par[k_N], sz[k_N];

    DSU(){
        for(int i = 0; i < k_N; ++i){
            par[i] = i;
            sz[i] = 1;
        }
    }

    int get_par(int x){
        if(par[x] == x) return x;
        return par[x] = get_par(par[x]);
    }

    bool unite(int x, int y){
        if(get_par(x) == get_par(y))
            return false;

        if(sz[par[x]] < sz[par[y]])
            swap(x, y);

        par[par[y]] = par[x];
        sz[par[x]] += sz[par[y]];

        return true;
    }
};

int construct(vector<vector<int>> p) {
	int n = p.size();
	vector<vector<int>> answer(n, vector<int>(n, 0));

    DSU dsu;

    for(int i = 0; i < n; ++i)
        for(int j = i + 1; j < n; ++j)
            if(p[i][j] == 3)
                return 0;

    for(int i = 0; i < n; ++i)
        for(int j = i + 1; j < n; ++j)
            if(p[i][j] == 1)
                dsu.unite(i, j);

    for(int i = 0; i < n; ++i)
        for(int j = i + 1; j < n; ++j)
            if(dsu.get_par(i) == dsu.get_par(j) && p[i][j] != 1)
                return 0;

    vector<int> roots;
    for(int i = 0; i < n; ++i){
        if(dsu.par[i] != i){
            answer[i][dsu.par[i]] = 1;
            answer[dsu.par[i]][i] = 1;
        }
        else roots.push_back(i);
    }

    //cout << "roots: ";
    //for(int root: roots)
    //    cout << root << " ";
    //cout << endl;

    for(int i = 0; i < n; ++i)
        for(int j = i + 1; j < n; ++j)
            if(p[i][j] == 2)
                dsu.unite(i, j);

    for(int i = 0; i < n; ++i)
        for(int j = i + 1; j < n; ++j)
            if(dsu.get_par(i) == dsu.get_par(j) && !p[i][j])
                return 0;

    vector<bool> vis(roots.size(), false);
    for(int i = 0; i < roots.size(); ++i){
        if(vis[i]) continue;
        int x = roots[i];

        int curr = x, cnt = 1;
        for(int j = i + 1; j < roots.size(); ++j){
            if(vis[j]) continue;
            int y = roots[j];

            if(dsu.get_par(x) == dsu.get_par(y)){
                vis[j] = true;
                answer[curr][y] = 1;
                answer[y][curr] = 1;

                curr = y;
                cnt++;
            }
        }

        if(cnt == 2) return 0;

        if(cnt == 1) continue;
        answer[curr][x] = 1;
        answer[x][curr] = 1;
    }

    build(answer);

	return 1;
}

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

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