제출 #536381

#제출 시각아이디문제언어결과실행 시간메모리
536381ddy888Connecting Supertrees (IOI20_supertrees)C++17
19 / 100
218 ms28056 KiB
#undef _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define pb push_back #define fi first #define si second #define ar array typedef pair<int,int> pi; typedef tuple<int,int,int> ti; void debug_out() {cerr<<endl;} template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {cerr<<" "<<to_string(H);debug_out(T...);} #define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__) #include "supertrees.h" int N; int A[1010][1010], branch[1010]; vector<vector<int> > ans; vector<int> adj[1010]; struct dsu { int par[1010]; void init(int x) {for (int i = 0; i <= x; ++i) par[i] = i;} int root(int x) { return (par[x]==x)? x:par[x]=root(par[x]); } bool same(int a, int b) { return root(a) == root(b); } void merge(int a, int b) { // b absorbs a if (same(a, b)) return; par[root(a)] = root(b); } }; void join(int x, int y) { ans[x - 1][y - 1] = 1; ans[y - 1][x - 1] = 1; } int construct(std::vector<std::vector<int>> p) { N = p.size(); ans.resize(N, vector<int>(N)); dsu one, two; one.init(N), two.init(N); for (int i = 1; i <= N; ++i) for (int j = 1; j <= N; ++j) A[i][j] = p[i - 1][j - 1]; for (int i = 1; i <= N; ++i) { for (int j = i + 1; j <= N; ++j) { if (A[i][j] == 3) return 0; if (A[i][j] == 2) two.merge(i, j); if (A[i][j] == 1) { join(one.root(i), one.root(j)); one.merge(one.root(i), one.root(j)); } } } for (int i = 1; i <= N; ++i) branch[i] = (one.root(i) != i); for (int i = 1; i <= N; ++i) adj[two.root(i)].pb(i); for (int i = 1; i <= N; ++i) { if ((int)adj[i].size() < 2) continue; vector<int> nodes; for (auto j: adj[i]) { if (branch[j]) continue; nodes.pb(j); } if ((int)nodes.size() <= 2) return 0; int cnt = 0; for (auto j: nodes) { for (auto k: nodes) { if (j == k) continue; cnt += (A[j][k] == 2); } } int sz = (int)nodes.size(); if (cnt != sz * (sz - 1)) return 0; for (int j = 1; j < (int)nodes.size(); ++j) join(nodes[j], nodes[j - 1]); join(nodes[0], nodes.back()); } for (int i = 1; i <= N; ++i) { for (int j = i + 1; j <= N; ++j) { if (A[i][j] == 0 && (one.same(i, j) || two.same(i, j))) return 0; } } 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...