Submission #377184

# Submission time Handle Problem Language Result Execution time Memory
377184 2021-03-13T08:42:03 Z nafis_shifat Making Friends on Joitter is Fun (JOI20_joitter2) C++14
100 / 100
2377 ms 123428 KB
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
using namespace std;
const int mxn=1e5+5;
const int inf=1e9;


int parent[mxn];
ll sz[mxn];


set<int> adj[mxn];
set<int> in[mxn];
vector<int> nodes[mxn];

ll sz2[mxn][2];
set<int> eds[mxn][2];


set<int> reps[mxn];
set<int> ireps[mxn];



ll ans = 0;


int findrep(int u) {
	return parent[u] == u ? u : parent[u] = findrep(parent[u]);
}
void unite(int u,int v) {
	int U = findrep(u);
	int V = findrep(v);
	if(U == V) return;

	if(adj[V].count(U)) {
		ans += 2 * sz[U] * sz[V];
		if(sz[U] + sz2[U][0] + sz2[U][1] > sz[V] + sz2[V][0] + sz2[V][1]) swap(U,V);
		vector<int> nod1,nod2;
		for(int i : nodes[U]) {
		
			auto it1 = eds[i][0].begin();
			while(it1 != eds[i][0].end()) {
				int x = *it1;
				int X = findrep(x);

				if(X == V) {
					ans -= sz[V];

					in[V].erase(U);
					adj[U].erase(V);

					eds[x][1].erase(i);
					sz2[X][1]--;
					it1 = eds[i][0].erase(it1);
					sz2[U][0]--;
				} else if(in[V].count(X)) {
					nod1.push_back(X);
					in[X].erase(U);
					eds[x][1].erase(i);
					sz2[X][1]--;
					ans -= sz[X];
					it1 = eds[i][0].erase(it1);
					sz2[U][0]--;

				} else it1++;
			}



			auto it2 = eds[i][1].begin();
			while(it2 != eds[i][1].end()) {
				int x = *it2;
				int X = findrep(x);

				if(X == V) {
					ans -= sz[U];
					adj[V].erase(U);
					in[U].erase(V);
					eds[x][0].erase(i);
					sz2[X][0]--;
					it2 = eds[i][1].erase(it2);
					sz2[U][1]--;
				} else if(adj[V].count(X)) {
					nod2.push_back(X);
					adj[X].erase(U);
					eds[x][0].erase(i);
					sz2[X][0]--;
					ans -= sz[U];
					it2 = eds[i][1].erase(it2);
					sz2[U][1]--;
				} else if(ireps[V].count(x)) {
					sz2[U][1]--;
					reps[x].erase(U);
					eds[x][0].erase(i);
					sz2[X][0]--;
					it2 = eds[i][1].erase(it2);
					ans -= sz[U];
				} else it2++;
			}

		}


		while(!ireps[U].empty()) {
			auto it = ireps[U].begin();
			ireps[V].insert(*it);
			reps[*it].erase(U);
			reps[*it].insert(V);
			ireps[U].erase(it);
		}
		
	

		ans += sz2[U][1] * sz[V];
		ans += sz2[V][1] * sz[U];


		parent[U] = V;
		sz[V] += sz[U];
		sz2[V][0] += sz2[U][0];
		sz2[V][1] += sz2[U][1];

		while(!nodes[U].empty()) {
			int x = nodes[U].back();
			nodes[U].pop_back();
			nodes[V].push_back(x);

		}
		while(!adj[U].empty()) {
			int x = *adj[U].begin();
			in[x].erase(U);
			adj[U].erase(x);
			adj[V].insert(x);
			in[x].insert(V);
		}

		while(!in[U].empty()) {
			int x = *in[U].begin();
			in[U].erase(x);
			adj[x].erase(U);
			in[V].insert(x);
			adj[x].insert(V);
		}







		for(int x : nod1) {
			unite(V,x);
		}
		for(int x : nod2) {
			unite(x,V);
		}

	} else {
	    if(reps[u].count(V)) return;
	    
		ans += sz[V];
		eds[u][0].insert(v);
		eds[v][1].insert(u);
		
		adj[U].insert(V);
		in[V].insert(U);
		
		reps[u].insert(V);
		ireps[V].insert(u);
		
		
		sz2[U][0]++;
		sz2[V][1]++;
	}
}


int main() {
	int n,m;
	cin >> n >> m;

	for(int i = 1; i <= n; i++) {
		parent[i] = i;
		sz[i] = 1;
		sz2[i][0] = sz2[i][1] = 0;
		nodes[i].push_back(i);
	}


	for(int i = 1; i <= m; i++) {
		int u,v;
		scanf("%d%d",&u,&v);
		unite(u,v);

		cout<<ans<<endl;
	}
	
}

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:196:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  196 |   scanf("%d%d",&u,&v);
      |   ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 19 ms 30828 KB Output is correct
