Submission #550081

#TimeUsernameProblemLanguageResultExecution timeMemory
550081esomerConnecting Supertrees (IOI20_supertrees)C++17
11 / 100
201 ms22100 KiB
#include "supertrees.h"
#include <bits/stdc++.h>

using namespace std;

struct DSU{
  vector<int> v;
  void init(int n){
    v.assign(n, -1);
  }
  int get(int x){return v[x] < 0 ? x : v[x] = get(v[x]);}

  void unite(int x, int y){
    x = get(x); y = get(y);
    if(x == y) return;
    if(x > y) swap(x, y);
    v[x] += v[y]; v[y] = x;
    return; 
  }
};

DSU UF2;

int construct(vector<vector<int>> p) { //
	int n = p.size();
	vector<bool> vis(n, 0);
	UF2.init(n);
	bool pos = 1;
	vector<vector<int>> ans(n, vector<int>(n, 0));
	for(int i = 0; i < n; i++){
		if(!vis[i]){
			vector<int> three;
			vector<int> two;
			vector<int> one;
			vis[i] = 1;
			for(int j = 0; j < n; j++){
				if(i == j || p[i][j] == 0) continue;
				vis[j] = 1;
				UF2.unite(i, j);
				if(p[i][j] == 3) three.push_back(j);
				else if(p[i][j] == 2) two.push_back(j);
				else one.push_back(j);
			}
			if(three.size() > 0){
				ans[i][three[three.size() - 1]] = ans[i][three[three.size() - 2]] = 1;
				ans[three[three.size() - 1]][i] = ans[three[three.size() - 2]][i] = 1;
				ans[three[0]][i] = ans[i][three[0]] = 1;
				for(int j = 0; j < three.size() - 1; j++){
					ans[three[j]][three[j+1]] = ans[three[j+1]][three[j]] = 1;
				}
			}
			if(two.size() > 0){
				ans[i][two[two.size() - 1]] = ans[two[two.size() - 1]][i] = 1;
				ans[i][two[0]] = ans[two[0]][i] = 1;
				for(int j = 0; j < two.size() - 1; j++){
					ans[two[j]][two[j+1]] = ans[two[j+1]][two[j]] = 1;
				}
			}
			if(one.size() > 0){
				ans[one[0]][i] = ans[i][one[0]] = 1;
				for(int j = 0; j < one.size() - 1; j++){
					ans[one[j]][one[j+1]] = ans[one[j+1]][one[j]] = 1;
				}
			}
      for(int q : two){
        for(int j : two){
          if(q == j) continue;
          if(p[q][j] != 2){
            pos = 0;
            break;
          }
        }
        for(int j : one){
          if(p[q][j] != 2){
            pos = 0;
            break;
          }
        }
      }
      for(int q : one){
        for(int j : one){
          if(q == j) continue;
          if(p[q][j] != 1){
            pos = 0;
            break;
          }
        }
      }
		}
	}
	if(pos){
		build(ans); //
    return 1;
	}else return 0;
}

Compilation message (stderr)

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