답안 #724155

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
724155 2023-04-14T18:39:53 Z badont 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
995 ms 72340 KB
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <queue>
#include <cassert>
#include <cmath>
#include <utility>
#include <numeric>
using namespace std;

template<typename T, typename = void> struct is_iterable : false_type {};
template<typename T> struct is_iterable<T, void_t<decltype(begin(declval<T>())),decltype(end(declval<T>()))>> : true_type {};
template<typename T> typename enable_if<is_iterable<T>::value&&!is_same<T, string>::value,ostream&>::type operator<<(ostream &cout, T const &v);
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename T> typename enable_if<is_iterable<T>::value&&!is_same<T, string>::value,ostream&>::type operator<<(ostream &cout, T const &v) {
	cout << "["; 
	for (auto it = v.begin(); it != v.end();) {
		cout << *it;
		if (++it != v.end()) cout << ", ";
	}
	return cout << "]";
}
 
 
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
 
#ifdef LOCAL
#define debug(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define debug(...) "zzz"
#endif
 
using ll = long long;
using ld = long double;
using pii = pair<ll,ll>;
 
#define FOR(i, n) for(int i = 1; i<=n; i++)
#define F0R(i, n) for(int i = 0; i<n; i++)
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second
 
//var 
ll T;
 
struct DSU {
	int n;
	vector<int> par, siz;
	vector<set<int>> outgoing, incoming, hitting, inside;
  vector<pii> to_onion;
 
	ll running_ans = 0;
 
	// inductively assuming it's up to date already
	// choose smallest outgoing + incoming size?
	// insert all incoming that aint from big boy into big boy
	// change all outgoing to new big boy	
		// delete outgoing if it goes into big boy
 
	DSU (int n) : n(n), par(n), siz(n, 1), outgoing(n), incoming(n), hitting(n), inside(n), running_ans(0) {
		iota(all(par), 0);
    for (int i = 0; i < n; i++)
      inside[i].insert(i);
	};
 
	int fnd(int g) {
		if (g == par[g]) return g;
		return par[g] = fnd(par[g]);
	}	

  void one_way(int a, int b) {
    assert (a == fnd(a) and b == fnd(b));
    outgoing[a].insert(b);
    incoming[b].insert(a);
    
    if (incoming[a].count(b)) {
      to_onion.pb({a, b});
    }
  }
 
	void onion(int a, int b) {
		a = fnd(a), b = fnd(b);
		if (a == b) return;
 
		int sa = (int)outgoing[a].size() + (int)incoming[a].size() + (ll)hitting[a].size() + (ll)inside[a].size();
		int sb = (int)outgoing[b].size() + (int)incoming[b].size() + (ll)hitting[b].size() + (ll)inside[b].size();
 
		if (sa > sb) {
			swap(a, b);
			swap(sa, sb);
		}
 
		auto C2 = [&](ll x) -> ll {
			return ((x) * (x - 1));
		};
 
		running_ans -= (ll)siz[a] * (ll)hitting[a].size();
		running_ans -= (ll)siz[b] * (ll)hitting[b].size();
		running_ans -= C2(siz[a]) + C2(siz[b]);

    for (auto u : hitting[a]) {
      assert(fnd(u) != a);
      if (fnd(u) != b) {
        hitting[b].insert(u);
      }
    }

    for (auto u : inside[a]) {
      inside[b].insert(u);
      hitting[b].erase(u);
    }

		for (auto u : incoming[a])  {
			outgoing[u].erase(a);
			if (fnd(u) != b and fnd(u) != a) {
        one_way(u, b);
			}
		}	
 
		for (auto u : outgoing[a]) {
			incoming[u].erase(a);
			if (fnd(u) != b and fnd(u) != a) {
        one_way(b, u);
			}
		}

    incoming[b].erase(a);
    outgoing[b].erase(a);
    incoming[a].erase(b);
    outgoing[a].erase(b);
    hitting[a].erase(b);
    hitting[b].erase(a);
 
		par[a] = b;
		siz[b] += siz[a];
		running_ans += (ll)siz[b] * (ll)hitting[b].size();
		running_ans += C2(siz[b]);
		incoming[a].clear();
		outgoing[a].clear();
    hitting[a].clear();
    inside[a].clear();
	}
 
	void add_edge(int a, int b) {
		b = fnd(b);
    if (fnd(a) == fnd(b)) return;

    running_ans -= (ll)siz[b] * (ll)hitting[b].size();
    hitting[b].insert(a);
    running_ans += (ll)siz[b] * (ll)hitting[b].size();

    a = fnd(a);
    one_way(a, b);

    while (!to_onion.empty()) {
      auto [x, y] = to_onion.back();
      to_onion.pop_back();
      onion(x, y);
    }
	}
};
 
void solve() {
	int n, m;
	cin >> n >> m;
 
	DSU dsu(n);
	while (m--) {
		ll x, y;
		cin >> x >> y;
		x--; y--;
 
		dsu.add_edge(x, y);
 
    #ifdef LOCAL
    cout << "answer: ";
    #endif
		cout << dsu.running_ans << "\n";
	}
	
}
 
