답안 #927648

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
927648 2024-02-15T08:12:15 Z haxorman 슈퍼트리 잇기 (IOI20_supertrees) C++14
11 / 100
191 ms 22188 KB
#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;

const int mxN = 1007;

int dsu[mxN];
bool mark[mxN], done[mxN];
vector<int> cmp[mxN];

int find(int x) {
    return dsu[x] < 0 ? x : dsu[x] = find(dsu[x]);
}

bool unite(int x, int y) {
    x = find(x), y = find(y);

    if (x == y) {
        return false;
    }

    if (dsu[x] > dsu[y]) {
        swap(x, y);
    }
    dsu[x] += dsu[y];
    dsu[y] = x;

    return true;
}

int construct(vector<vector<int>> p) {
	int n = p.size();
    memset(dsu, -1, sizeof(dsu));
    vector<vector<int>> ans(n, vector<int>(n));
    
    for (int i = 0; i < n; ++i) {
        cmp[i].push_back(i);
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (p[i][j] == 2) {
                int x = find(i), y = find(j);
                if (x != y) {
                    bool ok = true;
                    for (auto u : cmp[x]) {
                        for (auto v : cmp[y]) {
                            ok &= (p[u][v] == 2);
                        }
                    }
                    
                    if (ok) {
                        if (dsu[x] > dsu[y]) {
                            swap(x, y);
                        }

                        unite(x, y);
                        for (auto c : cmp[y]) {
                            cmp[x].push_back(c);
                        }
                    }
                }
            } 
        }
    }

    for (int i = 0; i < n; ++i) {
        int x = find(i);
        if (done[x] || cmp[x].size() <= 2) {
            continue;
        }
        done[x] = true;
    
        /*
        cout << x << "\n";
        for (auto c : cmp[x]) {
            cout << c << ' ';
        }
        cout << "\n";
        */

        for (int j = 0; j < cmp[x].size() - 1; ++j) {
            mark[cmp[x][j]] = true;
            ans[cmp[x][j]][cmp[x][j+1]] = ans[cmp[x][j+1]][cmp[x][j]] = 1;
        }
        mark[cmp[x].back()] = true;
        ans[cmp[x][0]][cmp[x].back()] = ans[cmp[x].back()][cmp[x][0]] = 1;
    }

    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (i != j && p[i][j] == 1) {
                if (mark[i] && mark[j]) {
                    int x = find(i), y = find(j);

                    if (x == y) {
                        return 0;
                    }
                    
                    bool swp = false;
                    if (dsu[x] > dsu[y]) {
                        swap(x, y);
                        swp = true;
                    }

                    for (auto c : cmp[y]) {
                        if ((swp ? c != i : c != j)) {
                            cmp[x].push_back(c);
                        }
                    }

                    if (swp) {
                        mark[i] = false;
                    }
                    else {
                        mark[j] = false;
                    }
                }
                //assert(!(mark[i] && mark[j]));
                

                if (unite(i, j)) {
                    ans[i][j] = ans[j][i] = 1;
                }
            }
        }
    }

    for (int i = 0; i < n; ++i) {
        int x = find(i);
        if (done[x] || cmp[x].size() <= 2) {
            continue;
        }
        done[x] = true;
    
        /*
        cout << x << "\n";
        for (auto c : cmp[x]) {
            cout << c << ' ';
        }
        cout << "\n";
        */

        for (int j = 0; j < cmp[x].size() - 1; ++j) {
            ans[cmp[x][j]][cmp[x][j+1]] = ans[cmp[x][j+1]][cmp[x][j]] = 1;
        }
        ans[cmp[x][0]][cmp[x].back()] = ans[cmp[x].back()][cmp[x][0]] = 1;
    }
    build(ans);
	return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:82:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |         for (int j = 0; j < cmp[x].size() - 1; ++j) {
      |                         ~~^~~~~~~~~~~~~~~~~~~
supertrees.cpp:144:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  144 |         for (int j = 0; j < cmp[x].size() - 1; ++j) {
      |                         ~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 8 ms 1116 KB Output is correct
7 Correct 155 ms 22100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 8 ms 1116 KB Output is correct
7 Correct 155 ms 22100 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 9 ms 1116 KB Output is correct
13 Correct 153 ms 22072 KB Output is correct
14 Incorrect 0 ms 344 KB Answer gives possible 1 while actual possible 0
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Incorrect 0 ms 348 KB Answer gives possible 1 while actual possible 0
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 43 ms 5872 KB Output is correct
5 Correct 157 ms 22108 KB Output is correct
6 Correct 153 ms 22072 KB Output is correct
7 Correct 162 ms 22100 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 53 ms 5772 KB Output is correct
10 Correct 161 ms 22188 KB Output is correct
11 Correct 159 ms 22060 KB Output is correct
12 Correct 191 ms 22184 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Incorrect 16 ms 3432 KB Answer gives possible 0 while actual possible 1
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 8 ms 1116 KB Output is correct
7 Correct 155 ms 22100 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 9 ms 1116 KB Output is correct
13 Correct 153 ms 22072 KB Output is correct
14 Incorrect 0 ms 344 KB Answer gives possible 1 while actual possible 0
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 8 ms 1116 KB Output is correct
7 Correct 155 ms 22100 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 9 ms 1116 KB Output is correct
13 Correct 153 ms 22072 KB Output is correct
14 Incorrect 0 ms 344 KB Answer gives possible 1 while actual possible 0
15 Halted 0 ms 0 KB -