답안 #597744

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
597744 2022-07-16T19:02:20 Z penguinhacker 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
1137 ms 86788 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ar array

#define dbg(x) cerr << "[" << #x << "] = [" << x << "]\n"

template<class T> ostream& operator<< (ostream& out, vector<T> v) {
	out << '[';
	for (int i = 0; i < v.size(); ++i) {
		if (i > 0) {
			out << ", ";
		}
		out << v[i];
	}
	return out << ']';
}

template<class T> ostream& operator<< (ostream& out, set<T> v) {
	return out << vector<T>(v.begin(), v.end());
}

template<class A, size_t sz> ostream& operator<< (ostream& out, ar<A, sz> a) {
	out << '[';
	for (int i = 0; i < sz; ++i) {
		if (i > 0) out << ", ";
		out << a[i];
	}
	return out << ']';
}

template<class A, class B> ostream& operator<< (ostream& out, pair<A, B> p) {
	return out << '[' << p.first << ", " << p.second << ']';
}

template<class A, class B> ostream& operator<< (ostream& out, map<A, B> mp) {
	return out << vector<pair<A, B>>(mp.begin(), mp.end());
}

const int mxN=1e5;
int n, m, p[mxN];
set<int> adj[mxN][2], to[mxN], back[mxN]; // forward and backward edges to other components, from i->component, component->i
vector<int> cmp[mxN];
queue<ar<int, 2>> q;
ll ans;

int find(int i) {
	return i^p[i]?p[i]=find(p[i]):i;
}

