Submission #841604

#TimeUsernameProblemLanguageResultExecution timeMemory
841604qthang2k11Connecting Supertrees (IOI20_supertrees)C++17
100 / 100
170 ms26412 KiB
#include "supertrees.h"
#include <bits/stdc++.h>

using namespace std;

const int N = 1003;

vector<int> adj[2][N];

struct DisjointSetUnion {
  vector<int> con[N];
  int par[N];
  
  DisjointSetUnion() {
  	for (int i = 0; i < N; i++)
  	  par[i] = i, con[i] = {i};
  }
  
  inline int find(int x) {
    if (x != par[x]) 
      par[x] = find(par[x]);
    return par[x];
  }
  
  void join(int x, int y) {
    x = find(x); y = find(y);
    if (x == y) return;
    if (con[x].size() < con[y].size()) swap(x, y);
    par[y] = x;
    for (int i: con[y])
      con[x].emplace_back(i);
    con[y].clear();
    con[y].shrink_to_fit();
  }
} one, two, positive;

int construct(vector<vector<int>> p) {
	int n = p.size();
	for (int i = 0; i < n; i++) {
	  for (int j = 0; j < n; j++) {
	    if (p[i][j] != p[j][i] || p[i][j] == 3) return 0;
	    if (!p[i][j]) continue;
	    adj[p[i][j] - 1][i].emplace_back(j);
	    positive.join(i, j);
	  }
	}
	for (int i = 0; i < n; i++)
	  for (int j: adj[0][i])
	    one.join(i, j);
	for (int i = 0; i < n; i++)
	  for (int j: adj[1][i])
	    two.join(one.find(i), one.find(j));
	vector<vector<int>> ans(n, vector<int>(n, 0));
	auto add_edge = [&] (int x, int y) -> void {
	  ans[x][y] = ans[y][x] = 1;
	};
	for (int i = 0; i < n; i++) {
	  if (positive.find(i) == i) {
  	  for (int x: positive.con[i])
  	    for (int y: positive.con[i])
  	      if (p[x][y] == 0)
  	        return 0;
	  }
	  if (one.find(i) == i) {
  	  for (int x: one.con[i])
  	    for (int y: one.con[i])
  	      if (p[x][y] != 1)
  	        return 0;
  	  for (int j = 0, o = one.con[i].size(); j < o - 1; j++)
  	    add_edge(one.con[i][j], one.con[i][j + 1]);
	  }
	  if (two.find(i) == i && (int)two.con[i].size() > 1) {
	    if ((int)two.con[i].size() == 2) return 0;
	    for (int j = 0, o = two.con[i].size(); j < o - 1; j++)
	      add_edge(two.con[i][j], two.con[i][j + 1]);
	    add_edge(two.con[i][0], two.con[i].back());
	  }
	}
	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...