Submission #259745

#TimeUsernameProblemLanguageResultExecution timeMemory
259745amoo_safar조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2)C++14
0 / 100
4 ms5760 KiB
// Zende bad Shoma nasime faghat ! #include <bits/stdc++.h> #define pb push_back #define F first #define S second #define all(x) x.begin(), x.end() #define debug(x) cerr << #x << " : " << x << '\n' using namespace std; typedef long long ll; typedef long double ld; typedef string str; typedef pair<ll, ll> pll; const ll Mod = 1000000007LL; const int N = 1e5 + 10; const ll Inf = 2242545357980376863LL; const ll Log = 30; ll ans; int n, m; map<pll, int> mp; int par[N], sz[N]; set<int> st[N]; vector<pll> U; ll Calc(int com){ ll res = 0; res = 1ll * sz[com] * st[com].size(); res -= sz[com]; return res; } int Find(int u){ if(par[u] == u) return u; return par[u] = Find(par[u]); } void AddE(int u, int v){ ans -= Calc(Find(v)); st[Find(v)].insert(u); ans += Calc(Find(v)); u = Find(u); v = Find(v); mp[{u, v}] = 1; if(mp.count({v, u})){ U.pb({u, v}); } } void Unite(int u, int v){ u = Find(u); v = Find(v); if(u == v) return ; if(st[u].size() > st[v].size()) swap(u, v); ans -= Calc(u); ans -= Calc(v); for(auto x : st[u]){ //AddE(x, v); st[v].insert(x); } for(auto x : st[u]){ if(mp.count({v, Find(x)})){ U.pb({v, Find(x)}); } } st[u].clear(); //st[u].shrink_to_fit(); par[u] = v; sz[v] += sz[u]; ans += Calc(v); } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); iota(par, par + N, 0); fill(sz, sz + N, 1); cin >> n >> m; for(int i = 1; i <= n; i++) st[i].insert(i); int u, v; for(int i = 0; i < m; i++){ cin >> u >> v; AddE(u, v); int f1, f2; while(!U.empty()){ f1 = U.back().F; f2 = U.back().S; U.pop_back(); Unite(f1, f2); //cerr << "# " << f1 << ' ' << f2 << '\n'; } cout << ans << '\n'; } return 0; } /* 4 6 1 2 2 1 3 4 4 3 1 3 4 2 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...