Submission #402530

#TimeUsernameProblemLanguageResultExecution timeMemory
402530JerryLiu06Making Friends on Joitter is Fun (JOI20_joitter2)C++17
100 / 100
1298 ms95732 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using db = long double; using str = string; using pi = pair<int, int>; using pl = pair<ll, ll>; using pd = pair<db, db>; using vi = vector<int>; using vb = vector<bool>; using vl = vector<ll>; using vd = vector<db>; using vs = vector<str>; using vpi = vector<pi>; using vpl = vector<pl>; using vpd = vector<pd>; #define mp make_pair #define f first #define s second #define sz(x) (int)(x).size() #define bg(x) begin(x) #define all(x) bg(x), end(x) #define sor(x) sort(all(x)) #define ft front() #define bk back() #define pb push_back #define pf push_front #define lb lower_bound #define ub upper_bound #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define F0R(i, a) FOR(i, 0, a) #define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--) #define R0F(i, a) ROF(i, 0, a) #define EACH(a, x) for (auto& a : x) const int MOD = 1e9 + 7; const int MX = 1e5 + 10; const ll INF = 1e18; int N, M; ll ans; int par[MX], sz[MX]; queue<pi> todo; unordered_set<int> ext[MX], adj[MX], radj[MX]; int dsu_sz(int A) { return sz(ext[A]) + sz(adj[A]) + sz(radj[A]); } int find(int A) { return (par[A] == A ? A : (par[A] = find(par[A]))); } void connect(int A, int B) { adj[A].insert(B); radj[B].insert(A); if (adj[B].count(A)) todo.push({A, B}); } void onion(int A, int B) { if (A == B) return ; if (dsu_sz(A) < dsu_sz(B)) swap(A, B); ans += 1LL * sz[B] * sz(ext[A]) + 1LL * sz[A] * sz(ext[B]); par[B] = A; sz[A] += sz[B]; EACH(C, ext[B]) { if (ext[A].count(C)) ans -= sz[A]; else ext[A].insert(C); } adj[A].erase(B); adj[B].erase(A); radj[A].erase(B); radj[B].erase(A); EACH(C, adj[B]) { radj[C].erase(B); connect(A, C); } EACH(C, radj[B]) { adj[C].erase(B); connect(C, A); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> N >> M; FOR(i, 1, N + 1) par[i] = i, sz[i] = 1, ext[i].insert(i); F0R(i, M) { int A, B; cin >> A >> B; B = find(B); if (find(A) != B && !ext[B].count(A)) { ext[B].insert(A); ans += sz[B]; A = find(A); connect(A, B); while (!todo.empty()) { pi cur = todo.front(); todo.pop(); onion(find(cur.f), find(cur.s)); } } cout << ans << "\n"; } }

Compilation message (stderr)

joitter2.cpp: In function 'void onion(int, int)':
joitter2.cpp:62:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   62 |     if (A == B) return ; if (dsu_sz(A) < dsu_sz(B)) swap(A, B);
      |     ^~
joitter2.cpp:62:26: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   62 |     if (A == B) return ; if (dsu_sz(A) < dsu_sz(B)) swap(A, B);
      |                          ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...