Submission #603942

#TimeUsernameProblemLanguageResultExecution timeMemory
603942Sam_a17Connecting Supertrees (IOI20_supertrees)C++14
0 / 100
4 ms4948 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define dbg(x) cout << #x << " " << x << endl;
#define uniq(x) x.resize(unique(all(x)) - x.begin());
 
#define pb push_back
#define ld long double
#define ll long long
 
void build(std::vector<std::vector<int>> p);

const int N = 2e5 + 10, M = 1e3 + 10;
vector<int> cycles[N];
int par[N], sz[N], comp[N], c = 1;
int comp2[N];

int find(int a) {
  if(a != par[a]) {
    par[a] = find(par[a]);
  }
  return par[a];
}
 
int same(int a, int b) {
  if(find(a) == find(b)) {
    return 1;
  } else {
    return 0;
  }
}
 
int merge(int a, int b) {
  a = find(a), b = find(b);
  if(a == b) {
    return 0;
  }
 
  if(sz[a] < sz[b]) {
    swap(a, b);
  }
 
  par[b] = a, sz[a] += sz[b];
  return 1;
}

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

  for(int i = 0; i < n; i++) {
    par[i] = i, sz[i] = 1;
    for(int j = 0; j < n; j++) {
      if(p[i][j] == 3) {
        return 0;
      }
    }
  }
  
  vector<bool> vis(n);
  vector<vector<int>> answ(n, vector<int> (n, 0));
  vector<int> cycle;
  for(int i = 0; i < n; i++) {
    if(vis[i]) {
      continue;
    }

    queue<int> q;
    q.push(i); vis[i] = 1;
    vector<int> curr;

    while(!q.empty()) {
      int u = q.front();
      q.pop();
      curr.push_back(u);

      for(int j = 0; j < n; j++) {
        if(!vis[j] && p[u][j] == 1) {
          vis[j] = 1;
          q.push(j);
        }
      }
    }

    int m = sz(curr);
    for(int j = 0; j < m; j++) {
      comp[curr[j]] = c;
    }

    cycle.push_back(curr[0]);
    for(int j = 1; j < m; j++) {
      answ[curr[j - 1]][curr[j]] = 1;
      answ[curr[j]][curr[j - 1]] = 1;
    }

    c++;
  }

  for(int i = 0; i < n; i++) {
    vis[i] = false;
  }

  c = 1;
  for(auto i: cycle) {
    if(vis[i]) continue;
    queue<int> q;
    vector<int> curr;

    q.push(i), vis[i] = true;
    curr.push_back(i);

    //
    while(!q.empty()) {
      auto u = q.front();
      q.pop();

      for(int j = 0; j < n; j++) {
        if(!vis[j] && p[u][j] == 2) {
          vis[j] = 1, curr.push_back(j);
          q.push(j);
        }
      }
    }

    int m = sz(curr);
    for(int j = 0; j < m; j++) {
      answ[curr[j]][curr[(j + 1) % m]] = 1;
      answ[curr[(j + 1) % m]][curr[j]] = 1;
    }
  }

  build(answ);
	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...