Submission #310820

#TimeUsernameProblemLanguageResultExecution timeMemory
310820ttnhuy313Izlet (COI19_izlet)C++14
100 / 100
1401 ms74740 KiB
#include<bits/stdc++.h> using namespace std; class dsu { public: vector<int> par, ct; dsu(int sz) { ct.resize(sz, 1); par.resize(sz); iota(par.begin(), par.end(), 0); } int get_par(int nd) { return (par[nd] == nd ? nd : get_par(par[nd])); } bool Merge(int a, int b) { a = get_par(a), b = get_par(b); if (a == b) return false; if (ct[a] < ct[b]) swap(a, b); ct[a] += ct[b]; par[b] = a; return true; } }; int main () { ios_base::sync_with_stdio(0); cin.tie(0); int type, n; cin >> type >> n; vector<vector<int>> adj(n, vector<int>(n)); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) cin >> adj[i][j]; dsu graph(n); vector<pair<int, int>> edges; vector<vector<int>> g(n); auto add_edge = [&](int val) { for (int i = 0; i < n; ++i) for (int j = n - 1; j >= 0; --j) { if (adj[i][j] == val && graph.Merge(i, j)) { edges.emplace_back(i, j); g[i].push_back(j); g[j].push_back(i); } } }; add_edge(1); add_edge(2); dsu col(n); for (int i = 0; i < n; ++i) { for (auto ad : g[i]) { function<int(int, int)> dfs = [&](int nd, int par) { int res = -1; if (adj[nd][ad] == adj[nd][i]) { return nd; } for (auto el : g[nd]) { if (el == par) continue; res = max(dfs(el, nd), res); } return res; }; int source = dfs(ad, i); if (source != -1) { col.Merge(source, i); } } } for (int i = 0; i < n; ++i) cout << col.get_par(i) + 1 << ' '; for (auto el : edges) cout << '\n' << el.first + 1 << ' ' << el.second + 1; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...