제출 #329515

#제출 시각아이디문제언어결과실행 시간메모리
329515blue슈퍼트리 잇기 (IOI20_supertrees)C++17
56 / 100
260 ms24172 KiB
#include "supertrees.h"
#include <vector>
using namespace std;

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

    vector<int> chain(n, -1), ring(n, -1);
    for(int i = 0; i < n; i++) chain[i] = ring[i] = i;

    for(int j = 0; j < n; j++)
    {
        for(int i = 0; i < j; i++)
        {
            if(p[i][j] >= 1)
            {
                ring[j] = ring[ring[chain[i]]];
                break;
            }
        }
        for(int i = 0; i < j; i++)
        {
            if(p[i][j] == 1)
            {
                chain[j] = chain[chain[i]];
                break;
            }
        }
    }
    int x;
    for(int i = 0; i < n; i++)
    {
        for(x = i; chain[x] != x; x = chain[x]);
        chain[i] = x;
        
        for(; ring[x] != x; x = ring[x]);
        ring[i] = x;
    }

    for(int i = 0; i < n; i++)
    {
        for(int j = i+1; j < n; j++)
        {
            if(p[i][j] == 0)
            {
                if(chain[i] == chain[j]) return 0;
                if(ring[i] == ring[j]) return 0;
            }
            else if(p[i][j] == 1)
            {
                if(chain[i] != chain[j]) return 0;
                if(ring[i] != ring[j]) return 0;
            }
            else if(p[i][j] == 2)
            {
                if(chain[i] == chain[j]) return 0;
                if(ring[i] != ring[j]) return 0;
            }
            else if(p[i][j] == 3)
            {
                return 0;
            }

            if(ring[i] == ring[j])
            {
                if(p[i][j] == 0) return 0;
            }
            else
            {
                if(p[i][j] != 0) return 0;
            }

            if(chain[i] == chain[j])
            {
                if(p[i][j] != 1) return 0;
            }
            else
            {
                if(p[i][j] == 1) return 0;
            }
        }
    }

    vector< vector<int> > b (n, vector<int>(n, 0));

    vector<int> oncycle(n, 1);

    for(int i = 0; i < n; i++)
    {
        for(int j = i-1; j >= 0; j--)
            if(p[i][j] == 1)
            {
                oncycle[i] = 0;
                b[i][j] = b[j][i] = 1;
                break;
            }
    }
    int pre;
    for(int i = 0; i < n; i++)
    {
        if(!oncycle[i]) continue;
        pre = i;
        oncycle[i] = 0;
        for(int j = i+1; j < n; j++)
        {
            if(!oncycle[j]) continue;
            if(p[pre][j] == 2)
            {
                b[pre][j] = b[j][pre] = 1;
                pre = j;
                oncycle[j] = 0;
            }
        }
        if(pre != i) b[pre][i] = b[i][pre] = 1;
    }
    build(b);
    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...