Submission #1201703

#TimeUsernameProblemLanguageResultExecution timeMemory
1201703crispxx철인 이종 경기 (APIO18_duathlon)C++20
10 / 100
1094 ms11232 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define all(x) x.begin(), x.end() #define pb push_back #define ar array #define nl '\n' template <typename A, typename B> bool chmin(A &a, const B &b) { if(a > b) { return a = b, true; } return false; } template <typename A, typename B> bool chmax(A &a, const B &b) { if(a < b) { return a = b, true; } return false; } struct DSU { int n; vector<int> p, sz; DSU(int n) : n(n), p(n), sz(n, 1) { iota(all(p), 0); } int find(int v) { return v == p[v] ? v : p[v] = find(p[v]); } void unite(int u, int v) { u = find(u), v = find(v); if(u == v) return; if(sz[u] < sz[v]) swap(u, v); sz[u] += sz[v]; p[v] = u; } }; void solve() { int n, m; cin >> n >> m; vector<vector<int>> adj(n); vector<pair<int, int>> edges; for(int i = 0; i < m; i++) { int u, v; cin >> u >> v; edges.pb({u, v}); adj[--u].pb(--v); adj[v].pb(u); } auto solve1 = [&]() { int ans = 0; for(int u = 0; u < n; u++) { queue<int> q; vector<int> d(n, -1), sum(n); q.emplace(u); d[u] = 0; while(!q.empty()) { int v = q.front(); q.pop(); ans += sum[v]; for(auto to : adj[v]) { if(d[to] == -1) { d[to] = d[v] + 1; sum[to] += d[v]; q.emplace(to); } } } } return ans; }; auto solve2 = [&]() { int ans = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { DSU d1(n), d2(n); for(auto [u, v] : edges) { if(u != i && v != i) d1.unite(u, v); if(u != j && v != j) d2.unite(u, v); } for(int v = 0; v < n; v++) { if(d1.find(j) == d1.find(v) && d2.find(i) == d2.find(v)) { ans++; } } } } return ans; }; auto print = [&](int x) { cout << x << nl; }; if(n <= 1000) { print(solve1()); return; } print(solve2()); } /** 4 3 1 2 2 3 3 4 4 4 1 2 2 3 3 4 4 2 **/ signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); solve(); }
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...