int main() {
	ios_base::sync_with_stdio(0); 
	cin.tie(0);
 
	T = 1;
	//cin >> T;
	FOR(t, T)
		solve();
 
	cout.flush();
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 0 ms 316 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 320 KB Output is correct
15 Correct 1 ms 316 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 320 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 320 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 320 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 0 ms 316 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 320 KB Output is correct
15 Correct 1 ms 316 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 320 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 320 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 320 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 3 ms 468 KB Output is correct
35 Correct 74 ms 6456 KB Output is correct
36 Correct 92 ms 9376 KB Output is correct
37 Correct 92 ms 9548 KB Output is correct
38 Correct 100 ms 9128 KB Output is correct
39 Correct 3 ms 852 KB Output is correct
40 Correct 4 ms 980 KB Output is correct
41 Correct 4 ms 952 KB Output is correct
42 Correct 3 ms 848 KB Output is correct
43 Correct 4 ms 980 KB Output is correct
44 Correct 5 ms 940 KB Output is correct
45 Correct 3 ms 852 KB Output is correct
46 Correct 3 ms 848 KB Output is correct
47 Correct 4 ms 980 KB Output is correct
48 Correct 6 ms 980 KB Output is correct
49 Correct 11 ms 1620 KB Output is correct
50 Correct 115 ms 9544 KB Output is correct
51 Correct 7 ms 1292 KB Output is correct
52 Correct 87 ms 7832 KB Output is correct
53 Correct 11 ms 1624 KB Output is correct
54 Correct 94 ms 8740 KB Output is correct
55 Correct 6 ms 1364 KB Output is correct
56 Correct 7 ms 1356 KB Output is correct
57 Correct 6 ms 1364 KB Output is correct
58 Correct 6 ms 1364 KB Output is correct
59 Correct 4 ms 852 KB Output is correct
60 Correct 74 ms 5796 KB Output is correct
61 Correct 8 ms 992 KB Output is correct
62 Correct 118 ms 8996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 0 ms 316 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 320 KB Output is correct
15 Correct 1 ms 316 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 320 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 320 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 320 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 3 ms 468 KB Output is correct
35 Correct 74 ms 6456 KB Output is correct
36 Correct 92 ms 9376 KB Output is correct
37 Correct 92 ms 9548 KB Output is correct
38 Correct 100 ms 9128 KB Output is correct
39 Correct 3 ms 852 KB Output is correct
40 Correct 4 ms 980 KB Output is correct
41 Correct 4 ms 952 KB Output is correct
42 Correct 3 ms 848 KB Output is correct
43 Correct 4 ms 980 KB Output is correct
44 Correct 5 ms 940 KB Output is correct
45 Correct 3 ms 852 KB Output is correct
46 Correct 3 ms 848 KB Output is correct
47 Correct 4 ms 980 KB Output is correct
48 Correct 6 ms 980 KB Output is correct
49 Correct 11 ms 1620 KB Output is correct
50 Correct 115 ms 9544 KB Output is correct
51 Correct 7 ms 1292 KB Output is correct
52 Correct 87 ms 7832 KB Output is correct
53 Correct 11 ms 1624 KB Output is correct
54 Correct 94 ms 8740 KB Output is correct
55 Correct 6 ms 1364 KB Output is correct
56 Correct 7 ms 1356 KB Output is correct
57 Correct 6 ms 1364 KB Output is correct
58 Correct 6 ms 1364 KB Output is correct
59 Correct 4 ms 852 KB Output is correct
60 Correct 74 ms 5796 KB Output is correct
61 Correct 8 ms 992 KB Output is correct
62 Correct 118 ms 8996 KB Output is correct
63 Correct 508 ms 72340 KB Output is correct
64 Correct 513 ms 72268 KB Output is correct
65 Correct 517 ms 72196 KB Output is correct
66 Correct 200 ms 29128 KB Output is correct
67 Correct 483 ms 35300 KB Output is correct
68 Correct 196 ms 29028 KB Output is correct
69 Correct 466 ms 36012 KB Output is correct
70 Correct 222 ms 29004 KB Output is correct
71 Correct 213 ms 29004 KB Output is correct
72 Correct 526 ms 35404 KB Output is correct
73 Correct 459 ms 35496 KB Output is correct
74 Correct 995 ms 48004 KB Output is correct
75 Correct 648 ms 41804 KB Output is correct
76 Correct 743 ms 46384 KB Output is correct
77 Correct 781 ms 46604 KB Output is correct
78 Correct 237 ms 34736 KB Output is correct
79 Correct 400 ms 38120 KB Output is correct
80 Correct 278 ms 34840 KB Output is correct
81 Correct 425 ms 38172 KB Output is correct
82 Correct 802 ms 56380 KB Output is correct
83 Correct 801 ms 56412 KB Output is correct
84 Correct 692 ms 56268 KB Output is correct
85 Correct 681 ms 56380 KB Output is correct
86 Correct 285 ms 29816 KB Output is correct
87 Correct 319 ms 32032 KB Output is correct
88 Correct 457 ms 35380 KB Output is correct
89 Correct 747 ms 45992 KB Output is correct