Submission #603788

#TimeUsernameProblemLanguageResultExecution timeMemory
603788Sam_a17Connecting Supertrees (IOI20_supertrees)C++14
40 / 100
268 ms36424 KiB
// #include "A.cpp" #include <bits/stdc++.h> // #include <vector> 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; int par[N], sz[N], arden[N]; set<int> st[N]; bitset<M> biti[M]; vector<int> adj[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]; for(auto i: st[b]) { st[a].insert(i); } st[b].clear(); return 1; } int construct(std::vector<std::vector<int>> p) { int n = p.size(); bool allOne = true, lowerOne = true, zeroOrtwo = true; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(p[i][j] == 3) { return 0; } if(p[i][j] != 1) { allOne = false; } if(p[i][j] > 1) { lowerOne = false; } if(i == j) continue; if(p[i][j] == 1 || p[i][j] == 3) { zeroOrtwo = false; } } } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(p[i][j] != p[j][i]) { return 0; } } } if(allOne) { vector<vector<int>> answ(n, vector<int> (n, 0)); for(int i = 0; i < n - 1; i++){ answ[i][i + 1] = 1; answ[i + 1][i] = 1; } build(answ); return 1; } if(lowerOne) { vector<vector<int>> answ(n, vector<int> (n, 0)); for(int i = 0; i < n; i++) { par[i] = i, sz[i] = 1; } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(p[i][j] == 1) { if(merge(i, j)) { answ[i][j] = answ[j][i] = 1; } } } } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(same(i, j) != p[i][j]) { return 0; } } } build(answ); return 1; } if(zeroOrtwo){ vector<vector<int>> answ(n, vector<int> (n, 0)); for(int i = 0; i < n; i++) { par[i] = i, sz[i] = 1; st[i].insert(i); } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(p[i][j]) biti[i][j] = 1; } } // vector<int> out(n, 0); vector<int> ans, vis(n); vector<vector<int>> components; for(int i = 0; i < n; i++) { if(vis[i]) continue; // dbg(i) for(int j = 0; j < n; j++) { if(p[i][j] == 2) { // dbg(j) merge(i, j); } } int pa = find(i); // const int x = n; bitset<M> bt; for(auto j: st[pa]) { bt[j] = 1; } // cout << bt << endl; for(auto j: st[pa]) { if((biti[j] | bt) != bt || (biti[j] & bt) != bt) { return 0; } } if(sz(st[pa]) == 2) { return 0; } vector<int> comp; for(auto j: st[pa]) { vis[j] = true; comp.push_back(j); } components.push_back(comp); } for(auto i: components) { for(int j = 1; j < sz(i); j++) { answ[i[j]][i[j - 1]] = 1; answ[i[j - 1]][i[j]] = 1; } answ[i.back()][i.front()] = 1; answ[i.front()][i.back()] = 1; } // if(n > 7) { // answ[4][4] = 0; // answ[7][7] = 0; // } for(int i = 0; i < n; i++) { answ[i][i] = 0; } build(answ); return 1; } vector<vector<int>> answ(n, vector<int> (n, 0)); vector<int> cont; { vector<int> vis(n); for(int i = 0; i < n; i++) { if(vis[i]) continue; vis[i] = 1; vector<int> merged{i}; for(int j = 0; j < n; j++) { if(!vis[j] && i != j && p[i][j] == 1) { answ[i][j] = answ[j][i] = 1; vis[j] = 1; merged.push_back(j); } } if(sz(merged) >= 2) { int back = merged.back(); cont.push_back(back); arden[back] = 1; merged.pop_back(); for(auto j: merged) { arden[j] = 1; adj[back].push_back(j); } } } } // gtnenq ciklery merge anenq irar // taky mnac gcery { vector<bool> vis(n); for(auto i: cont) { if(vis[i]) continue; vis[i] = true; int lst = i; for(int j = 0; j < n; j++) { if(p[i][j] == 2) { vis[j] = true; answ[lst][j] = answ[j][lst] = 1; lst = j; } } } } for(int i = 0; i < n; i++) { if(adj[i].empty()) continue; for(auto j: adj[i]) { answ[i][j] = answ[j][i] = 1; } } for(int i = 0; i < n; i++) { answ[i][i] = 0; } 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...