# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
212767 | 2020-03-24T09:22:09 Z | BThero | Making Friends on Joitter is Fun (JOI20_joitter2) | C++17 | 11 ms | 9728 KB |
#define DBG 1 // chrono::system_clock::now().time_since_epoch().count() #include<bits/stdc++.h> //#include<ext/pb_ds/tree_policy.hpp> //#include<ext/pb_ds/assoc_container.hpp> #define pb push_back #define eb emplace_back #define mp make_pair #define fi first #define se second #define all(x) (x).begin(), (x).end() #define debug(x) if (DBG) cerr << #x << " = " << x << endl; using namespace std; //using namespace __gnu_pbds; typedef long long ll; typedef pair<int, int> pii; //template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const int MAXN = (int)1e5 + 5; const int MAXM = (int)1e6 + 5; set<int> in[MAXN], out[MAXN]; int par[MAXN], sz[MAXN]; pii Q[MAXM]; int n, m, t; ll ans; int getPar(int x) { if (x == par[x]) { return x; } return par[x] = getPar(par[x]); } ll f(int x) { set<int> S; for (int y : in[x]) { S.insert(getPar(y)); } in[x] = S; S.clear(); for (int y : out[x]) { S.insert(getPar(y)); } out[x] = S; return sz[x] * (sz[x] - 1ll) + sz[x] * in[x].size(); } void uni(int a, int b) { a = getPar(a); b = getPar(b); if (a == b) { return; } ans -= f(a); ans -= f(b); out[a].erase(b); in[a].erase(b); out[b].erase(a); in[b].erase(a); for (int x : in[b]) { out[x].erase(b); out[x].insert(a); in[a].insert(x); if (out[a].find(x) != out[a].end()) { Q[t++] = mp(a, x); } } for (int x : out[b]) { in[x].erase(b); in[x].insert(a); out[a].insert(x); if (in[a].find(x) != in[a].end()) { Q[t++] = mp(a, x); } } in[b].clear(); out[b].clear(); par[b] = a; sz[a] += sz[b]; ans += f(a); } void push() { while (1) { int a = -1, b = -1; for (int i = 1; i <= n; ++i) { f(i); for (int x : in[i]) { if (out[i].find(x) != out[i].end()) { a = i; b = x; } } } if (a == -1 && b == -1) { break; } uni(a, b); } //for (int i = 0; i < t; ++i) { //uni(Q[i].fi, Q[i].se); //} } void solve() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; ++i) { par[i] = i; sz[i] = 1; } for (int i = 1; i <= m; ++i) { int u, v; scanf("%d %d", &u, &v); u = getPar(u); v = getPar(v); if (u != v) { out[u].insert(v); ans -= f(v); in[v].insert(u); ans += f(v); if (in[u].find(v) != in[u].end()) { t = 0; Q[t++] = mp(u, v); push(); } } ans = 0; for (int j = 1; j <= n; ++j) { if (par[j] == j) { ans += f(j); } } printf("%lld\n", ans); } } int main() { int tt = 1; while (tt--) { solve(); } return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 11 ms | 9728 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 11 ms | 9728 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 11 ms | 9728 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |