Submission #1039662

#TimeUsernameProblemLanguageResultExecution timeMemory
1039662AmirAli_H1Connecting Supertrees (IOI20_supertrees)C++17
56 / 100
146 ms27992 KiB
// In the name of Allah #include <bits/stdc++.h> #include "supertrees.h" using namespace std; typedef long long int ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef complex<ld> cld; #define all(x) (x).begin(),(x).end() #define len(x) ((ll) (x).size()) #define F first #define S second #define pb push_back #define sep ' ' #define endl '\n' #define Mp make_pair #define kill(x) cout << x << '\n', exit(0) #define set_dec(x) cout << fixed << setprecision(x); #define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int maxn = 1000 + 4; int n; int adj[maxn][maxn]; int p[maxn], sz[maxn]; vector<pii> E; int col[maxn]; vector<int> ls[maxn], lsx[maxn]; vector<vector<int>> ans; int get(int a) { return (p[a] == a) ? a : p[a] = get(p[a]); } void merge(int a, int b) { a = get(a); b = get(b); if (a == b) return ; if (sz[a] > sz[b]) swap(a, b); p[a] = b; sz[b] += sz[a]; sz[a] = 0; } int construct(vector<vector<int>> Ax) { n = Ax.size(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { adj[i][j] = Ax[i][j]; } } iota(p, p + n, 0); fill(sz, sz + n, 1); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (adj[i][j] >= 1) merge(i, j); } } for (int i = 0; i < n; i++) { col[i] = get(i); for (int j = i + 1; j < n; j++) { if (get(i) == get(j) && adj[i][j] == 0) return 0; } } iota(p, p + n, 0); fill(sz, sz + n, 1); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (adj[i][j] == 1) merge(i, j); } } for (int i = 0; i < n; i++) { ls[get(i)].pb(i); for (int j = i + 1; j < n; j++) { if (get(i) == get(j) && adj[i][j] != 1) return 0; } } for (int i = 0; i < n; i++) { if (len(ls[i]) == 0) continue; for (int j = 1; j < len(ls[i]); j++) { E.pb(Mp(ls[i][j - 1], ls[i][j])); } int v = ls[i].back(); lsx[col[v]].pb(v); } for (int i = 0; i < n; i++) { if (len(lsx[i]) <= 1) continue; for (int j = 0; j < len(lsx[i]); j++) { int r = j + 1; if (r >= len(lsx[i])) r -= len(lsx[i]); E.pb(Mp(lsx[i][j], lsx[i][r])); } } ans.resize(n); for (int i = 0; i < n; i++) ans[i].resize(n); for (auto f : E) { int u = f.F, v = f.S; ans[u][v] = ans[v][u] = 1; } 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...