ll calc(int i) {
	assert(i==p[i]);
	ll x=cmp[i].size();
	return x*(x-1)+(ll)back[i].size()*x;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m;
	for (int i=0; i<n; ++i) {
		p[i]=i;
		cmp[i]={i};
	}
	while(m--) {
		int u, v;
		cin >> u >> v, --u, --v;
		if (find(u)==find(v)) {
			cout << ans << "\n";
			continue;
		}
		if (to[u].find(find(v))==to[u].end()) {
			ans+=cmp[find(v)].size();
			to[u].insert(find(v));
			back[find(v)].insert(u);
		}
		u=find(u), v=find(v);
		if (adj[u][0].find(v)==adj[u][0].end()) {
			adj[u][0].insert(v);
			adj[v][1].insert(u);
			if (adj[u][1].find(v)!=adj[u][1].end())
				q.push({u, v});
		}
		for (; q.size(); q.pop()) {
			u=find(q.front()[0]), v=find(q.front()[1]);
			//cout << "trying to merge " << u << " " << v << endl;
			//dbg(to[u]), dbg(to[v]), dbg(back[u]), dbg(back[v]);
			if (u==v)
				continue;
			if (cmp[u].size()<cmp[v].size())
				swap(u, v);
			ans-=calc(u)+calc(v);
			for (int j : {0, 1}) {
				for (int rep=0; rep<2; ++rep) {
					auto it=adj[u][j].find(v);
					assert(it!=adj[u][j].end());
					adj[u][j].erase(it);
					swap(u, v);
				}
			}
			for (int i : cmp[v]) {
				if (to[i].find(u)!=to[i].end()) {
					to[i].erase(u);
					back[u].erase(i);
				}
				cmp[u].push_back(i);
			}
			vector<int>().swap(cmp[v]);
			for (int i : back[v]) {
				auto it=to[i].find(v);
				assert(it!=to[i].end());
				to[i].erase(it);
				if (find(i)!=u) {
					to[i].insert(u);
					back[u].insert(i);
				}
			}
			set<int>().swap(back[v]);
			//back[u].erase(v);
			//cout << adj[v][0].size() << " " << adj[v][1].size() << " " << adj[0][0].size() << endl;
			for (int j : {0, 1}) {
				for (int i : adj[v][j]) {
					//cout << j << " " << v << " " << i << endl;
					adj[u][j].insert(i);
					auto it=adj[i][j^1].find(v);
					assert(it!=adj[i][j^1].end());
					adj[i][j^1].erase(it);
					adj[i][j^1].insert(u);
					if (adj[u][j^1].find(i)!=adj[u][j^1].end())
						q.push({u, i});
				}
				set<int>().swap(adj[v][j]);
			}
			//dbg(to[u]), dbg(to[v]), dbg(back[u]), dbg(back[v]);
			ans+=calc(u);
			p[v]=u;
		}
		cout << ans << "\n";
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 21368 KB Output is correct
2 Correct 11 ms 21460 KB Output is correct
3 Correct 14 ms 21464 KB Output is correct
4 Correct 11 ms 21472 KB Output is correct
5 Correct 11 ms 21372 KB Output is correct
6 Correct 12 ms 21440 KB Output is correct
7 Correct 12 ms 21460 KB Output is correct
8 Correct 14 ms 21460 KB Output is correct
9 Correct 13 ms 21520 KB Output is correct
10 Correct 11 ms 21464 KB Output is correct
11 Correct 10 ms 21460 KB Output is correct
12 Correct 12 ms 21464 KB Output is correct
13 Correct 11 ms 21460 KB Output is correct
14 Correct 11 ms 21460 KB Output is correct
15 Correct 14 ms 21460 KB Output is correct
16 Correct 11 ms 21452 KB Output is correct
17 Correct 12 ms 21380 KB Output is correct
18 Correct 11 ms 21460 KB Output is correct
19 Correct 11 ms 21460 KB Output is correct
20 Correct 13 ms 21460 KB Output is correct
21 Correct 11 ms 21504 KB Output is correct
22 Correct 10 ms 21460 KB Output is correct
23 Correct 11 ms 21468 KB Output is correct
24 Correct 11 ms 21464 KB Output is correct
25 Correct 11 ms 21388 KB Output is correct
26 Correct 11 ms 21460 KB Output is correct
27 Correct 11 ms 21464 KB Output is correct
28 Correct 14 ms 21460 KB Output is correct
29 Correct 11 ms 21460 KB Output is correct
30 Correct 11 ms 21468 KB Output is correct
31 Correct 13 ms 21448 KB Output is correct
32 Correct 10 ms 21460 KB Output is correct
33 Correct 11 ms 21408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 21368 KB Output is correct
2 Correct 11 ms 21460 KB Output is correct
3 Correct 14 ms 21464 KB Output is correct
4 Correct 11 ms 21472 KB Output is correct
5 Correct 11 ms 21372 KB Output is correct
6 Correct 12 ms 21440 KB Output is correct
7 Correct 12 ms 21460 KB Output is correct
8 Correct 14 ms 21460 KB Output is correct
9 Correct 13 ms 21520 KB Output is correct
10 Correct 11 ms 21464 KB Output is correct
11 Correct 10 ms 21460 KB Output is correct
12 Correct 12 ms 21464 KB Output is correct
13 Correct 11 ms 21460 KB Output is correct
14 Correct 11 ms 21460 KB Output is correct
15 Correct 14 ms 21460 KB Output is correct
16 Correct 11 ms 21452 KB Output is correct
17 Correct 12 ms 21380 KB Output is correct
18 Correct 11 ms 21460 KB Output is correct
19 Correct 11 ms 21460 KB Output is correct
20 Correct 13 ms 21460 KB Output is correct
21 Correct 11 ms 21504 KB Output is correct
22 Correct 10 ms 21460 KB Output is correct
23 Correct 11 ms 21468 KB Output is correct
24 Correct 11 ms 21464 KB Output is correct
25 Correct 11 ms 21388 KB Output is correct
26 Correct 11 ms 21460 KB Output is correct
27 Correct 11 ms 21464 KB Output is correct
28 Correct 14 ms 21460 KB Output is correct
29 Correct 11 ms 21460 KB Output is correct
30 Correct 11 ms 21468 KB Output is correct
31 Correct 13 ms 21448 KB Output is correct
32 Correct 10 ms 21460 KB Output is correct
33 Correct 11 ms 21408 KB Output is correct
34 Correct 16 ms 21716 KB Output is correct
35 Correct 97 ms 27592 KB Output is correct
36 Correct 112 ms 30964 KB Output is correct
37 Correct 109 ms 30988 KB Output is correct
38 Correct 109 ms 30580 KB Output is correct
39 Correct 14 ms 21604 KB Output is correct
40 Correct 17 ms 21728 KB Output is correct
41 Correct 16 ms 21700 KB Output is correct
42 Correct 17 ms 21584 KB Output is correct
43 Correct 19 ms 21716 KB Output is correct
44 Correct 15 ms 21700 KB Output is correct
45 Correct 13 ms 21604 KB Output is correct
46 Correct 18 ms 21588 KB Output is correct
47 Correct 15 ms 21716 KB Output is correct
48 Correct 18 ms 21784 KB Output is correct
49 Correct 23 ms 22564 KB Output is correct
50 Correct 118 ms 31100 KB Output is correct
51 Correct 20 ms 22100 KB Output is correct
52 Correct 102 ms 29024 KB Output is correct
53 Correct 23 ms 22476 KB Output is correct
54 Correct 102 ms 30044 KB Output is correct
55 Correct 21 ms 22336 KB Output is correct
56 Correct 18 ms 22312 KB Output is correct
57 Correct 17 ms 22296 KB Output is correct
58 Correct 22 ms 22240 KB Output is correct
59 Correct 16 ms 21608 KB Output is correct
60 Correct 108 ms 26592 KB Output is correct
61 Correct 16 ms 21852 KB Output is correct
62 Correct 113 ms 30344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 21368 KB Output is correct
2 Correct 11 ms 21460 KB Output is correct
3 Correct 14 ms 21464 KB Output is correct
4 Correct 11 ms 21472 KB Output is correct
5 Correct 11 ms 21372 KB Output is correct
6 Correct 12 ms 21440 KB Output is correct
7 Correct 12 ms 21460 KB Output is correct
8 Correct 14 ms 21460 KB Output is correct
9 Correct 13 ms 21520 KB Output is correct
10 Correct 11 ms 21464 KB Output is correct
11 Correct 10 ms 21460 KB Output is correct
12 Correct 12 ms 21464 KB Output is correct
13 Correct 11 ms 21460 KB Output is correct
14 Correct 11 ms 21460 KB Output is correct
15 Correct 14 ms 21460 KB Output is correct
16 Correct 11 ms 21452 KB Output is correct
17 Correct 12 ms 21380 KB Output is correct
18 Correct 11 ms 21460 KB Output is correct
19 Correct 11 ms 21460 KB Output is correct
20 Correct 13 ms 21460 KB Output is correct
21 Correct 11 ms 21504 KB Output is correct
22 Correct 10 ms 21460 KB Output is correct
23 Correct 11 ms 21468 KB Output is correct
24 Correct 11 ms 21464 KB Output is correct
25 Correct 11 ms 21388 KB Output is correct
26 Correct 11 ms 21460 KB Output is correct
27 Correct 11 ms 21464 KB Output is correct
28 Correct 14 ms 21460 KB Output is correct
29 Correct 11 ms 21460 KB Output is correct
30 Correct 11 ms 21468 KB Output is correct
31 Correct 13 ms 21448 KB Output is correct
32 Correct 10 ms 21460 KB Output is correct
33 Correct 11 ms 21408 KB Output is correct
34 Correct 16 ms 21716 KB Output is correct
35 Correct 97 ms 27592 KB Output is correct
36 Correct 112 ms 30964 KB Output is correct
37 Correct 109 ms 30988 KB Output is correct
38 Correct 109 ms 30580 KB Output is correct
39 Correct 14 ms 21604 KB Output is correct
40 Correct 17 ms 21728 KB Output is correct
41 Correct 16 ms 21700 KB Output is correct
42 Correct 17 ms 21584 KB Output is correct
43 Correct 19 ms 21716 KB Output is correct
44 Correct 15 ms 21700 KB Output is correct
45 Correct 13 ms 21604 KB Output is correct
46 Correct 18 ms 21588 KB Output is correct
47 Correct 15 ms 21716 KB Output is correct
48 Correct 18 ms 21784 KB Output is correct
49 Correct 23 ms 22564 KB Output is correct
50 Correct 118 ms 31100 KB Output is correct
51 Correct 20 ms 22100 KB Output is correct
52 Correct 102 ms 29024 KB Output is correct
53 Correct 23 ms 22476 KB Output is correct
54 Correct 102 ms 30044 KB Output is correct
55 Correct 21 ms 22336 KB Output is correct
56 Correct 18 ms 22312 KB Output is correct
57 Correct 17 ms 22296 KB Output is correct
58 Correct 22 ms 22240 KB Output is correct
59 Correct 16 ms 21608 KB Output is correct
60 Correct 108 ms 26592 KB Output is correct
61 Correct 16 ms 21852 KB Output is correct
62 Correct 113 ms 30344 KB Output is correct
63 Correct 593 ms 86728 KB Output is correct
64 Correct 637 ms 86788 KB Output is correct
65 Correct 634 ms 86780 KB Output is correct
66 Correct 175 ms 29956 KB Output is correct
67 Correct 421 ms 37748 KB Output is correct
68 Correct 180 ms 29852 KB Output is correct
69 Correct 539 ms 39016 KB Output is correct
70 Correct 173 ms 29800 KB Output is correct
71 Correct 207 ms 29828 KB Output is correct
72 Correct 459 ms 38668 KB Output is correct
73 Correct 423 ms 38844 KB Output is correct
74 Correct 1137 ms 51372 KB Output is correct
75 Correct 635 ms 46020 KB Output is correct
76 Correct 931 ms 51692 KB Output is correct
77 Correct 850 ms 51884 KB Output is correct
78 Correct 246 ms 37856 KB Output is correct
79 Correct 376 ms 41248 KB Output is correct
80 Correct 207 ms 37916 KB Output is correct
81 Correct 357 ms 41240 KB Output is correct
82 Correct 995 ms 66928 KB Output is correct
83 Correct 1033 ms 66836 KB Output is correct
84 Correct 682 ms 66796 KB Output is correct
85 Correct 745 ms 66736 KB Output is correct
86 Correct 199 ms 29152 KB Output is correct
87 Correct 266 ms 31236 KB Output is correct
88 Correct 461 ms 38836 KB Output is correct
89 Correct 779 ms 51456 KB Output is correct