제출 #628361

#제출 시각아이디문제언어결과실행 시간메모리
628361boris_mihov슈퍼트리 잇기 (IOI20_supertrees)C++17
40 / 100
225 ms30024 KiB
#include "supertrees.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#include <stack>

typedef long long llong;
const int MAXN = 1000 + 10;

int n;
int p[MAXN];
int d[MAXN];
std::vector < std::vector <int> > ans;
std::vector <int> byPaths[MAXN][3];

inline int find(int x)
{
    if (p[x] == x) return x;
    return p[x] = find(p[x]);
}

inline void connect(int x, int y)
{
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (d[x] > d[y]) std::swap(x, y);
    if (d[x] == d[y]) ++d[y];
    p[x] = y;
}

inline bool areConnected(int x, int y)
{
    return find(x) == find(y);
}

int cycle[MAXN], cycleCnt;
int construct(std::vector < std::vector <int> > wantedPaths) 
{
    n = wantedPaths.size();
    ans.resize(n);

    for (int i = 1 ; i <= n ; ++i)
    {
        ans[i-1].resize(n, 0);
        for (int j = 1 ; j <= n ; ++j)
        {
            if (i == j) continue;
            if (wantedPaths[i - 1][j - 1] == 3) return 0;
            byPaths[i][wantedPaths[i - 1][j - 1]].push_back(j);
        }
    }

    std::iota(p + 1, p + 1 + n, 1);
    std::fill(d + 1, d + 1 + n, 1);
    for (int i = 1 ; i <= n ; ++i)
    {
        int last = i;
        int alreadyConnected = 0;
        if (byPaths[i][2].size() == 1) return 0;
        for (const int &j : byPaths[i][2])
        {
            if (areConnected(i, j)) 
            {
                if (alreadyConnected == 1) return 0;
                cycle[i] = cycle[j];
                alreadyConnected = 2;
                continue;
            }

            if (alreadyConnected == 2 || cycle[j] != 0) return 0;
            if (cycle[i] == 0) cycle[i] = ++cycleCnt;
            ans[last - 1][j - 1] = 1;
            ans[j - 1][last - 1] = 1;
            alreadyConnected = 1;
            cycle[j] = cycle[i];
            connect(i, j);
            last = j;
        }

        if (last != i)
        {
            ans[last - 1][i - 1] = 1;
            ans[i - 1][last - 1] = 1;
        }

        for (const int &j : byPaths[i][1])
        {
            if (areConnected(i, j)) continue;
            ans[i - 1][j - 1] = 1;
            ans[j - 1][i - 1] = 1;
            connect(i, j);
        }
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        for (int j = 1 ; j <= n ; ++j)
        {
            if (i == j) continue;
            if (cycle[i] != cycle[j] && cycle[i] != 0 && cycle[j] != 0 && areConnected(i, j)) return 0;
            if (wantedPaths[i - 1][j - 1] == 0 && areConnected(i, j)) return 0;
            if (wantedPaths[i - 1][j - 1] == 1 && (!areConnected(i, j) || !(cycle[i] == 0 && cycle[j] == 0))) return 0;
            if (wantedPaths[i - 1][j - 1] == 2 && (!areConnected(i, j) || (cycle[i] == 0 && cycle[j] == 0))) return 0;
        }
    }

    build(ans);
	return 1;
}
#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...