제출 #431106

#제출 시각아이디문제언어결과실행 시간메모리
4311062qbingxuan조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2)C++17
0 / 100
14 ms19132 KiB
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #ifdef local #define safe cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n" #define pary(a...) danb(#a, a) #define debug(a...) qqbx(#a, a) template <typename ...T> void qqbx(const char *s, T ...a) { int cnt = sizeof...(T); ((std::cerr << "\033[1;32m(" << s << ") = (") , ... , (std::cerr << a << (--cnt ? ", " : ")\033[0m\n"))); } template <typename T> void danb(const char *s, T L, T R) { std::cerr << "\033[1;32m[ " << s << " ] = [ "; for (int f = 0; L != R; ++L) std::cerr << (f++ ? ", " : "") << *L; std::cerr << " ]\033[0m\n"; } #else #define debug(...) ((void)0) #define safe ((void)0) #define pary(...) ((void)0) #endif // local #define all(v) begin(v),end(v) #define pb emplace_back #define sort_uni(v) sort(all(v)),v.erase(unique(all(v)),v.end()) using namespace std; using ll = long long; using ld = long double; template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>; const int mod = 1000000007; const int inf = 1e9; const ll INF = 1e18; const int maxn = 100025; int pa[maxn], sz[maxn]; int anc(int x) { return x==pa[x] ? x : pa[x]=anc(pa[x]); } set<int> in[maxn], cc[maxn]; set<int> s_in[maxn], s_out[maxn]; queue<pair<int,int>> que; ll ans = 0; bool join(int x, int y) { x = anc(x); y = anc(y); if (x == y) return false; if (sz[x] > sz[y]) swap(x, y); ans -= 1LL * sz[x] * (sz[x] - 1) + sz[x] * ll(in[x].size()); ans -= 1LL * sz[y] * (sz[y] - 1) + sz[y] * ll(in[y].size()); pa[x] = y; sz[y] += sz[x]; for (int z: s_in[x]) { if (s_out[y].count(z)) { que.emplace(y, z); } } for (int z: s_out[x]) { if (s_in[y].count(z)) { que.emplace(y, z); } } for (int z: s_out[x]) { s_in[z].erase(x); s_in[z].insert(y); } for (int z: s_in[x]) { s_out[z].erase(x); s_out[z].insert(y); } s_in[y].erase(x); s_out[y].erase(x); for (int z: s_out[x]) if (z != y) s_out[y].insert(z); for (int z: s_in[x]) if (z != y) s_in[y].insert(z); for (int i: cc[x]) in[y].erase(i); for (int i: cc[x]) cc[y].insert(i); for (int i: in[x]) if (!cc[y].count(i)) in[y].insert(i); ans += 1LL * sz[y] * (sz[y] - 1) + sz[y] * ll(in[y].size()); return true; } int ADJ[2025][2025]; signed main() { ios_base::sync_with_stdio(0), cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) pa[i] = i, sz[i] = 1, cc[i].insert(i); vector<pair<int,int>> edges; while (m--) { int a, b; cin >> a >> b; --a, --b; if (s_out[anc(b)].count(anc(a))) { join(a, b); } if (anc(a) != anc(b)) { s_out[anc(a)].insert(anc(b)); s_in[anc(b)].insert(anc(a)); if (!in[anc(b)].count(a)) ans += sz[anc(b)]; in[anc(b)].insert(a); } cout << ans << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...