2 Correct 19 ms 30828 KB Output is correct
3 Correct 22 ms 30828 KB Output is correct
4 Correct 20 ms 30828 KB Output is correct
5 Correct 20 ms 30828 KB Output is correct
6 Correct 20 ms 30828 KB Output is correct
7 Correct 25 ms 30956 KB Output is correct
8 Correct 26 ms 30956 KB Output is correct
9 Correct 25 ms 30956 KB Output is correct
10 Correct 19 ms 30828 KB Output is correct
11 Correct 20 ms 30828 KB Output is correct
12 Correct 20 ms 30828 KB Output is correct
13 Correct 21 ms 30828 KB Output is correct
14 Correct 21 ms 30828 KB Output is correct
15 Correct 19 ms 30828 KB Output is correct
16 Correct 20 ms 30828 KB Output is correct
17 Correct 19 ms 30828 KB Output is correct
18 Correct 20 ms 30828 KB Output is correct
19 Correct 20 ms 30828 KB Output is correct
20 Correct 21 ms 30956 KB Output is correct
21 Correct 31 ms 30956 KB Output is correct
22 Correct 20 ms 30828 KB Output is correct
23 Correct 22 ms 30956 KB Output is correct
24 Correct 21 ms 30956 KB Output is correct
25 Correct 24 ms 30956 KB Output is correct
26 Correct 22 ms 30956 KB Output is correct
27 Correct 20 ms 30956 KB Output is correct
28 Correct 20 ms 30956 KB Output is correct
29 Correct 19 ms 30956 KB Output is correct
30 Correct 20 ms 30828 KB Output is correct
31 Correct 24 ms 30828 KB Output is correct
32 Correct 20 ms 30828 KB Output is correct
33 Correct 24 ms 30956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 30828 KB Output is correct
2 Correct 19 ms 30828 KB Output is correct
3 Correct 22 ms 30828 KB Output is correct
4 Correct 20 ms 30828 KB Output is correct
5 Correct 20 ms 30828 KB Output is correct
6 Correct 20 ms 30828 KB Output is correct
7 Correct 25 ms 30956 KB Output is correct
8 Correct 26 ms 30956 KB Output is correct
9 Correct 25 ms 30956 KB Output is correct
10 Correct 19 ms 30828 KB Output is correct
11 Correct 20 ms 30828 KB Output is correct
12 Correct 20 ms 30828 KB Output is correct
13 Correct 21 ms 30828 KB Output is correct
14 Correct 21 ms 30828 KB Output is correct
15 Correct 19 ms 30828 KB Output is correct
16 Correct 20 ms 30828 KB Output is correct
17 Correct 19 ms 30828 KB Output is correct
18 Correct 20 ms 30828 KB Output is correct
19 Correct 20 ms 30828 KB Output is correct
20 Correct 21 ms 30956 KB Output is correct
21 Correct 31 ms 30956 KB Output is correct
22 Correct 20 ms 30828 KB Output is correct
23 Correct 22 ms 30956 KB Output is correct
24 Correct 21 ms 30956 KB Output is correct
25 Correct 24 ms 30956 KB Output is correct
26 Correct 22 ms 30956 KB Output is correct
27 Correct 20 ms 30956 KB Output is correct
28 Correct 20 ms 30956 KB Output is correct
29 Correct 19 ms 30956 KB Output is correct
30 Correct 20 ms 30828 KB Output is correct
31 Correct 24 ms 30828 KB Output is correct
32 Correct 20 ms 30828 KB Output is correct
33 Correct 24 ms 30956 KB Output is correct
34 Correct 46 ms 31092 KB Output is correct
35 Correct 819 ms 35932 KB Output is correct
36 Correct 930 ms 40300 KB Output is correct
37 Correct 902 ms 40684 KB Output is correct
38 Correct 870 ms 40008 KB Output is correct
39 Correct 32 ms 31212 KB Output is correct
40 Correct 36 ms 31340 KB Output is correct
41 Correct 33 ms 31340 KB Output is correct
42 Correct 31 ms 31084 KB Output is correct
43 Correct 34 ms 31340 KB Output is correct
44 Correct 33 ms 31340 KB Output is correct
45 Correct 32 ms 31176 KB Output is correct
46 Correct 31 ms 31084 KB Output is correct
47 Correct 34 ms 31340 KB Output is correct
48 Correct 36 ms 31340 KB Output is correct
49 Correct 63 ms 32748 KB Output is correct
50 Correct 875 ms 40648 KB Output is correct
51 Correct 54 ms 31724 KB Output is correct
52 Correct 832 ms 37520 KB Output is correct
53 Correct 62 ms 32620 KB Output is correct
54 Correct 870 ms 39148 KB Output is correct
55 Correct 40 ms 32108 KB Output is correct
56 Correct 39 ms 32140 KB Output is correct
57 Correct 48 ms 32492 KB Output is correct
58 Correct 41 ms 32492 KB Output is correct
59 Correct 34 ms 31212 KB Output is correct
60 Correct 829 ms 33660 KB Output is correct
61 Correct 37 ms 31468 KB Output is correct
62 Correct 863 ms 39532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 30828 KB Output is correct
2 Correct 19 ms 30828 KB Output is correct
3 Correct 22 ms 30828 KB Output is correct
4 Correct 20 ms 30828 KB Output is correct
5 Correct 20 ms 30828 KB Output is correct
6 Correct 20 ms 30828 KB Output is correct
7 Correct 25 ms 30956 KB Output is correct
8 Correct 26 ms 30956 KB Output is correct
9 Correct 25 ms 30956 KB Output is correct
10 Correct 19 ms 30828 KB Output is correct
11 Correct 20 ms 30828 KB Output is correct
12 Correct 20 ms 30828 KB Output is correct
13 Correct 21 ms 30828 KB Output is correct
14 Correct 21 ms 30828 KB Output is correct
15 Correct 19 ms 30828 KB Output is correct
16 Correct 20 ms 30828 KB Output is correct
17 Correct 19 ms 30828 KB Output is correct
18 Correct 20 ms 30828 KB Output is correct
19 Correct 20 ms 30828 KB Output is correct
20 Correct 21 ms 30956 KB Output is correct
21 Correct 31 ms 30956 KB Output is correct
22 Correct 20 ms 30828 KB Output is correct
23 Correct 22 ms 30956 KB Output is correct
24 Correct 21 ms 30956 KB Output is correct
25 Correct 24 ms 30956 KB Output is correct
26 Correct 22 ms 30956 KB Output is correct
27 Correct 20 ms 30956 KB Output is correct
28 Correct 20 ms 30956 KB Output is correct
29 Correct 19 ms 30956 KB Output is correct
30 Correct 20 ms 30828 KB Output is correct
31 Correct 24 ms 30828 KB Output is correct
32 Correct 20 ms 30828 KB Output is correct
33 Correct 24 ms 30956 KB Output is correct
34 Correct 46 ms 31092 KB Output is correct
35 Correct 819 ms 35932 KB Output is correct
36 Correct 930 ms 40300 KB Output is correct
37 Correct 902 ms 40684 KB Output is correct
38 Correct 870 ms 40008 KB Output is correct
39 Correct 32 ms 31212 KB Output is correct
40 Correct 36 ms 31340 KB Output is correct
41 Correct 33 ms 31340 KB Output is correct
42 Correct 31 ms 31084 KB Output is correct
43 Correct 34 ms 31340 KB Output is correct
44 Correct 33 ms 31340 KB Output is correct
45 Correct 32 ms 31176 KB Output is correct
46 Correct 31 ms 31084 KB Output is correct
47 Correct 34 ms 31340 KB Output is correct
48 Correct 36 ms 31340 KB Output is correct
49 Correct 63 ms 32748 KB Output is correct
50 Correct 875 ms 40648 KB Output is correct
51 Correct 54 ms 31724 KB Output is correct
52 Correct 832 ms 37520 KB Output is correct
53 Correct 62 ms 32620 KB Output is correct
54 Correct 870 ms 39148 KB Output is correct
55 Correct 40 ms 32108 KB Output is correct
56 Correct 39 ms 32140 KB Output is correct
57 Correct 48 ms 32492 KB Output is correct
58 Correct 41 ms 32492 KB Output is correct
59 Correct 34 ms 31212 KB Output is correct
60 Correct 829 ms 33660 KB Output is correct
61 Correct 37 ms 31468 KB Output is correct
62 Correct 863 ms 39532 KB Output is correct
63 Correct 1573 ms 123372 KB Output is correct
64 Correct 1654 ms 123336 KB Output is correct
65 Correct 1630 ms 123428 KB Output is correct
66 Correct 907 ms 48732 KB Output is correct
67 Correct 1259 ms 57624 KB Output is correct
68 Correct 685 ms 41844 KB Output is correct
69 Correct 1137 ms 57100 KB Output is correct
70 Correct 810 ms 46332 KB Output is correct
71 Correct 790 ms 46180 KB Output is correct
72 Correct 1193 ms 58088 KB Output is correct
73 Correct 1218 ms 58344 KB Output is correct
74 Correct 2377 ms 81856 KB Output is correct
75 Correct 1763 ms 67080 KB Output is correct
76 Correct 1964 ms 78952 KB Output is correct
77 Correct 2003 ms 79356 KB Output is correct
78 Correct 810 ms 59112 KB Output is correct
79 Correct 1434 ms 63288 KB Output is correct
80 Correct 782 ms 59192 KB Output is correct
81 Correct 1301 ms 62692 KB Output is correct
82 Correct 1778 ms 97624 KB Output is correct
83 Correct 1773 ms 97388 KB Output is correct
84 Correct 1658 ms 114536 KB Output is correct
85 Correct 1709 ms 114484 KB Output is correct
86 Correct 906 ms 51176 KB Output is correct
87 Correct 1177 ms 53348 KB Output is correct
88 Correct 1193 ms 58220 KB Output is correct
89 Correct 1900 ms 77356 KB Output is correct