Submission #642939

#TimeUsernameProblemLanguageResultExecution timeMemory
642939_petar_bConnecting Supertrees (IOI20_supertrees)C++14
0 / 100
1101 ms2097152 KiB
#include "supertrees.h"
#include <bits/stdc++.h>
#define MAXN 1010

using namespace std;

int graph[MAXN], graph_count = 1, krak[MAXN];

int dfs(int node, vector<vector<int>> p)
{
    graph[node] = graph_count;
    int r = 0;
    for (int i = 0; i < p.size(); i++)
    {
        if (p[node][i] == 0 && graph[i] == graph[node])
            return 1;
        else if (graph[i] == graph[node])
            continue;
        else if (p[node][i])
        {
            if ((graph[i] != 0 && graph[i] != graph[node]) || (p[node][i] == 3))
                return 1;
            if (graph[i] == 0)
                r = dfs(i, p);
            if (r == 1)
                return 1;
        }
    }
    return r;
}

int construct(std::vector<std::vector<int>> p) {
	int n = p.size();
	std::vector<std::vector<int>> answer;
	for (int i = 0; i < n; i++) {
		std::vector<int> row;
		row.resize(n, 0);
		answer.push_back(row);
	}
	//
	for (int i = 0; i < n; i++) if (graph[i] == 0)
    {
        int r = dfs(i, p);
        if (r) return 0;
        graph_count++;
    }
    //
    vector<vector<int>> kraci;
    vector<int> empt; kraci.push_back(empt);
    vector<vector<int>> ciklusi;
    ciklusi.resize(graph_count);
    for (int i = 0 ; i < n; i++)
    {
        bool all2 = true, all0 = true;
        for (int j = 0; j < n; j++) if (j != i && graph[i] == graph[j])
        {
            if (p[i][j] == 2) all0 = false;
            if (p[i][j] == 1)
            {
                all2 = false;
                all0 = false;
                if (krak[i] == 0)
                {
                    krak[i] = krak[j];
                }
                else if (krak[j] != 0 && krak[i] != krak[j])
                    return 0;
            }
        }
        if (all0)
            ;
        else if (all2)
        {
            ciklusi[graph[i]].push_back(i);
        }
        else
        {
            if (krak[i] == 0)
            {
                kraci.push_back(empt);
                kraci[kraci.size()-1].push_back(i);
                krak[i] = kraci.size() - 1;
                ciklusi[graph[i]].push_back(i);
            }
            else
                kraci[krak[i]].push_back(i);
        }
    }
    //povezivanje ciklusa
    for (int i = 1; i < graph_count; i++) if (!ciklusi[i].empty())
    {
        if (ciklusi[i].size() == 1)
            ;
        else if (ciklusi[i].size() == 2)
            return 0;
        else
        {
            int last = ciklusi[i][0];
            for (int j = 1; j < ciklusi[i].size(); j++)
            {
                answer[last][ciklusi[i][j]] = 1;
                answer[ciklusi[i][j]][last] = 1;
                last = ciklusi[i][j];
            }
            answer[ciklusi[i][0]][last] = 1;
            answer[last][ciklusi[i][0]] = 1;
        }
    }
    //povezivanje krakova
    for (int i = 1; i < kraci.size(); i++)
    {
        for (int j = 1; j < kraci[i].size(); j++)
        {
            answer[kraci[i][j-1]][kraci[i][j]] = 1;
            answer[kraci[i][j]][kraci[i][j-1]] = 1;
        }
    }
	build(answer);
	return 1;
}

Compilation message (stderr)

supertrees.cpp: In function 'int dfs(int, std::vector<std::vector<int> >)':
supertrees.cpp:13:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 |     for (int i = 0; i < p.size(); i++)
      |                     ~~^~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:99:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |             for (int j = 1; j < ciklusi[i].size(); j++)
      |                             ~~^~~~~~~~~~~~~~~~~~~
supertrees.cpp:110:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |     for (int i = 1; i < kraci.size(); i++)
      |                     ~~^~~~~~~~~~~~~~
supertrees.cpp:112:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |         for (int j = 1; j < kraci[i].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...